C Miscellaneous - Study Mode

[#46] Determine Output: void main()
{
static int var = 5
printf("%d ", var--)
if(var)
main()
}
Correct Answer

(B) 5 4 3 2 1

Explanation

Solution: When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.

[#47] Determine Output: void main()
{
char *p
printf("%d %d", sizeof(*p), sizeof(p))
}
Correct Answer

(B) 1 2

Explanation

Solution: The sizeof () operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof ( *p ) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof ( p ) gives 2.

[#48] Determine the Final Output: void main()
{
printf("
ab")
printf("x08si")
printf("
ha")
}
Correct Answer

(D) hai

Explanation

Solution:
- newline - printf ( "
ab " )
- Prints ab x08 - backspace - printf ( " x08 si " )
- firstly ' x08 ' removes 'b' from 'ab ' and then prints 'si'. So after execution of printf

[#49] Determine Output: void main()
{
int i=10
i=!i>14
printf("i=%d", i)
}
Correct Answer

(C) 0

Explanation

Solution: In the expression !i>14 , NOT (!) operator has more precedence than ">" symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).

[#50] Determine Output: void main()
{
int c = - -2
printf("c=%d", c)
}
Correct Answer

(C) 2

Explanation

Solution: In the given C program code snippet: void main()
{
int c = - -2
printf("c=%d", c)
} The line int c = - -2
is evaluated as follows: 1. The expression ` - -2 ` involves two consecutive negative signs.
2. The first negative sign (`-`) acts as the unary minus operator, and the second negative sign (`-`) is also a unary operator.
3. When two negative signs are used together, they effectively cancel each other out, so `- -2` becomes `2`. Therefore, the variable `c` is assigned the value `2` . The output of the `printf` function will be: 2