C Miscellaneous - Study Mode
[#61] Determine Output: void main()
{
int i = -1
+i
printf("i = %d, +i = %d", i, +i)
}
Correct Answer
(C) i = -1, +i = -1
Explanation
Solution: Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator).
[#62] Determine Output: void main()
{
char *str1 = "abcd"
char str2[] = "abcd"
printf("%d %d %d", sizeof(str1), sizeof(str2), sizeof("abcd"))
}
Correct Answer
(A) 2 5 5
Explanation
Solution: In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the 'x00' termination character). The third sizeof is similar to the second one.
[#63] Determine Output: void main()
{
char string[]="Hello World"
display(string)
}
void display(char *string)
{
printf("%s", string)
}
Correct Answer
(B) Compiler Error
Explanation
Solution: Type mismatch in redeclaration of function display. In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.
[#64] Determine Output: void main()
{
int i=5
printf("%d%d%d%d%d", i++, i--, ++i, --i, i)
}
Correct Answer
(A) 45545
Explanation
Solution: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result. See the picture below
[#65] Determine Output: #define square(x) x*x
void main()
{
int i
i = 64/square(4)
printf("%d", i)
}
Correct Answer
(B) 64
Explanation
Solution: The macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64