What does
printfreturn when you nest it inside anotherprintf?C#include <stdio.h> int main(void) { printf("%d\n", printf("Hello")); return 0; }It prints
Hello5. The innerprintfprintsHelloand returns the number of characters printed, 5.scanflikewise returns how many items it read: checkscanf("%d", &n) != 1.Does
sizeof(i++)incrementi?C#include <stdio.h> int main(void) { int i = 1; size_t n = sizeof(i++); printf("%zu %d\n", n, i); return 0; }It prints
4 1.sizeofneeds only the operand's type, soi++is never evaluated. The exception is a variable length array, whosesizeofis computed at runtime and does evaluate its operand.Is
-1 < 1utrue in C?C#include <stdio.h> int main(void) { int a = -1; unsigned int b = 1; if (a < b) printf("less\n"); else printf("greater\n"); return 0; }It prints
greater. Theintis converted to unsigned, and-1becomesUINT_MAX. The same bug hitsi < strlen(s) - 1on an empty string;-Wsign-comparewarns.What runs when a
switchcase has nobreak?C#include <stdio.h> int main(void) { int n = 2; switch (n) { case 1: printf("one "); case 2: printf("two "); case 3: printf("three "); break; default: printf("other "); } printf("\n"); return 0; }It prints
two three. Execution jumps tocase 2and falls through intocase 3until itsbreak. Mark deliberate fallthrough with a comment or C23's[[fallthrough]];so readers and-Wimplicit-fallthroughknow.What happens to a
staticlocal variable between function calls?C#include <stdio.h> int counter(void) { static int count = 0; count++; return count; } int main(void) { printf("%d ", counter()); printf("%d ", counter()); printf("%d\n", counter()); return 0; }It prints
1 2 3. A static local is initialized once, before the program starts, and keeps its value between calls; a plain local would give1 1 1. Its initializer must be a constant expression.Is a
floatset to 0.1 equal to the literal0.1?C#include <stdio.h> int main(void) { float f = 0.1; if (f == 0.1) printf("equal\n"); else printf("not equal\n"); return 0; }It prints
not equal.0.1has no exact binary form:fholds the nearestfloat, the literal is the nearestdouble, and they differ. Compare with a tolerance, such asfabs(a - b) < 1e-6.How many times does
i++ && i++incrementiwhenistarts at 0?C#include <stdio.h> int main(void) { int i = 0; if (i++ && i++) { printf("inside\n"); } printf("%d\n", i); return 0; }It prints
1. The lefti++yields 0 (false), so&&short-circuits and the right side never runs. It is defined because&&and||have a sequence point after the left operand, which is also what makesp != NULL && p->value > 0safe.What happens with a semicolon right after a
forloop's parentheses?C#include <stdio.h> int main(void) { int i; for (i = 0; i < 3; i++); { printf("i = %d\n", i); } return 0; }It prints
i = 3once. The;is the loop's empty body, so the loop countsito 3 and the braces run once afterwards.if (x > 0);has the same bug.What happens when an
unsigned chargoes past 255 or below 0?C#include <stdio.h> int main(void) { unsigned char c = 255; c++; unsigned char d = 0; d--; printf("%d %d\n", c, d); return 0; }It prints
0 255. Unsigned arithmetic wraps modulo 2 to the power of the bit width. Signed overflow such asINT_MAX + 1is undefined behavior, not a guaranteed wrap, and optimizers rely on that.Is
sizeof('a')the same assizeof(char)in C?C#include <stdio.h> int main(void) { printf("%zu %zu\n", sizeof(char), sizeof('a')); return 0; }It prints
1 4. In C a character constant like'a'has type int, so its size issizeof(int). In C++ it is acharand the size is 1.How does C parse
1 << 3 + 1and6 & 2 == 2?C#include <stdio.h> int main(void) { int a = 1 << 3 + 1; int b = (6 & 2 == 2) + 6; printf("%d %d\n", a, b); return 0; }It prints
16 6.+binds tighter than<<, so1 << 3 + 1is1 << 4.==binds tighter than&, so6 & 2 == 2is6 & 1, which is 0. Parenthesize bitwise expressions;-Wallwarns about both.What do
'A' + 2and'3' - '0'evaluate to?C#include <stdio.h> int main(void) { char c = 'A' + 2; printf("%c %d %d\n", c, c, '3' - '0'); return 0; }It prints
C 67 3. Acharis a small integer, so'A' + 2is 67, shown asCby%c.'3' - '0'is 3 because the standard requires the digits to be consecutive; letters are consecutive only in ASCII-based encodings.Practice it: To Lower Case
What is the output of
i = i++ + ++i;?There is none: modifying
itwice between sequence points (C11: unsequenced side effects on the same object) is undefined behavior, so any output is allowed.Term Meaning Example Undefined no requirements at all i = i++ + 1, signed overflowUnspecified one of several results, not documented order of f() + g()Implementation-defined documented by the compiler sizeof(int), signedness ofcharSee undefined behavior.
C Programming Output Questions
Predict-the-output questions from written tests; each one has a defined result on this build.