LearnFrenzy
Sign in Take a free test

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

  1. int* a, b; – sizes of a and b Medium
  2. Designated array initializer and a short string Medium
  3. A designated member followed by a positional value Hard
  4. A function using an extern variable defined later Easy
  5. sizeof 10, sizeof 10.0f and sizeof "C23" Medium
  6. Conversions during initialisation – output Medium
  7. Enum constants with explicit and implicit values Easy
  8. Modifying an array created by a compound literal Medium
  9. Three ways to declare "hello" – sizes Medium
  10. Repeated tentative definitions at file scope Hard
  11. Two variables of an unnamed struct type Medium
  12. C23 constexpr, auto and typeof together – output Hard

Control Instructions 12 questions

  1. Output of if (x = 5) in C Easy
  2. Dangling else with misleading indentation – output Medium
  3. switch with no break statements – output Easy
  4. default written first in a switch – output Medium
  5. Short-circuit evaluation with ++ side effects Hard
  6. FizzBuzz with an else-if ladder – output Easy
  7. Type of the conditional operator: sizeof(flag ? 1 : 2.0) Hard
  8. Counting vowels with grouped case labels Easy
  9. A loop built with goto – output Easy
  10. Comma operator inside an if condition – output Hard
  11. break inside a nested switch – output Medium
  12. C23 [[fallthrough]] chain – output Medium

Floating Point Issues 12 questions

  1. Output of 0.1 + 0.2 == 0.3 in C Easy
  2. float f = 0.1f compared with 0.1 – output Medium
  3. Storing 16777217 in a float – output Hard
  4. float r = 5 / 2 – output Easy
  5. printf rounding: 2.675 with %.2f and 2.5 with %.0f Hard
  6. Comparisons involving NaN – output Medium
  7. Arithmetic with infinity – output Medium
  8. Printing 123456789.0f – output Hard
  9. (int) -3.7 vs floor(-3.7) – output Medium
  10. (1e16 + 1.0) - 1e16 – output Hard
  11. float 0.5f == double 0.5 – output Medium
  12. fmod() vs remainder() – output Hard

Functions 12 questions

  1. Changing a parameter inside a function – output Easy
  2. static vs normal local variable – output in C Medium
  3. Recursion: printing before and after the call Medium
  4. Recursive digit sum f(f(n)) – output in C Medium
  5. Global vs local variable with the same name (shadowing) Medium
  6. Nested calls through an array of function pointers Medium
  7. How many calls does recursive fib(6) make? Hard
  8. Passing and returning a struct by value – output Easy
  9. Reassigning a pointer parameter vs writing through it Hard
  10. Mutual recursion: is_even() and is_odd() in C Medium
  11. Is the function called inside sizeof? (output) Hard
  12. C23 auto and typeof with a function – output Hard

Pointers 12 questions

  1. Output of changing a variable through a pointer in C Easy
  2. Output of pointer arithmetic on an array in C Easy
  3. Output of *p++ vs (*p)++ in C Medium
  4. Output of ++*p and *++p in C Medium
  5. Swap by value vs swap by pointers – output in C Easy
  6. Output of subtracting two pointers in C Medium
  7. sizeof array vs sizeof pointer in C – output Medium
  8. Pointer to pointer (**pp) output in C Easy
  9. Output of moving a char pointer through a string in C Easy
  10. Pointer to a 2D array row – output in C Hard
  11. Function pointer output question in C Medium
  12. Output of a pointer loop that steps by 2 in C Medium

Strings 12 questions

  1. sizeof vs strlen with an embedded \0 in C Medium
  2. Character arithmetic in C – output Easy
  3. What does printf() return? (output question) Medium
  4. Printing parts of a string with pointer offsets Easy
  5. strcmp() order: uppercase, prefixes and digits Medium
  6. strtok() token count and the changed string Hard
  7. printf %6s, %-6s and %.3s – output Easy
  8. Adjacent string literals are joined – output Medium
  9. char s[8] = "hi" – sizeof, strlen and the rest Easy
  10. Uppercasing a string in place with toupper() Easy
  11. sizeof('A') in C – output (C vs C++) Hard
  12. snprintf() return value when the buffer is too small Hard

C Input / Output 12 questions

  1. printf width, precision, - 0 and + flags – output Easy
  2. printf inside printf: what does the return value print? Medium
  3. Width taken from an argument with %*d – output Medium
  4. Precision on %s: %.3s and %8.3s – output Medium
  5. %c, %x, %#o and %#X with numbers – output Medium
  6. scanf("%c") after scanf("%d") – the leftover newline Medium
  7. sscanf return value when the second field does not match Medium
  8. fgets() and strlen: does the newline count? Easy
  9. scanf with field widths %2d and %3s – output Hard
  10. fseek with a negative offset from SEEK_END – output Medium
  11. Counting characters with while (!feof(fp)) – output Hard
  12. snprintf into a small buffer – result and return value Medium

Arrays 12 questions

  1. Output of mixed designated initializers in C Hard
  2. Output of 2[a], *(a + 3) and *a + 3 in C Medium
  3. &a + 1 vs a + 1 – output in C Hard
  4. Output of a 2D array with a flat initializer list Medium
  5. Output of a partly initialised 2D array in C Easy
  6. Copying an array inside a struct – output in C Hard
  7. Compound literal (int[]){…} output in C Medium
  8. sizeof of a 2D array, a row and an element Easy
  9. Printing an array backwards with *--p Medium
  10. sizeof of a variable length array in C Medium
  11. Does a function change the caller's array? (output) Easy
  12. C23 constexpr, typeof and = {} with arrays – output Hard

Data Types 12 questions

  1. sizeof(3.0f), sizeof(3.0) and sizeof('x') – output Medium
  2. unsigned 0 minus 1 – output Easy
  3. Comparing -1 with an unsigned 1 – output Hard
  4. signed char incremented past 127 (GCC/Clang) Hard
  5. Integer division and % with negative numbers in C Medium
  6. Adding two unsigned chars – int vs stored result Medium
  7. int i = 3.99 and 7 / 2 stored in a double – output Medium
  8. Octal, hexadecimal and escape literals – output Medium
  9. sizeof(i++) – is i incremented? (output) Medium
  10. Converting 5 and 0.1 to bool – output Medium
  11. Converting the character '7' to the number 7 Easy
  12. C23: sizeof(true) and sizeof(unsigned _BitInt(8)) Hard

C Variables 12 questions

  1. static and automatic variables inside a loop body Medium
  2. extern inside a block refers to the global – output Hard
  3. A static array without an initializer – output Easy
  4. static local initialised only once – output Easy
  5. A for-loop variable with the same name as an outer one Easy
  6. A local declared in a loop body is re-created each pass Easy
  7. Initialising variables from earlier ones in one declaration Easy
  8. Swapping two variables without a temporary – output Easy
  9. A static counter inside a recursive function Medium
  10. A global and a static local with the same name Medium
  11. Address of a static local across calls – output Medium
  12. C23 auto with 10L, 3u and 1.5f – output Hard

C Const 12 questions

  1. The object behind a pointer to const can still change Medium
  2. SQUARE(2 + 3) macro vs function – output Medium
  3. #define TOTAL 10 + 5 vs enum – output Medium
  4. A const local inside a loop – output Easy
  5. sizeof a const char pointer vs a const char array Medium
  6. Summing part of a static const table – output Easy
  7. Counting vowels with const strings and strchr() Easy
  8. A #define inside a function ignores scope – output Hard
  9. A const array of const strings – output Medium
  10. A function returning the same const string twice Medium
  11. Changing a value through a non-const alias – output Medium
  12. C23 constexpr sizes for a 2D array – output Hard

Structures, Unions, Enums 12 questions

  1. Struct designated initializers out of order – output Easy
  2. Enum values with gaps and expressions – output Medium
  3. sizeof three structures with padding – output Hard
  4. Reading an int's bytes through a union – output Hard
  5. Struct copy with a pointer member (shallow copy) Medium
  6. Array of structures with a pointer – output Medium
  7. Nested structure through a pointer – output Easy
  8. Storing 5 in a 2-bit bit-field – output Hard
  9. sizeof a union and an enum – output Easy
  10. Struct passed by value vs by pointer – output Easy
  11. offsetof() of structure members – output Medium
  12. C23 enum : unsigned char – size and value Hard

Typedef 12 questions

  1. sizeof and initialisation of a typedef struct Easy
  2. #define vs typedef for char pointers – sizes Medium
  3. Array typedef used for a 2D grid – sizes Medium
  4. A pipeline of typedef'd function pointers – output Medium
  5. Two typedefs of int mix without complaint – output Easy
  6. const String: writing the characters is allowed Hard
  7. A local typedef hiding a global one – output Medium
  8. Using both the struct tag and the typedef name Easy
  9. typedef of a function type (not a pointer) – output Hard
  10. A Byte typedef wraps around like unsigned char Easy
  11. An array of typedef'd structs – count and average Easy
  12. C23 typeof with an array and an element – output Hard

Bitwise Operators 12 questions

  1. Output of 6 & 3, 6 | 3 and 6 ^ 3 in C Easy
  2. Output of left and right shifts in C Easy
  3. Output of ~x and ~x + 1 for a signed int Medium
  4. Precedence trap: if (x & 1 == 0) – output Medium
  5. XOR swap without a temporary – output Easy
  6. Counting set bits with x &= x - 1 – output Medium
  7. Integer promotion with ~ on an unsigned char – output Hard
  8. Extracting nibbles and bytes with shifts and masks Medium
  9. Power-of-two check n & (n - 1) – output Easy
  10. Toggling letter case with ^= 32 – output Medium
  11. Right-shifting a negative number with GCC/Clang Hard
  12. C23 binary literals with &, | and ^ – output Medium

Command Line Arguments 12 questions

  1. argc and the arguments printed in reverse Easy
  2. First character of each argument – output Easy
  3. Total length of all arguments – output Easy
  4. atoi() on valid and invalid arguments – output Medium
  5. Counting arguments by walking argv until NULL Medium
  6. Comparing "10" and "9" as strings and as numbers Medium
  7. Modifying an argument string inside the program Medium
  8. A program run with no arguments – output Easy
  9. Multiplying two floating-point arguments with strtod() Easy
  10. A quoted argument containing a space – output Medium
  11. Parsing --size=42 and --name=box options – output Medium
  12. *(argv + 2) and **(argv + 1) – output Hard

C Loops 12 questions

  1. for loop with a semicolon after it – output Easy
  2. while (n--) – count and final n (output) Medium
  3. do-while with a false condition – output Easy
  4. break in the inner of two nested loops – output Medium
  5. Summing with continue to skip odd numbers Easy
  6. Two counters with the comma operator – output Medium
  7. Counting the digits of 0: while vs do-while Medium
  8. Nested loop starting at the outer index – count Medium
  9. Loop with a double counter stepping by 0.1 – output Hard
  10. while (i++ < 3) – final values (output) Medium
  11. break and continue inside a switch in a loop Hard
  12. Nested loop printing a 0/1 pattern – output Easy

Memory Allocation 12 questions

  1. sizeof p vs sizeof *p after malloc – output Easy
  2. calloc() then realloc() – which values survive? Medium
  3. memmove() shifting a string left – output Medium
  4. memset() with 0 and 0xFF on ints – output Hard
  5. Allocating memory through an int ** parameter – output Medium
  6. Copying a string to the heap – output Easy
  7. free(), set to NULL, free() again – output Easy
  8. Returning the address of a static local variable Medium
  9. Shrinking a block with realloc() – output Medium
  10. An array of heap-allocated strings – output Medium
  11. calloc() a struct – are pointer members NULL? Medium
  12. C23 auto and nullptr with malloc() – output Hard

Variable Number of Arguments 12 questions

  1. Largest of n integers with va_arg Easy
  2. Passing a count smaller than the number of values Medium
  3. Joining strings until a NULL sentinel Medium
  4. Reading promoted char and short arguments Medium
  5. Average of float values read as double Medium
  6. Return value of a vsnprintf-based wrapper Medium
  7. Two passes over the arguments with va_copy Hard
  8. A type string that describes each argument Medium
  9. Counting macro arguments with a compound literal Hard
  10. Stringifying __VA_ARGS__ with # Medium
  11. C23 __VA_OPT__ with and without arguments Hard
  12. C23 variadic function with no named parameter Hard

Complicated Declarations 12 questions

  1. sizeof int *a[4] vs int (*b)[4] Easy
  2. Stepping a pointer to an array through a 2D array Medium
  3. An array of pointers to strings with ** and [] Medium
  4. Calling through an array of function pointers Easy
  5. A function that returns a function pointer Medium
  6. A pointer to a function pointer – output Hard
  7. Changing values through const-qualified pointers Medium
  8. arr, &arr and &arr + 1 – output Hard
  9. typedef of an array type and a pointer to it Medium
  10. Passing a function pointer as a parameter Easy
  11. int (*(*get)(void))[3] in action Hard
  12. C23 typeof to declare a function-pointer array Hard

Operators and Expressions 12 questions

  1. b = a++ * 2 – output Easy
  2. 10 - 4 - 3 * 2 + 8 / 4 % 3 – output Medium
  3. a += b -= c *= 2 – output Hard
  4. Logical NOT precedence: !x + 1 vs !(x + 1) Medium
  5. x = (1, 2, 3) and y = 4, 5 – output Hard
  6. 7 / 2 * 2 vs 7 / 2.0 * 2 – output Medium
  7. r = x++ || x++ – output Hard
  8. Nested conditional operator without brackets – output Medium
  9. sizeof c, sizeof(c + 1) and sizeof(c = 'B') Hard
  10. Unary minus with % and / on negative numbers Medium
  11. n /= 4 then n *= 4 – output Easy
  12. C23: the type of a comparison result with auto Hard

Decision Making 12 questions

  1. Logical operators with non-0/1 values – output Easy
  2. De Morgan in practice: two forms of "outside 10–20" Medium
  3. Which functions run? A short-circuit trace Hard
  4. Marks and attendance rule with an else-if ladder Easy
  5. NULL pointer vs empty string in conditions Medium
  6. Logical XOR with !a != !b vs bitwise ^ – output Hard
  7. A tiny double as a condition vs after conversion to int Medium
  8. Exact vs tolerant comparison of doubles in a condition Medium
  9. Branchless min and max with masks – output Hard
  10. Leap years with a nested conditional expression Medium
  11. An if without braces and misleading indentation Easy
  12. C23 bool values from 2 and 0.5 – output Hard

Error Handling 12 questions

  1. strtol with an end pointer on "250ml" Easy
  2. strtol on a number too large for long Medium
  3. strtol when the string has no digits Medium
  4. strerror after fopen fails on a missing file Easy
  5. assert with a side effect when NDEBUG is defined Hard
  6. Order of atexit handlers and output from main Medium
  7. exit() called inside a nested function Easy
  8. setjmp return values with longjmp(env, 3) Medium
  9. What setjmp returns after longjmp(env, 0) Hard
  10. Is errno cleared by a successful call? Medium
  11. raise() with an installed signal handler Medium
  12. C23 ckd_add on INT_MAX + 1 – output Hard

C Library Functions 12 questions

  1. pow, sqrt, fabs and ceil/floor on negatives Easy
  2. div() with positive and negative numbers Medium
  3. Signs of strcmp results – output Easy
  4. Positions found by strchr, strrchr and strstr Medium
  5. memset(a, 1, sizeof a) on an int array Hard
  6. strtok on "a,b,,c" – how many tokens? Medium
  7. memmove with overlapping source and destination Medium
  8. toupper and isdigit over a string Easy
  9. qsort with a descending comparator Medium
  10. bsearch for a present and a missing key Medium
  11. strftime on time_t 0 with gmtime Medium
  12. strncpy with a short source pads with zeros Hard

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links