C Programming · Practice by type
C Programming MCQ Questions and Answers
Multiple-choice questions on C Programming concepts and common errors, topic by topic, each with the answer and a clear explanation.
- 460 questions
- 23 topics
- Answers checked on a real compiler
Declarations and Initializations 20 questions
- Which of these is a declaration but not a definition? Easy
- extern int fun(); vs int fun(); – any difference? Medium
- By default, a real number constant in C is a … Easy
- What are the types of linkage in C? Medium
- Which special symbol is allowed in a C variable name? Easy
- When must a function prototype appear? Easy
- Which of these is not a user-defined data type? Easy
- How to get the remainder of 3.14 divided by 2.1 in C Easy
- How to round 1.66 up to 2.0 in C Easy
- int a[5] = {1}; – what is a[3]? Easy
- What does volatile mean in a declaration? Medium
- What is nullptr in C23? Medium
- Error: using a variable before it is declared Easy
- Error: redeclaring a variable with a different type Easy
- Error: global variable initialised with a function call Medium
- Error: more initializers than array elements Easy
- Error: const variable assigned after its declaration Easy
- Error (C17): initialising a variable length array Hard
- Error: extern variable with an initializer inside a function Hard
- Error: int* p, q; then q = &x Medium
Control Instructions 20 questions
- Which values count as true in a C condition? Easy
- if (x = 5) – what happens in C? Easy
- Dangling else: which if does the else belong to? Medium
- Which type cannot be used in a C switch? Easy
- Rules for case labels in a C switch Medium
- Missing break in a switch case – what happens? Easy
- Can default appear before other cases in a switch? Medium
- Short-circuit evaluation of && in C Easy
- Type of cond ? 1 : 2.0 in C Hard
- Which goto is not allowed in C? Hard
- What does [[fallthrough]] do in C23? Medium
- case 1: int x = 5; – valid in C? Hard
- Error: using switch on a string in C Easy
- Error: duplicate case value in a switch Easy
- Error: a variable used as a case label Medium
- Bug: missing break makes extra cases run Easy
- Bug: = instead of == in an if condition Easy
- Bug: semicolon right after an if condition Easy
- Error (C17): declaration right after a case label Hard
- Error: goto jumping past a variable length array Hard
Floating Point Issues 20 questions
- Why is 0.1 + 0.2 != 0.3 in C? Easy
- Correct way to compare two doubles in C Medium
- Precision of float vs double in C Easy
- What is 1.0 / 0.0 in C (IEEE 754)? Medium
- Is NaN == NaN true in C? Medium
- scanf: %f vs %lf for float and double Medium
- Largest integer a float can store exactly Hard
- Best way to store money amounts in C Easy
- float f = 0.1f; is f == 0.1 true? Medium
- Is -0.0 == 0.0 in C? Medium
- round(2.5) vs rint(2.5) in C Hard
- What is Kahan summation used for? Hard
- Bug: comparing a computed price with == Easy
- Reading a double with scanf("%f") Medium
- Bug: area = 1/2 * base * height Easy
- Bug: using abs() on a double Medium
- Bug: a float counter that stops counting Hard
- Integer division by zero when computing an average Easy
- Bug: testing for NaN with x == NAN Medium
- Bug: int cents = 0.29 * 100 gives 28 Hard
Functions 20 questions
- How does C pass arguments to functions? Easy
- C23: what does int f() mean? Hard
- Why do we need a function prototype in C? Easy
- static local variable in a C function Easy
- What does static mean for a function in C? Medium
- Order of evaluation of function arguments in C Medium
- Omitting the return type of a function in modern C Medium
- Which is true about recursion in C? Easy
- Can a C function return an array? Medium
- What does the inline keyword do in C? Medium
- What does [[nodiscard]] do in C23? Medium
- What does [[noreturn]] / _Noreturn mean in C? Hard
- Missing return on one path of a non-void function Easy
- Error: calling a function before it is declared Easy
- Recursion without a base case – stack overflow Easy
- C23 error: int area(); called with arguments Hard
- Error: static local variable with a non-constant initializer Hard
- Recursive factorial overflows int at 13! Medium
- Bug: relying on the order function arguments are evaluated Hard
- Error: function pointer with the wrong type Medium
Pointers 20 questions
- What is a pointer in C? (MCQ) Easy
- Which operator gives the address of a variable in C? Easy
- Which operator gives the value at a pointer's address? Easy
- int *p, q; – what type is q? Easy
- Size of a pointer in C on a 64-bit system Easy
- What is a NULL pointer in C? Easy
- Pointer arithmetic: int pointer + 1 Medium
- a[i] is the same as which pointer expression? Easy
- What is a void pointer in C? Medium
- What is a dangling pointer in C? Medium
- const int *p vs int *const p Medium
- How is call by reference done in C? Easy
- Error: assigning an int to a pointer in C Easy
- Wild pointer: writing through an uninitialised pointer in C Easy
- Returning the address of a local variable in C Medium
- Use after free: reading memory after free() in C Medium
- Error: dereferencing a void pointer in C Medium
- Modifying a string literal through a char pointer in C Medium
- Error: re-pointing a constant pointer (int *const) in C Medium
- Reading past the end of an array with a pointer in C Easy
Strings 20 questions
- Size of char s[] = "Hello" in C Easy
- char s[5] = "Hello" – is it valid in C? Hard
- Why was gets() removed from C11? Easy
- char *p = "hi" vs char a[] = "hi" in C Medium
- What does strcmp() return in C? Easy
- strncpy() and the missing null terminator Medium
- Which C function safely builds a formatted string? Easy
- Which is true about strtok() in C? Medium
- Why cast to unsigned char before toupper()? Hard
- strlen() of a UTF-8 string like "héllo" Medium
- strdup() in standard C – since when? Medium
- Can you compare two strings with == in C? Easy
- strcat() buffer overflow in C Easy
- Bug: comparing strings with == in C Easy
- Error: strtok() on a string literal Medium
- Bug: scanf("%s") reads only the first word Easy
- Bug: fgets(buf, sizeof buf, …) with a malloc pointer Medium
- Missing '\0' after copying characters by hand Medium
- Bug: storing getchar() in a char instead of an int Hard
- char s[5] then strcpy(s, "Hello") – the off-by-one Easy
C Input / Output 20 questions
- What does scanf() return in C? Easy
- Which specifier reads a double with scanf()? Easy
- Why was gets() removed from C? Easy
- What does fgets() do with the newline? Easy
- What are stdin, stdout and stderr in C? Easy
- Which fopen() mode appends to a file? Easy
- What does fseek(fp, -3L, SEEK_END) do? Medium
- When does feof() return true? Medium
- How to open a JPEG for reading in C Easy
- What does * mean in scanf("%*d %d", &x)? Medium
- What does snprintf() return when output is truncated? Medium
- Is fflush(stdin) correct in standard C? Medium
- Error: scanf("%d", n) without the & Easy
- Error: reading from a file that failed to open Easy
- Error: scanf("%s") into a small array Medium
- Error: FILE fp = fopen(...) Easy
- Error: fprintf called like printf Easy
- Error: putchar("A") with a string Easy
- Error: fgets(line, stdin) without a size Easy
- Error: sprintf writing into a string literal Medium
Arrays 20 questions
- Remaining elements of a partly initialised array in C Easy
- Array size with designated initializers in C Hard
- When does an array NOT decay to a pointer in C? Medium
- Can one array be assigned to another in C? Easy
- C23 empty initializer int a[5] = {} Medium
- &a + 1 for an int array – which address? Medium
- Is i[a] the same as a[i] in C? Medium
- Which is true about variable length arrays (VLA) in C? Hard
- Meaning of int a[static 10] in a function parameter Hard
- Address of m[i][j] in a 2D array (row-major) Medium
- Valid prototype to pass a 2D array to a function in C Medium
- sizeof(a) / sizeof(a[0]) inside a function in C Medium
- Error: assigning one array to another in C Easy
- Off-by-one: writing a[5] in a 5-element array Easy
- Returning a local array from a function in C Medium
- Error: variable length array at file scope Hard
- Bug: counting array elements with sizeof inside a function Medium
- Error: 2D array parameter without the column size Medium
- Summing an uninitialised local array in C Easy
- Bug: memset(a, 1, sizeof a) on an int array Hard
Data Types 20 questions
- What is sizeof(char) in C? Easy
- Size of long on 64-bit Windows vs 64-bit Linux Medium
- Range of a signed char in C Easy
- Unsigned vs signed integer overflow in C Medium
- Is plain char signed or unsigned in C? Medium
- Integer promotion of char and short in C Medium
- Why is -1 < 1u false in C? Hard
- Correct printf format for size_t in C Easy
- int32_t vs int_least32_t in C Hard
- Type of the literal 3.14 vs 3.14f in C Easy
- bool in C23 vs C99 Medium
- What is _BitInt(N) in C23? Hard
- Printing a double with %d in C Easy
- Milliseconds per year overflows int Medium
- strlen(s) - 1 with an empty string Hard
- Bug: storing 300 in an unsigned char Easy
- Bug: average computed with integer division Easy
- Bug: unsigned subtraction that should be negative Medium
- Converting a too-large double to int Hard
- Error (C17): bool used without <stdbool.h> Medium
C Variables 20 questions
- Value of an uninitialised local variable in C Easy
- Value of an uninitialised global variable in C Easy
- Which is not a valid variable name in C? Easy
- Is extern int x; a declaration or a definition? Medium
- Lifetime of a static local variable in C Easy
- What does static do to a global variable in C? Medium
- Scope of a variable declared inside a block in C Easy
- Can you take the address of a register variable? Medium
- Scope vs lifetime of a variable in C Medium
- Which identifiers are reserved in C? Hard
- What does thread_local mean in C? Hard
- C23 auto: what type does auto x = 2.5f; get? Medium
- Using an uninitialised local variable as a total Easy
- Error: using a variable outside the block that declared it Easy
- Error: defining the same variable twice in one scope Easy
- Link error: extern variable that is never defined Medium
- Error: taking the address of a register variable Medium
- Error: a variable name starting with a digit Easy
- Bug: a function accumulates into a global by mistake Medium
- Error (C23): auto without an initializer Hard
C Const 20 questions
- const int x = 5; x = 6; – what happens? Easy
- Is int const *p the same as const int *p? Medium
- Can a const local be initialised at run time? Medium
- Can const int size a global array in C? Hard
- #define vs const in C – the key difference Easy
- Modifying a const object through a cast in C Medium
- Passing const char * to a char * parameter Medium
- What is the type of a string literal in C? Hard
- What does const do on a by-value parameter? Easy
- const int *const p – what can change? Easy
- What does volatile const mean in C? Hard
- Where can a C23 constexpr object be used? Medium
- Error: assigning to a const variable Easy
- Error: writing through a pointer to const Easy
- Error: changing a member of a const struct Easy
- Casting away const to change a static const value Hard
- Error: const int used as a global array size Hard
- Error: a const int used as a case label Hard
- A const string passed to a function that modifies it Medium
- Bug: a macro without parentheses computes the wrong area Medium
Structures, Unions, Enums 20 questions
- p->x vs (*p).x vs *p.x in C Easy
- sizeof(struct { char c; int i; }) in C Medium
- Can two structures be compared with == in C? Easy
- Size of a union in C Easy
- Value of an enum constant after an explicit value Easy
- Rules for a flexible array member in C Hard
- Is struct assignment a deep copy in C? Medium
- What does typedef struct point Point; allow? Easy
- Can you take the address of a bit-field in C? Medium
- Anonymous unions and structs in C11 Medium
- C23 enum with a fixed underlying type Hard
- Self-referential structure: why a pointer? Easy
- Error: a struct that contains itself Easy
- Error: comparing two structs with == Easy
- Error: using . on a pointer to a structure Easy
- Error: taking the address of a bit-field Medium
- Double free after copying a struct with a pointer Hard
- Bug: comparing structs with memcmp() and padding Hard
- Error: flexible array member that is not the last member Medium
- Off-by-one with an enum COUNT member Medium
Typedef 20 questions
- What does typedef do in C? Easy
- Can typedef be combined with static? Hard
- Self-referencing typedef struct needs a tag Medium
- typedef char *String; – what is const String s? Hard
- #define PTR int * vs typedef with two variables Medium
- Correct typedef for a function pointer in C Medium
- typedef int Row[4]; Row grid[3]; – what is grid? Medium
- Can a typedef be declared inside a function? Easy
- Why avoid typedef names ending in _t? Medium
- Are typedef Meters and typedef Seconds different types? Medium
- Can the same typedef be declared twice? Hard
- What does typeof give you in C23? Medium
- Error: using the typedef name inside its own struct Medium
- Error: typedef combined with static Medium
- Error: #define used as a pointer type for two variables Medium
- Error: re-pointing a const pointer-typedef variable Hard
- Error: a typedef name reused as a variable in the same scope Easy
- Error: declaring a variable of an incomplete (opaque) type Hard
- Error: wrong function signature for a typedef'd pointer Medium
- A pointer hidden behind a typedef leads to a double free Hard
Bitwise Operators 20 questions
- What is 5 & 3 in C? Easy
- What is x ^ x in C? Easy
- How to test whether bit k is set in C Easy
- How to clear bit k in C Easy
- Is x << 1 the same as x * 2? Easy
- Shifting a 32-bit int by 32 – what happens? Medium
- Right-shifting a negative number in C Medium
- Left-shifting a negative value in C Hard
- Precedence trap: x & 1 == 0 Medium
- What is ~0u in C? Easy
- C23 binary literals and digit separators Medium
- Integer promotion: ~ on an unsigned char Hard
- Shifting a 32-bit value by 32 bits Medium
- 1 << 31 with a signed int Medium
- Left-shifting a negative number Medium
- Bug: && used instead of & to test a flag Easy
- Bug: ! used instead of ~ to clear a bit Medium
- Bug: comparing (x & 4) with 1 Easy
- Combining bytes stored in a signed char buffer Hard
- Bug: using ^ to square a number in C Easy
Command Line Arguments 20 questions
- Which main() signatures are standard C? Easy
- What is argc for ./app one two? Easy
- What does argv[0] contain? Easy
- What is argv[argc] in C? Medium
- What is the type of argv in main()? Medium
- How does a number typed on the command line arrive? Easy
- How to pass an argument that contains spaces Easy
- Why prefer strtol() over atoi() for arguments? Medium
- What happens to main()'s return value? Medium
- Why does ./calc 3 * 4 not receive a *? Medium
- Is getopt() part of standard C? Medium
- Standard way to read environment variables in C Medium
- Using argv[1] without checking argc Easy
- Bug: comparing an argument with == "-v" Easy
- Bug: atoi() accepts "12abc" as 12 Medium
- Loop with i <= argc reads the NULL terminator Easy
- Error: argv[1] + argv[2] to join two arguments Medium
- Bug: counting arguments with sizeof(argv) Medium
- Error: void main() in standard C Easy
- Bug: (int) atol() on an out-of-range argument Hard
C Loops 20 questions
- Which C loop always runs its body at least once? Easy
- What does for (;;) mean in C? Easy
- Where does continue jump to in a for loop? Medium
- How many loops does break exit in nested loops? Easy
- How many times does for (i = 0; i < 10; i += 3) run? Easy
- Scope of a variable declared in a for loop header Easy
- for (unsigned i = 10; i >= 0; i--) – how many passes? Medium
- Why is a float loop counter dangerous in C? Medium
- Iterations of two nested n-loops in C Easy
- while (1) vs for (;;) in C Easy
- while (n--) with n = 3 – how many passes? Medium
- Why do C macros use do { … } while (0)? Hard
- Unsigned countdown loop that never stops in C Medium
- Semicolon after while (…) – infinite loop Easy
- Error: using a for-loop variable after the loop Easy
- Error: missing semicolon after do-while Easy
- Sum of cubes in a loop overflows int Medium
- Bug: float counter that misses its end value Hard
- Error: break outside a loop or switch Easy
- Loop with a char counter that can never reach 200 Hard
Memory Allocation 20 questions
- What does malloc() return in C? Easy
- malloc() vs calloc() – the real difference Easy
- What does free(NULL) do in C? Easy
- What happens when realloc() fails? Medium
- What is a memory leak in C? Easy
- Stack vs heap: where is malloc() memory? Easy
- Why write malloc(n * sizeof *p)? Medium
- Should you cast the result of malloc() in C? Medium
- memcpy() with overlapping regions Medium
- realloc(p, 0) in C23 Hard
- How long does malloc() memory live? Easy
- What does aligned_alloc() do in C11? Hard
- Memory leak: overwriting the only pointer to a block Easy
- Double free of the same pointer in C Easy
- Using malloc()'s result without checking for NULL Medium
- Heap overflow: malloc(strlen(s)) without + 1 Easy
- Error: freeing the address of a local variable Medium
- Error: freeing a pointer that was moved inside the block Medium
- Bug: p = realloc(p, n) leaks when realloc() fails Hard
- Bug: malloc(sizeof p) instead of malloc(sizeof *p) Medium
Variable Number of Arguments 20 questions
- Which header provides va_list and va_arg? Easy
- Where must the ellipsis appear in a parameter list? Easy
- What is the second argument of va_start? Easy
- How does a variadic function know the argument count? Easy
- How to read a float passed to a variadic function Medium
- How to read a char passed through ... Medium
- How does printf know how many arguments it has? Easy
- Why use va_copy? Medium
- Why do vprintf and vsnprintf exist? Medium
- What does the standard say about omitting va_end? Medium
- What does __VA_ARGS__ represent? Easy
- What did C23 change for variadic functions? Hard
- Error: va_arg(ap, float) Medium
- Error: ellipsis before a named parameter Easy
- Error (C17): a function with only ... as parameters Medium
- Error: va_start used in a function with no ... parameter Medium
- Error: forgetting the NULL sentinel Medium
- Error: passing ints where va_arg expects double Medium
- Error: using one va_list for two vprintf calls Hard
- Error (C17): variadic macro called with no extra arguments Hard
Complicated Declarations 20 questions
- What does int *p[10]; declare? Easy
- What does int (*p)[10]; declare? Easy
- int *f(void); vs int (*f)(void); Easy
- Declare an array of 4 pointers to functions Medium
- What is char *(*fp)(const char *)? Medium
- Declare a function returning a function pointer Hard
- What does the declaration of signal() mean? Hard
- int *const p vs const int *p Medium
- Are const char *p and char const *p the same? Medium
- Which of these declarations is illegal in C? Medium
- What type does an int m[][5] parameter really have? Medium
- What does typedef void (*callback)(int); define? Easy
- Error: a function declared to return an array Medium
- Error: declaring an array of functions Medium
- Error: int *fp(int); then fp = square; Easy
- Error: writing through a const int * pointer Easy
- Error: moving a const pointer Easy
- Error: function pointer of the wrong type Medium
- Error: int *p[4] = &row; instead of int (*p)[4] Medium
- Error: function-pointer typedef used as a return type Hard
Operators and Expressions 20 questions
- What is 7 / 2 in C? Easy
- What is -7 % 3 in C? Medium
- Operator precedence: 2 + 3 * 4 Easy
- Associativity of the assignment operator in C Easy
- What does i = i++ do in C? Medium
- What does a relational operator return in C? Easy
- Is sizeof a function or an operator? Medium
- Value of a comma expression in C Medium
- How is a && b || c grouped in C? Easy
- x = 5; y = x++ + 1; – values of x and y Easy
- Overflow-safe average of two ints in C Hard
- Can % be used with double in C? Easy
- Undefined behaviour: x = x++ in a loop Medium
- Undefined behaviour: printf("%d %d", i++, i++) Medium
- Error: using % with a double operand Easy
- Average of two large ints overflows Medium
- Error: applying ++ to an expression that is not a variable Easy
- Bug: *count++ instead of (*count)++ Hard
- Bug: a macro without brackets changes the precedence Medium
- Bug: expecting sizeof(counter++) to increment Medium
Decision Making 20 questions
- What are !0 and !5 in C? Easy
- De Morgan's law: !(a && b) equals … Easy
- How to test that x is between 10 and 20 in C Easy
- Which condition is safe when p may be NULL? Medium
- Correct condition to check if a string equals "admin" Easy
- How to compare two doubles in an if condition Medium
- Nested if vs && – are they the same? Easy
- if (p) when p is a pointer – what does it test? Easy
- What happens when 42 is stored in a bool? Medium
- Logical XOR of two conditions in C Hard
- Which condition is always true? Medium
- Can a C switch test a range such as 90 to 100? Hard
- Bug: an age range check that is always true Easy
- NULL check written after the dereference Medium
- Bug: if (!count == 5) Medium
- Bug: a branch that can never run Easy
- Bug: integer division inside a percentage condition Medium
- Error: case ranges in a switch (not standard C) Hard
- Bug: mixing && and || without brackets in an access check Medium
- Error: a chained comparison a < b < c Medium
Error Handling 20 questions
- How does C report errors without exceptions? Easy
- Which header declares errno? Easy
- When should errno be set to 0? Medium
- perror() vs strerror() – the difference Easy
- Where are EXIT_SUCCESS and EXIT_FAILURE defined? Easy
- exit() vs abort() in C Medium
- In what order do atexit handlers run? Medium
- What happens to assert() when NDEBUG is defined? Medium
- When is static_assert checked? Easy
- What does setjmp return after longjmp(env, 0)? Hard
- Why is strtol preferred over atoi? Easy
- ferror() vs feof() after a failed read Medium
- Error: if (fp = NULL) instead of fp == NULL Easy
- Error: using errno without including errno.h Easy
- Error: static_assert assumes a 64-bit int Medium
- Error: assert used to validate user input Medium
- Error: checking errno after a successful call Medium
- Error: checking the divisor after dividing Easy
- Error: freeing a buffer twice on the error path Hard
- Error: calling exit() without including stdlib.h Easy
C Library Functions 20 questions
- Which header file declares sqrt()? Easy
- Which header contains qsort() and bsearch()? Easy
- What does a header file contain? Easy
- Is main() a library function? Easy
- What does strcmp() return? Easy
- Does strlen() count the null terminator? Easy
- Which function copies overlapping memory safely? Medium
- What does srand() do? Easy
- abs() vs fabs() in C Easy
- What does strtok() do to its input string? Medium
- What does time(NULL) return? Easy
- What does isdigit() return for true? Medium
- Error: calling sqrt() without math.h Easy
- Error: strcpy into a buffer that is too small Easy
- Error: qsort comparator with int * parameters Medium
- Error: strlen on a char array without '\0' Medium
- Error: memcpy with overlapping regions Medium
- Error: toupper() applied to a whole string Easy
- Error: comparing strings with == Easy
- Error: strncpy leaves the copy unterminated Hard
Ready to test your Programming?
Timed online tests with instant scores and explanations.