To read a integer variable, find the correct format given below : Int m;
scanf ("\n %d", & m);
Two things must both be right — the conversion specifier and the ampersand — and only option 4 has both.
scanf("\n %d", &m);
Why %d. The specifier tells scanf how to interpret the incoming characters and how many bytes to write. %d means signed decimal integer, matching the declaration int m. Option 3's %c would store a single raw character — typing 5 would leave the code 53, not the value 5 — and option 2's %s would copy characters into memory as a string, overrunning a four-byte integer at once. Option 2 is also missing its closing quotation mark, so it will not even compile.
Why the ampersand. This is the real point of the question. C passes everything by value, so scanf receives copies of its arguments and could never change a caller's variable through one. It is given the address instead:
\(\texttt{\&m}\ \longrightarrow\ \text{a pointer to m}\)
and writes through that pointer. Omitting it, as option 1 does, passes the current value of m — whatever rubbish an uninitialised variable holds — and scanf treats that number as an address and writes there. The result is undefined behaviour: usually a segmentation fault, occasionally silent corruption of unrelated memory, which is worse.
| Option | Fault |
|---|---|
| 1 | No & — passes a value as an address |
| 2 | Wrong specifier and an unterminated string |
| 3 | %c stores a character code, not a number |
| 4 | Correct |
The one exception to the rule is worth remembering because it is the source of much confusion: with %s and a character array, no ampersand is used — an array name already decays to the address of its first element. Everything else, including every numeric type, needs the &.
The leading \n in the format string is not required. Whitespace in a scanf format means "skip any amount of whitespace here", so "\n %d" and " %d" and "%d" behave identically for numeric input, which already skips leading blanks.
Hence, the correct form is scanf("\n %d", &m);.
The function "isalnum( )" returns a non-zero value if the character is alphanumeric and zero otherwise. What is header file to be used in the main program ?
Find the correct list, which is the reverse (at all levels) of the given list :
[[4, 7, 8], 2, 9, [19, 6], 20, 3]
(i) 3, 20
(ii) 9, 2
(iii) [6, 19]
(iv) [8, 7, 4]
Which of the following is not an infinite loop ?
(A) int i = 1;
while (1)
{
i++ ;
}
(B) for ( ; ; ) ;
(C) int t = 0, f ;
while (t)
{
f = 1;
}
(D) int y, x = 0 ;
do
{
y = x;
}
while (x == 0);
The three types of loops available in C language are
i. for
ii. while
iii. do-while
Which loops do not operate without testing the condition even once ?
Match the following :
| List – I | List – II |
| a. associativity | i. memory storage |
| b. # define | ii. if-then-else |
| c. auto | iii. operators with equal precedence |
| d. conditional operator | iv. define operator |
Codes :
Assertion (A) : 'C' uses many data types such as integers (short and long) and float etc.
Reason (R) : Conversion specifier used for short unsigned integer is % lu.
Which bitwise operator is suitable for turning off a particular bit in a number ?
Which storage class is used for the variables that are often required in a program ?
Which is not a proper way of array declaration ?
Exponentiation operation in C is carried out as
Which of the following is NOT a state function or state variable in a thermodynamic system?