C Programming · Practice by type
C Programming Output Questions with Answers
Predict-the-output questions for every C Programming topic. Each program was compiled and run, so the answer is its real output, with a step-by-step explanation.
- 276 questions
- 23 topics
- Answers checked on a real compiler
Declarations and Initializations 12 questions
- int* a, b; – sizes of a and b Medium
- Designated array initializer and a short string Medium
- A designated member followed by a positional value Hard
- A function using an extern variable defined later Easy
- sizeof 10, sizeof 10.0f and sizeof "C23" Medium
- Conversions during initialisation – output Medium
- Enum constants with explicit and implicit values Easy
- Modifying an array created by a compound literal Medium
- Three ways to declare "hello" – sizes Medium
- Repeated tentative definitions at file scope Hard
- Two variables of an unnamed struct type Medium
- C23 constexpr, auto and typeof together – output Hard
Control Instructions 12 questions
- Output of if (x = 5) in C Easy
- Dangling else with misleading indentation – output Medium
- switch with no break statements – output Easy
- default written first in a switch – output Medium
- Short-circuit evaluation with ++ side effects Hard
- FizzBuzz with an else-if ladder – output Easy
- Type of the conditional operator: sizeof(flag ? 1 : 2.0) Hard
- Counting vowels with grouped case labels Easy
- A loop built with goto – output Easy
- Comma operator inside an if condition – output Hard
- break inside a nested switch – output Medium
- C23 [[fallthrough]] chain – output Medium
Floating Point Issues 12 questions
- Output of 0.1 + 0.2 == 0.3 in C Easy
- float f = 0.1f compared with 0.1 – output Medium
- Storing 16777217 in a float – output Hard
- float r = 5 / 2 – output Easy
- printf rounding: 2.675 with %.2f and 2.5 with %.0f Hard
- Comparisons involving NaN – output Medium
- Arithmetic with infinity – output Medium
- Printing 123456789.0f – output Hard
- (int) -3.7 vs floor(-3.7) – output Medium
- (1e16 + 1.0) - 1e16 – output Hard
- float 0.5f == double 0.5 – output Medium
- fmod() vs remainder() – output Hard
Functions 12 questions
- Changing a parameter inside a function – output Easy
- static vs normal local variable – output in C Medium
- Recursion: printing before and after the call Medium
- Recursive digit sum f(f(n)) – output in C Medium
- Global vs local variable with the same name (shadowing) Medium
- Nested calls through an array of function pointers Medium
- How many calls does recursive fib(6) make? Hard
- Passing and returning a struct by value – output Easy
- Reassigning a pointer parameter vs writing through it Hard
- Mutual recursion: is_even() and is_odd() in C Medium
- Is the function called inside sizeof? (output) Hard
- C23 auto and typeof with a function – output Hard
Pointers 12 questions
- Output of changing a variable through a pointer in C Easy
- Output of pointer arithmetic on an array in C Easy
- Output of *p++ vs (*p)++ in C Medium
- Output of ++*p and *++p in C Medium
- Swap by value vs swap by pointers – output in C Easy
- Output of subtracting two pointers in C Medium
- sizeof array vs sizeof pointer in C – output Medium
- Pointer to pointer (**pp) output in C Easy
- Output of moving a char pointer through a string in C Easy
- Pointer to a 2D array row – output in C Hard
- Function pointer output question in C Medium
- Output of a pointer loop that steps by 2 in C Medium
Strings 12 questions
- sizeof vs strlen with an embedded \0 in C Medium
- Character arithmetic in C – output Easy
- What does printf() return? (output question) Medium
- Printing parts of a string with pointer offsets Easy
- strcmp() order: uppercase, prefixes and digits Medium
- strtok() token count and the changed string Hard
- printf %6s, %-6s and %.3s – output Easy
- Adjacent string literals are joined – output Medium
- char s[8] = "hi" – sizeof, strlen and the rest Easy
- Uppercasing a string in place with toupper() Easy
- sizeof('A') in C – output (C vs C++) Hard
- snprintf() return value when the buffer is too small Hard
C Input / Output 12 questions
- printf width, precision, - 0 and + flags – output Easy
- printf inside printf: what does the return value print? Medium
- Width taken from an argument with %*d – output Medium
- Precision on %s: %.3s and %8.3s – output Medium
- %c, %x, %#o and %#X with numbers – output Medium
- scanf("%c") after scanf("%d") – the leftover newline Medium
- sscanf return value when the second field does not match Medium
- fgets() and strlen: does the newline count? Easy
- scanf with field widths %2d and %3s – output Hard
- fseek with a negative offset from SEEK_END – output Medium
- Counting characters with while (!feof(fp)) – output Hard
- snprintf into a small buffer – result and return value Medium
Arrays 12 questions
- Output of mixed designated initializers in C Hard
- Output of 2[a], *(a + 3) and *a + 3 in C Medium
- &a + 1 vs a + 1 – output in C Hard
- Output of a 2D array with a flat initializer list Medium
- Output of a partly initialised 2D array in C Easy
- Copying an array inside a struct – output in C Hard
- Compound literal (int[]){…} output in C Medium
- sizeof of a 2D array, a row and an element Easy
- Printing an array backwards with *--p Medium
- sizeof of a variable length array in C Medium
- Does a function change the caller's array? (output) Easy
- C23 constexpr, typeof and = {} with arrays – output Hard
Data Types 12 questions
- sizeof(3.0f), sizeof(3.0) and sizeof('x') – output Medium
- unsigned 0 minus 1 – output Easy
- Comparing -1 with an unsigned 1 – output Hard
- signed char incremented past 127 (GCC/Clang) Hard
- Integer division and % with negative numbers in C Medium
- Adding two unsigned chars – int vs stored result Medium
- int i = 3.99 and 7 / 2 stored in a double – output Medium
- Octal, hexadecimal and escape literals – output Medium
- sizeof(i++) – is i incremented? (output) Medium
- Converting 5 and 0.1 to bool – output Medium
- Converting the character '7' to the number 7 Easy
- C23: sizeof(true) and sizeof(unsigned _BitInt(8)) Hard
C Variables 12 questions
- static and automatic variables inside a loop body Medium
- extern inside a block refers to the global – output Hard
- A static array without an initializer – output Easy
- static local initialised only once – output Easy
- A for-loop variable with the same name as an outer one Easy
- A local declared in a loop body is re-created each pass Easy
- Initialising variables from earlier ones in one declaration Easy
- Swapping two variables without a temporary – output Easy
- A static counter inside a recursive function Medium
- A global and a static local with the same name Medium
- Address of a static local across calls – output Medium
- C23 auto with 10L, 3u and 1.5f – output Hard
C Const 12 questions
- The object behind a pointer to const can still change Medium
- SQUARE(2 + 3) macro vs function – output Medium
- #define TOTAL 10 + 5 vs enum – output Medium
- A const local inside a loop – output Easy
- sizeof a const char pointer vs a const char array Medium
- Summing part of a static const table – output Easy
- Counting vowels with const strings and strchr() Easy
- A #define inside a function ignores scope – output Hard
- A const array of const strings – output Medium
- A function returning the same const string twice Medium
- Changing a value through a non-const alias – output Medium
- C23 constexpr sizes for a 2D array – output Hard
Structures, Unions, Enums 12 questions
- Struct designated initializers out of order – output Easy
- Enum values with gaps and expressions – output Medium
- sizeof three structures with padding – output Hard
- Reading an int's bytes through a union – output Hard
- Struct copy with a pointer member (shallow copy) Medium
- Array of structures with a pointer – output Medium
- Nested structure through a pointer – output Easy
- Storing 5 in a 2-bit bit-field – output Hard
- sizeof a union and an enum – output Easy
- Struct passed by value vs by pointer – output Easy
- offsetof() of structure members – output Medium
- C23 enum : unsigned char – size and value Hard
Typedef 12 questions
- sizeof and initialisation of a typedef struct Easy
- #define vs typedef for char pointers – sizes Medium
- Array typedef used for a 2D grid – sizes Medium
- A pipeline of typedef'd function pointers – output Medium
- Two typedefs of int mix without complaint – output Easy
- const String: writing the characters is allowed Hard
- A local typedef hiding a global one – output Medium
- Using both the struct tag and the typedef name Easy
- typedef of a function type (not a pointer) – output Hard
- A Byte typedef wraps around like unsigned char Easy
- An array of typedef'd structs – count and average Easy
- C23 typeof with an array and an element – output Hard
Bitwise Operators 12 questions
- Output of 6 & 3, 6 | 3 and 6 ^ 3 in C Easy
- Output of left and right shifts in C Easy
- Output of ~x and ~x + 1 for a signed int Medium
- Precedence trap: if (x & 1 == 0) – output Medium
- XOR swap without a temporary – output Easy
- Counting set bits with x &= x - 1 – output Medium
- Integer promotion with ~ on an unsigned char – output Hard
- Extracting nibbles and bytes with shifts and masks Medium
- Power-of-two check n & (n - 1) – output Easy
- Toggling letter case with ^= 32 – output Medium
- Right-shifting a negative number with GCC/Clang Hard
- C23 binary literals with &, | and ^ – output Medium
Command Line Arguments 12 questions
- argc and the arguments printed in reverse Easy
- First character of each argument – output Easy
- Total length of all arguments – output Easy
- atoi() on valid and invalid arguments – output Medium
- Counting arguments by walking argv until NULL Medium
- Comparing "10" and "9" as strings and as numbers Medium
- Modifying an argument string inside the program Medium
- A program run with no arguments – output Easy
- Multiplying two floating-point arguments with strtod() Easy
- A quoted argument containing a space – output Medium
- Parsing --size=42 and --name=box options – output Medium
- *(argv + 2) and **(argv + 1) – output Hard
C Loops 12 questions
- for loop with a semicolon after it – output Easy
- while (n--) – count and final n (output) Medium
- do-while with a false condition – output Easy
- break in the inner of two nested loops – output Medium
- Summing with continue to skip odd numbers Easy
- Two counters with the comma operator – output Medium
- Counting the digits of 0: while vs do-while Medium
- Nested loop starting at the outer index – count Medium
- Loop with a double counter stepping by 0.1 – output Hard
- while (i++ < 3) – final values (output) Medium
- break and continue inside a switch in a loop Hard
- Nested loop printing a 0/1 pattern – output Easy
Memory Allocation 12 questions
- sizeof p vs sizeof *p after malloc – output Easy
- calloc() then realloc() – which values survive? Medium
- memmove() shifting a string left – output Medium
- memset() with 0 and 0xFF on ints – output Hard
- Allocating memory through an int ** parameter – output Medium
- Copying a string to the heap – output Easy
- free(), set to NULL, free() again – output Easy
- Returning the address of a static local variable Medium
- Shrinking a block with realloc() – output Medium
- An array of heap-allocated strings – output Medium
- calloc() a struct – are pointer members NULL? Medium
- C23 auto and nullptr with malloc() – output Hard
Variable Number of Arguments 12 questions
- Largest of n integers with va_arg Easy
- Passing a count smaller than the number of values Medium
- Joining strings until a NULL sentinel Medium
- Reading promoted char and short arguments Medium
- Average of float values read as double Medium
- Return value of a vsnprintf-based wrapper Medium
- Two passes over the arguments with va_copy Hard
- A type string that describes each argument Medium
- Counting macro arguments with a compound literal Hard
- Stringifying __VA_ARGS__ with # Medium
- C23 __VA_OPT__ with and without arguments Hard
- C23 variadic function with no named parameter Hard
Complicated Declarations 12 questions
- sizeof int *a[4] vs int (*b)[4] Easy
- Stepping a pointer to an array through a 2D array Medium
- An array of pointers to strings with ** and [] Medium
- Calling through an array of function pointers Easy
- A function that returns a function pointer Medium
- A pointer to a function pointer – output Hard
- Changing values through const-qualified pointers Medium
- arr, &arr and &arr + 1 – output Hard
- typedef of an array type and a pointer to it Medium
- Passing a function pointer as a parameter Easy
- int (*(*get)(void))[3] in action Hard
- C23 typeof to declare a function-pointer array Hard
Operators and Expressions 12 questions
- b = a++ * 2 – output Easy
- 10 - 4 - 3 * 2 + 8 / 4 % 3 – output Medium
- a += b -= c *= 2 – output Hard
- Logical NOT precedence: !x + 1 vs !(x + 1) Medium
- x = (1, 2, 3) and y = 4, 5 – output Hard
- 7 / 2 * 2 vs 7 / 2.0 * 2 – output Medium
- r = x++ || x++ – output Hard
- Nested conditional operator without brackets – output Medium
- sizeof c, sizeof(c + 1) and sizeof(c = 'B') Hard
- Unary minus with % and / on negative numbers Medium
- n /= 4 then n *= 4 – output Easy
- C23: the type of a comparison result with auto Hard
Decision Making 12 questions
- Logical operators with non-0/1 values – output Easy
- De Morgan in practice: two forms of "outside 10–20" Medium
- Which functions run? A short-circuit trace Hard
- Marks and attendance rule with an else-if ladder Easy
- NULL pointer vs empty string in conditions Medium
- Logical XOR with !a != !b vs bitwise ^ – output Hard
- A tiny double as a condition vs after conversion to int Medium
- Exact vs tolerant comparison of doubles in a condition Medium
- Branchless min and max with masks – output Hard
- Leap years with a nested conditional expression Medium
- An if without braces and misleading indentation Easy
- C23 bool values from 2 and 0.5 – output Hard
Error Handling 12 questions
- strtol with an end pointer on "250ml" Easy
- strtol on a number too large for long Medium
- strtol when the string has no digits Medium
- strerror after fopen fails on a missing file Easy
- assert with a side effect when NDEBUG is defined Hard
- Order of atexit handlers and output from main Medium
- exit() called inside a nested function Easy
- setjmp return values with longjmp(env, 3) Medium
- What setjmp returns after longjmp(env, 0) Hard
- Is errno cleared by a successful call? Medium
- raise() with an installed signal handler Medium
- C23 ckd_add on INT_MAX + 1 – output Hard
C Library Functions 12 questions
- pow, sqrt, fabs and ceil/floor on negatives Easy
- div() with positive and negative numbers Medium
- Signs of strcmp results – output Easy
- Positions found by strchr, strrchr and strstr Medium
- memset(a, 1, sizeof a) on an int array Hard
- strtok on "a,b,,c" – how many tokens? Medium
- memmove with overlapping source and destination Medium
- toupper and isdigit over a string Easy
- qsort with a descending comparator Medium
- bsearch for a present and a missing key Medium
- strftime on time_t 0 with gmtime Medium
- strncpy with a short source pads with zeros Hard
Ready to test your Programming?
Timed online tests with instant scores and explanations.