What is the output of the above code?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
b = c = 1;
printf("%d\t", b >> -1);
printf("%d\t", c >> -1);
}
int fun1(char b, char *r c)
{
b = b << 1;
c = c = 1 << 1;
printf("%d\n",c);
}
What is the output of the above code?
Show answer & explanation
The code exhibits undefined behavior in C. Right-shifting by a negative value (b >> -1 and c >> -1) is undefined according to the C standard. Additionally, the function signature int fun1(char b, char *r c) contains a syntax error (stray 'r' before parameter 'c'), which will prevent compilation. Even if that typo is corrected, the negative shift operations make this code invalid.
Step-by-step Derivation:
Analysis of compilation and runtime issues:
Syntax Error: The function parameter list
char *r cis malformed. The 'r' should not be there—it should bechar *c. This causes a compile-time error immediately.Undefined Behavior (if syntax is fixed): Even correcting the typo, the expressions
b >> -1andc >> -1invoke undefined behavior. Per C11 standard (6.5.7.3): 'If the value of the right operand is negative or is greater than or equal to the width in bits of the promoted left operand, the behavior is undefined.'Conclusion: The code will not compile due to the syntax error in the function definition. This makes option D correct.