C Miscellaneous - Study Mode
[#56] Determine Output: void main()
{
struct xx
{
int x=3
char name[] = "hello"
}
struct xx *s = malloc(sizeof(struct xx))
printf("%d", s->x)
printf("%s", s->name)
}
Correct Answer
(B) Compiler Error
Explanation
Solution: Initialization should not be done for structure members inside the structure declaration.
[#57] Determine output: void main()
{
extern int i
i=20
printf("%d", sizeof(i))
}
Correct Answer
(D) Linker Error
Explanation
Solution: Linker error: undefined symbol '_i'. Explanation: extern declaration specifies that the variable i is defined somewhere else. The compiler passes the external variable to be resolved by the linker. So compiler doesn't find any error. During linking the linker searches for the definition of i. Since it is not found the linker flags an error.
[#58] Determine Output: void main()
{
int i=0, j=0
if(i && j++)
printf("%d..%d", i++, j)
printf("%d..%d", i, j)
}
Correct Answer
(C) 0..0
Explanation
Solution: The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed. The values of i and j remains unchanged and get printed.
[#59] Determine Output: void main()
{
static int i=5
if(--i){
main()
printf("%d ", i)
}
}
Correct Answer
(B) 0 0 0 0
Explanation
Solution: The variable "i" is declared as static, hence memory for I will be allocated for only once, as it encounters the statement. The function main () will be called recursively unless I becomes equal to 0, and since main () is recursively called, so the value of static I ie., 0 will be printed every time the control is returned.
[#60] Determine Output: void main()
{
int i=-1, j=-1, k=0, l=2, m
m = i++ && j++ && k++ || l++
printf("%d %d %d %d %d", i, j, k, l, m)
}
Correct Answer
(C) 0 0 1 3 1
Explanation
Solution: Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression 'i++ && j++ && k++' is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for '0 || 0' combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.