C Programming Output Questions with Answers
Last updated 2026-11-19
"What is the output of this program?" is the most common question type in campus placement tests for C. The snippets are short, but each is built around one rule: macro expansion is textual, integer division truncates, unsigned arithmetic wraps, a char array is not a pointer, and switch cases fall through. Learn the rule and the answer follows.
Every program below was compiled and run with gcc on a 64-bit Linux system, and the output shown is what it printed. Where a question has undefined or implementation-defined behaviour, the answer says so instead of giving a single number. For the concepts behind them, see C language questions and C pointers questions.
| Compiler | gcc, 64-bit Linux, default int is 32 bits |
|---|---|
| Questions | 16 programs with output and reason |
| Level | Easy to hard |
How to approach an output question
- First scan for undefined behaviour: modifying a variable twice between sequence points, reading uninitialised data, out-of-range access.
- Check types before arithmetic: integer promotion, signed versus unsigned, int versus float.
- Expand macros by hand, without adding any parentheses.
- Note the platform: sizeof(int) is 4 and sizeof(void*) is 8 in these answers.
- If the format specifier does not match the argument type, the behaviour is undefined.
Why platform matters
A few answers depend on the implementation: the size of long, whether plain char is signed, the order function arguments are evaluated. Tests usually pick a 32-bit or 64-bit assumption, so state yours. Questions whose answer is "undefined" are fair to say so; saying it with a reason scores more than guessing one number.
C Programming Output Questions with Answers: questions with worked answers
1 What does this program print?
easyIt prints 5, then 9. The macro SQ(x) is x*x with no parentheses, so SQ(2+1) expands to 2+1*2+1, which is 2 + 2 + 1 = 5. Parenthesising every use of the parameter and the whole body, as in SQ2, gives (2+1)*(2+1) = 9.
c#define SQ(x) x*x #define SQ2(x) ((x)*(x)) int main() { printf("%d\n%d\n", SQ(2+1), SQ2(2+1)); }2 What is the output: strlen versus sizeof?
easyIt prints 5 8 and then 5 6. strlen counts characters before the terminating zero, so it is 5 for both. sizeof of a pointer is the pointer size (8 on 64-bit); sizeof of the array a is 6, the five letters plus the terminator.
cchar *s = "hello"; char a[] = "hello"; printf("%zu %zu\n", strlen(s), sizeof(s)); // 5 8 printf("%zu %zu\n", strlen(a), sizeof(a)); // 5 63 What do integer division and modulus print for negative operands?
easyIt prints 3, -3, -1 and 3.500000. Since C99 integer division truncates toward zero, so 7/2 is 3 and -7/2 is -3. The remainder takes the sign of the dividend: -7 % 3 is -1, consistent with (a/b)*b + a%b == a. Dividing by 2.0 makes the operation floating point.
cprintf("%d %d %d %f\n", 7/2, -7/2, -7%3, 7/2.0);4 What does this print and why? (unsigned wrap-around)
mediumIt prints 4294967295. Unsigned arithmetic is modulo 2^32, so decrementing 0 wraps to the maximum value. A common related trap is comparing a signed -1 with an unsigned int: the signed value converts to unsigned and the comparison -1 < 1u is false.
cunsigned int u = 0; u--; printf("%u\n", u); // 4294967295 printf("%d\n", -1 < 1u); // 05 What does this print? (assignment in a condition)
easyIt prints "in 5". "x = 5" is an assignment, not a comparison, so it stores 5 and the expression evaluates to 5, which is true. Writing 5 == x (a Yoda condition) or enabling warnings catches it.
cint x = 10; if (x = 5) printf("in %d\n", x);6 What is the output? (operator precedence with shift)
mediumIt prints 16. Addition binds tighter than shift, so 1 << 3 + 1 is 1 << 4. Precedence puzzles like this appear in many tests: arithmetic beats shifts, shifts beat relational operators, relational beat bitwise AND, and && beats ||.
cprintf("%d\n", 1 << 3 + 1); // 167 What does this print? (switch fall-through)
mediumIt prints 23. The matching case 2 prints 2, and without a break execution falls into case 3, which prints 3 and then breaks. default is skipped. A missing break is the deliberate trap; modern compilers warn with -Wimplicit-fallthrough.
cswitch (2) { case 1: printf("1"); case 2: printf("2"); case 3: printf("3"); break; default: printf("d"); }8 What is the output? (printf return value)
mediumIt prints ab2. The inner printf("ab") runs first, printing ab and returning the number of characters written, 2. The outer printf then prints that integer. printf returns the count of characters output, or a negative value on error.
cprintf("%d\n", printf("ab")); // ab29 What does this print? (short-circuit evaluation)
mediumIt prints 1, 0, 1 and then 4. p && q is logical AND, so 1. p & q is bitwise: 3 & 4 is 011 & 100 = 0. In p || q++ the left side is true, so the right side is never evaluated and q stays 4, which the last line confirms.
cint p = 3, q = 4; printf("%d\n", p && q); // 1 printf("%d\n", p & q); // 0 printf("%d\n", p || q++); // 1 printf("%d\n", q); // 410 What is the output? (char literal type and arithmetic)
mediumIt prints 97, 4 and b. In C a character constant has type int, so sizeof('a') is 4 (in C++ it is 1). The value is the ASCII code 97, and 'a' + 1 is 98, printed as the character b with %c.
cprintf("%d\n", 'a'); // 97 printf("%zu\n", sizeof('a')); // 4 printf("%c\n", 'a' + 1); // b11 What does this print? (octal and hex literals)
mediumIt prints 24. A leading zero makes an octal literal, so 010 is 8, and 0x10 is 16. 8 + 16 is 24. This is a favourite trap because 010 looks like ten.
cprintf("%d\n", 010 + 0x10); // 2412 What does this print? (float comparison)
hardIt prints 0 1. The literal 0.1 is a double, but f holds the nearest float. Comparing f == 0.1 converts f to double, which gives a slightly different value from the double 0.1, so it is false. f == 0.1f compares two floats and is true. Never compare floating point values with ==; compare with a tolerance.
cfloat f = 0.1f; printf("%d %d\n", f == 0.1, f == 0.1f); // 0 113 What is the output? (char overflow and promotion)
mediumIt prints -56 and 0. On gcc plain char is signed, so 200 does not fit and the stored value is implementation defined, giving -56 (200 - 256). An unsigned char holding 255 incremented wraps to 0, since it converts to int, adds 1, and the result 256 is converted back modulo 256. Both are shown with %d after promotion to int.
cchar c = 200; printf("%d\n", c); // -56 (signed char on gcc x86) unsigned char uc = 255; uc++; printf("%d\n", uc); // 014 What is the output of this program? (static and comma operator)
hardIt prints 0 and then 5. A static variable without an initialiser is zero-initialised, so sc is 0. In the second statement the comma operator evaluates m == 0, discards it, and yields 5 as the value of the whole parenthesised expression. Inside a function call, a comma without parentheses would separate arguments instead.
cstatic int sc; int m = 0; printf("%d\n", sc); // 0 printf("%d\n", (m == 0, 5)); // 515 What is the output of printf("%d %d", i++, i)?
hardThere is no single correct answer. The order of evaluation of function arguments is unspecified, and reading i in one argument while modifying it in another without a sequence point is undefined behaviour. GCC happened to print 5 6 for i = 5, but another compiler or optimisation level may print 5 5 or 6 6. The correct answer in an interview is "undefined, do not write this". The same applies to k++ + ++k and a[i] = i++.
cint i = 5; printf("%d %d\n", i++, i); // undefined behaviour16 What does a mismatched printf format do?
hardIt is undefined behaviour. printf reads its arguments according to the format specifier, so printing a double with %d, or a ternary that yields a double (cond ? 10 : 20.5) with %d, reads the wrong type and prints garbage, a different garbage on each run or platform. Print with %f, or cast the value. Modern compilers warn with -Wformat. Also match %lld to long long, %zu to size_t and %p to void*.
Frequently asked questions
Why do output questions mention undefined behaviour?
Because C lets the compiler assume it never happens, so no output is guaranteed. A strong answer names the rule instead of guessing a value.
Should I assume 16-bit or 32-bit int in a test?
Read the question. Modern tests assume 32-bit int unless a Turbo C setting is stated, and the answer here assumes 32-bit.
How do I practise C output questions effectively?
Predict first, then compile with gcc -Wall -Wextra and compare. Keep a list of the rules each mistake exposed.
Are these the same as C++ output questions?
Many are, but character literal size, bool and stream formatting differ. See C++ interview questions for the C++ side.
Related guides
More in Programming Language Interview Questions by Language
- C# Async Await Interview Questions
- C# Coding Interview Questions
- C# Collections Interview Questions
- C# Delegates and Events Interview Questions
- C# Interview Questions and Answers
- C# LINQ Interview Questions
- C# OOPs Interview Questions
- C++ Coding Interview Questions
- C++ Interview Questions and Answers
- C++ Memory Management Interview Questions
- C++ OOPS Interview Questions
- C++ Pointers and References Interview Questions