C Miscellaneous - Study Mode
[#51] Determine Output: #define int char
void main()
{
int i = 65
printf("sizeof(i)=%d", sizeof(i))
}
Correct Answer
(B) sizeof(i)=1
Explanation
Solution: Since the #define replaces the string int by the macro char .
[#52] Determine Output: void main()
{
int i=3
switch(i)
{
default: printf("zero")
case 1: printf("one")
break
case 2: printf("two")
break
case 3: printf("three")
break
}
}
Correct Answer
(B) three
Explanation
Solution: The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.
[#53] Determine Output: void main()
{
int i=1, j=2
switch(i)
{
case 1: printf("GOOD")
break
case j: printf("BAD")
break
}
}
Correct Answer
(C) Compiler Error
Explanation
Solution: Compiler Error: Constant expression required in function main. The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error). Note: Enumerated types can be used in case statements.
[#54] Determine Output: void main()
{
int i
printf("%d", scanf("%d", &i))
// value 10 is given as input here
}
Correct Answer
(B) 1
Explanation
Solution: scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.
[#55] Determine Output: void main()
{
int i=0
for(
i++
printf("%d", i))
printf("%d", i)
}
Correct Answer
(A) 1
Explanation
Solution: Before entering into the for loop checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).