OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

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?

Choose one option.
Show answer & explanation
Answer: D. Compile time error

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:

  1. Syntax Error: The function parameter list char *r c is malformed. The 'r' should not be there—it should be char *c. This causes a compile-time error immediately.

  2. Undefined Behavior (if syntax is fixed): Even correcting the typo, the expressions b >> -1 and c >> -1 invoke 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.'

  3. Conclusion: The code will not compile due to the syntax error in the function definition. This makes option D correct.