C Notes
Practice C output-based interview questions covering operator precedence, pointer tricks, type promotion, undefined behavior, and tricky printf scenarios.
Output prediction questions test your understanding of how C actually works – operator precedence, type conversions, undefined behavior, and memory layout. These are favorites in campus placements and technical interviews because they can't be answered by memorizing syntax.
Operator Precedence and Evaluation
Question 1: Pre and Post Increment
a = 7, b = 12
Explanation: This is actually undefined behavior in C because a is modified twice between sequence points. Different compilers may give different results. GCC typically evaluates: ++a makes a=6, then a++ uses 6 (post), giving b=6+6=12, then a becomes 7.
Interview Tip: If asked this, mention it's undefined behavior. The interviewer may be testing whether you know that.
Question 2: sizeof with Expressions
4 a = 5
Explanation: sizeof is a compile-time operator (for non-VLAs). The expression inside is never evaluated – only its type is determined. So a++ never executes.
Question 3: Comma Operator
a = 3 x = 13, y = 12
Explanation: The comma operator evaluates left to right and returns the last value. (1,2,3) evaluates to 3. For y: x increments to 11, 12, 13 – but x++ returns 12 (pre-increment value of last x++).
Question 4: Integer Promotion
#include <stdio.h>
int main() {
char c = 125;
c = c + 10;
printf("%d\n", c);
unsigned char uc = 250;
uc = uc + 10;
printf("%d\n", uc);
return 0;
}-121 4
Explanation: char is signed (typically -128 to 127). 125+10=135 overflows to -121 (wraps around). unsigned char (0-255): 250+10=260, wraps to 260-256=4.
Question 5: printf Return Value
Hello 5 World5 1 [y=1]
Explanation: printf returns the number of characters printed. "Hello" = 5 chars. Inner printf prints "World" (5 chars, returns 5), outer printf prints "5" (1 char, returns 1).
Question 6: String and Pointer
H e o llo l
Explanation: *str = first char. *(str+1) = second char. str+2 starts string from index 2. 2[str] = *(2+str) = *(str+2) = 'l'.
Question 7: Bitwise Operations
#include <stdio.h>
int main() {
printf("%d\n", 5 & 3); // 101 & 011 = 001
printf("%d\n", 5 | 3); // 101 | 011 = 111
printf("%d\n", 5 ^ 3); // 101 ^ 011 = 110
printf("%d\n", ~0); // All bits flipped
printf("%d\n", 1 << 4); // 1 shifted left 4 = 16
printf("%d\n", 16 >> 2); // 16 shifted right 2 = 4
return 0;
}1 7 6 -1 16 4
Question 8: Structure Size and Padding
#include <stdio.h>
struct A { char c; int i; char d; };
struct B { char c; char d; int i; };
struct C { int i; char c; char d; };
int main() {
printf("struct A: %zu\n", sizeof(struct A));
printf("struct B: %zu\n", sizeof(struct B));
printf("struct C: %zu\n", sizeof(struct C));
return 0;
}struct A: 12 struct B: 8 struct C: 8
Explanation: The compiler adds padding for alignment. In struct A: char(1) + 3 padding + int(4) + char(1) + 3 padding = 12. Reordering members (B, C) reduces padding.
Question 9: Macro Side Effects
7 16 m=6, a=7, b=4
Question 10: Array and sizeof
In main: 40 In func: 8 str: 6 ptr: 8
Question 11: Logical Operators Short-Circuit
a=1, b=0 c=0, b=0 x=1, b=5
Explanation: || stops if left is true (1). && stops if left is false (0). The right side is not evaluated when short-circuiting occurs.
Question 12: Ternary and Type Promotion
#include <stdio.h>
int main() {
int a = 5;
float b = 3.5;
printf("%f\n", (a > 3) ? a : b); // int promoted to float
printf("%d\n", (a > 3) ? 1 : 2.5); // 2.5 promotes 1 to double
char *p = NULL;
printf("%s\n", p ? p : "NULL pointer");
return 0;
}5.000000 1 NULL pointer
Question 13: Recursion Output
#include <stdio.h>
void fun(int n) {
if (n == 0) return;
printf("%d ", n);
fun(n - 1);
printf("%d ", n);
}
int main() {
fun(3);
printf("\n");
return 0;
}3 2 1 1 2 3
Explanation: Prints n going down (3,2,1), reaches base case, then unwinds printing n again going up (1,2,3).
Question 14: Static Variable
1 2 3
Explanation: Static variables retain their value between function calls. Initialized only once.
Question 15: Union Behavior
#include <stdio.h>
union Data {
int i;
float f;
char c;
};
int main() {
union Data d;
d.i = 65;
printf("d.i = %d\n", d.i);
printf("d.c = %c\n", d.c); // Same memory, interpreted as char
d.f = 3.14;
printf("d.i = %d\n", d.i); // Garbage - memory now holds float bits
printf("d.f = %f\n", d.f);
printf("sizeof(union) = %zu\n", sizeof(union Data));
return 0;
}d.i = 65 d.c = A d.i = 1078523331 d.f = 3.140000 sizeof(union) = 4
Tips for Output Questions
| Trap | What to Watch For | ||
|---|---|---|---|
| Operator precedence | *p++ vs (*p)++ vs ++*p | ||
| Type promotion | char/short → int in expressions | ||
| sizeof | Compile-time, doesn't evaluate expression | ||
| Short-circuit | && and ` | ` may skip evaluation | |
| Macro expansion | Text substitution, no type safety | ||
| Static variables | Persist between calls | ||
| Array decay | Arrays become pointers in functions | ||
| Undefined behavior | Multiple modifications between sequence points |
Summary
Output prediction questions test deep understanding of C's rules. Focus on: operator precedence and associativity, type promotion rules (char→int, int→float), sizeof behavior (compile-time, no evaluation), short-circuit evaluation in logical operators, macro pitfalls (lack of parentheses, double evaluation), and the difference between arrays and pointers. When in doubt during an interview, mention if something is undefined behavior – interviewers respect that awareness.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Output Prediction Questions in C – What Will This Program Print?.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, interview, preparation, output, based, questions
Related C Programming Topics