What will be the output of the program given below?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What will be the output of the program given below?**
#include <stdio.h>
int main(void)
{
char c = -1;
signed char d = 0;
printf("%d ", c);
printf("%d ", --d);
printf("%d ", d | | c);
return 0;
}
Show answer & explanation
The first printf outputs c = -1 as -1. The second printf outputs --d (pre-decrement of d from 0 to -1) as -1. The third printf evaluates the logical OR operator (d || c): since both d = -1 and c = -1 are non-zero (true), the result is 1. Note: the question text contains a typo 'd | | c' (bitwise OR with spaces), but standard interpretation in a logical context means the logical OR operator '||', which returns 1 (true) when at least one operand is non-zero.
Step-by-step Derivation:
Step 1: char c = -1 is initialized. When printed as %d, char -1 sign-extends to int -1.
Step 2: signed char d = 0 is initialized. --d (pre-decrement) changes d to -1 and prints -1.
Step 3: d || c evaluates the logical OR of d = -1 and c = -1. Both are non-zero (true), so the result is 1.
Output: -1 -1 1