MCQ Practice13 min read

C Programming MCQs with Answers and Explanations

By the QUFF Team

C MCQs are unusually unforgiving, because C is a language where the honest answer is often 'it depends on the implementation' or 'that is undefined behaviour'. Questions are built exactly on that: the size of an int, what happens when you modify a string literal, what i++ + ++i evaluates to. This set covers the recurring questions with the standard's actual position, which is what separates a well-prepared candidate from one who memorised what their compiler happened to print.

A laptop showing a code editor with curly braces and a terminal window, representing C programming

Data types and sizeof

What is the size of an int in C? (a) Always 2 bytes (b) Always 4 bytes (c) Implementation-defined, at least 16 bits (d) Same as a pointer

(c) Implementation-defined, with a minimum range requiring at least 16 bits. It is 4 bytes on most modern desktop compilers, but the standard only guarantees minimum ranges - which is exactly why portable C code uses the fixed-width types from stdint.h.

What does sizeof(char) return, and is it guaranteed?

1, and yes - the standard defines a char as one byte and sizeof measures in units of char. It is the only size C guarantees outright.

What is the difference between strlen("hello") and sizeof("hello")?

strlen returns 5, counting characters up to the terminator. sizeof returns 6, because the string literal is a char array that includes the terminating '\0'. This off-by-one is one of the most-asked C MCQs.

What is the output of printf("%d", 10 % -3); ?

1. Since C99, integer division truncates toward zero, so 10 / −3 is −3, and the remainder satisfies a = (a/b)*b + a%b, giving 10 − 9 = 1. The sign of the result follows the dividend, not the divisor.

What is the value of an uninitialised local variable? (a) 0 (b) NULL (c) Indeterminate (d) Compiler error

(c) Indeterminate - reading it is undefined behaviour. Only variables with static storage duration (globals and static locals) are zero-initialised. 'Garbage value' is the informal answer; 'undefined behaviour' is the correct one.

Pointers and arrays

What is a[i] equivalent to in C? (a) *(a + i) (b) *(i + a) (c) i[a] (d) All of these

(d) All of these. Subscripting is defined as pointer arithmetic plus dereference, and addition commutes - so i[a] compiles and works, however unreadable it is. MCQs love this because it exposes whether you understand subscripting or merely use it.

What is the difference between char *s = "abc"; and char s[] = "abc"; ?

The first makes s a pointer to a string literal, which lives in read-only storage - attempting to modify it is undefined behaviour and typically crashes. The second creates a modifiable array of 4 chars initialised with a copy of the literal, so s[0] = 'x' is legal.

Inside a function declared as void f(int arr[]), what does sizeof(arr) give?

The size of a pointer, not of the array. An array parameter decays to a pointer, so the length information is lost at the call boundary - which is why C functions must take the length as a separate argument.

What does *p++ do? (a) Increments the pointed-to value (b) Dereferences, then increments the pointer (c) Increments the pointer, then dereferences (d) Compiler error

(b). Postfix ++ binds more tightly than unary *, so the expression yields the current *p and then advances p. To increment the pointed-to value you need (*p)++.

What is a dangling pointer?

A pointer that still holds the address of memory which has been freed or has gone out of scope. Dereferencing it is undefined behaviour. Set pointers to NULL after free() to make misuse detectable rather than silently corrupting memory.

What does malloc return, and how does it differ from calloc?

malloc returns a void* to uninitialised memory, or NULL on failure - so always check the result. calloc takes a count and a size, and zero-initialises the block. Neither runs a constructor, since C has none.

Storage classes and scope

Which storage class preserves a local variable's value between function calls? (a) auto (b) register (c) static (d) extern

(c) static. A static local is initialised once and retains its value across calls while remaining visible only inside the function - the standard way to write a call counter.

What does static mean when applied to a global variable or function?

It restricts linkage to the current translation unit, so the name is not visible to other source files. This is C's mechanism for private helpers - a completely different meaning from static on a local variable, which is what the MCQ is testing.

What does the extern keyword do?

It declares that a variable or function is defined elsewhere, giving the compiler its type without allocating storage. Header files declare with extern; exactly one source file provides the definition.

What is the scope and lifetime of a variable declared inside a for loop?

Block scope and automatic storage duration since C99 - it exists only within the loop and is destroyed on exit. In the original C89 rules such a declaration was not permitted inside the for statement at all.

Practice this now

Operators and undefined behaviour

Several famous 'guess the output' questions have no defined answer at all. Recognising them is the point - a question whose four options are all numbers may be testing whether you know that none of them is guaranteed.

What is the output of: int i = 5; printf("%d %d", i++, ++i); ?

Undefined behaviour. The variable is modified twice without an intervening sequence point, and the order in which function arguments are evaluated is unspecified. Different compilers print different results, and all of them are 'correct'. Never write this, and recognise it when it appears.

What do 5 & 3, 5 | 3 and 5 ^ 3 evaluate to?

1, 7 and 6. In binary 5 is 101 and 3 is 011: AND gives 001, OR gives 111, XOR gives 110. Also worth knowing: x << 1 doubles x, and x & 1 tests whether x is odd.

What is the result of the expression (5 > 3) in C? (a) true (b) 1 (c) TRUE (d) Compiler error

(b) 1. C's relational operators yield an int - 1 for true and 0 for false. There is no built-in boolean type in C89; _Bool and the stdbool.h macros arrived with C99.

In the expression a && b, is b always evaluated?

No. && and || short-circuit: if a is false, b is never evaluated. This is why idioms like if (p != NULL && p->x) are safe, and why putting a side effect in the right operand is a bug waiting to happen.

What does printf() return?

The number of characters written, or a negative value on error. Most code ignores it, and most students do not know it - which is exactly why it appears in MCQs.

Structures, unions and functions

What is the key difference between a struct and a union?

A struct allocates separate storage for every member, so its size is at least the sum of the members plus padding. A union overlays all members in the same storage, so its size is that of its largest member and only one member holds a valid value at a time.

Why can't a function swap two integers if they are passed by value?

Because C passes copies. The function modifies its own parameters, leaving the caller's variables untouched. Pass pointers - void swap(int *a, int *b) - so the function can write through to the originals.

What is the default return type of main() in modern C?

int. Implicit int was removed in C99, so int main(void) or int main(int argc, char *argv[]) are the standard forms. Returning 0 signals successful termination.

What does the -> operator do?

It accesses a member through a pointer to a structure: p->x is shorthand for (*p).x. The parentheses in the long form are required, because . binds more tightly than *.

What is recursion, and what does every recursive function need?

A function calling itself on a smaller input. It needs a base case that returns without recursing, and each call must move measurably toward it. Without that, the call stack is exhausted and the program crashes with a stack overflow.

Why C MCQs are worth the effort

C questions persist in placement tests long after most companies stopped writing C, and the reason is diagnostic: pointers, memory and undefined behaviour reveal whether a candidate understands what a program does to a machine or has only learnt a syntax. That is also why C MCQs transfer - the same understanding shows up in C++ questions, in operating-systems questions, and in interview conversations about memory and performance.

  • Trace every output question on paper, tracking each variable and each pointer.
  • When four numeric options are offered for an expression with two modifications of one variable, consider that the real answer may be 'undefined'.
  • Draw memory for pointer questions - boxes for variables, arrows for pointers. Guessing about pointers is not a strategy.
  • Learn the guaranteed facts separately from the typical ones: sizeof(char) is 1 by the standard; sizeof(int) is 4 only on your compiler.
  • Pair this with C++ MCQs, where the same pointer knowledge is tested alongside OOP and the STL.

The bottom line

Now go test yourself

C rewards precision about what the language actually guarantees. Sizes are implementation-defined except for char; arrays decay to pointers at function boundaries and lose their length; string literals are not modifiable; and a handful of famous expressions have no defined result at all. Once you hold those distinctions, most of the C question bank becomes routine.

Trace on paper, draw your pointers, then drill a timed C programming quiz on QUFF and read the explanation on every miss - especially the ones where your compiler's output and the standard disagree.

FAQs

Frequently asked questions

What topics are covered in C programming MCQs?

Data types and sizeof, operators and precedence, control flow, functions and recursion, pointers and arrays, strings, storage classes, structures and unions, dynamic memory, and undefined behaviour.

Why is the size of int not fixed in C?

Because the standard specifies minimum ranges rather than exact widths, leaving the size to the implementation and its target hardware. It is commonly 4 bytes today, but portable code uses the fixed-width types in stdint.h instead of assuming.

What is the difference between char *s = "abc" and char s[] = "abc"?

The pointer version points at a string literal in read-only storage, so modifying it is undefined behaviour. The array version creates a modifiable local copy of 4 characters including the terminator, so its elements can be changed.

What is undefined behaviour in C?

Code the standard places no requirements on - such as reading an uninitialised variable, dereferencing a freed pointer, or modifying a variable twice without an intervening sequence point. The compiler may do anything, so results vary and are all equally valid.

Why does i++ + ++i not have a defined answer?

Because i is modified twice within one expression with no sequence point between the modifications, which the standard leaves undefined. Different compilers produce different values and none is wrong - the correct MCQ answer is that the behaviour is undefined.

Is C still asked in placement tests?

Yes. C questions remain common in company written tests and university papers because pointers, memory and undefined behaviour reveal genuine understanding of how a program runs, and that understanding transfers to C++, operating systems and interviews.

What is the difference between malloc and calloc?

malloc(size) returns uninitialised memory; calloc(count, size) allocates for an array and zero-initialises it. Both return NULL on failure, which must always be checked before use.

Where can I practise C programming MCQs online?

QUFF has a free adaptive C programming quiz with no sign-up. It escalates as you score and explains each answer, which is particularly useful for the pointer and undefined-behaviour questions where intuition tends to mislead.

Related quizzes

Put it into practice

Keep reading

Related articles

Browse all articles →

Test yourself in two minutes

Six adaptive questions, every answer explained by an AI tutor. Free.

▶ Start an AI quiz