C Programming MCQs
Multiple-choice questions are common in placement tests, competitive exams and university internal tests. They check whether you know the exact rule, not just the general idea, so small details like integer division, operator precedence and string termination decide the answer.
Here are 30 C MCQs in six sets, covering the whole tutorial, with answers and short explanations after each set. Try a set, note your answers, then open the solutions. Aim for at least 25/30.
What You Will Learn
- 30 MCQs from every part of C.
- Short explanations for each answer.
- Two programs that prove the trickiest answers.
- Strategies to eliminate wrong options quickly.
- Which topics to revise based on your score.
C Programming MCQs Explained
Strategy: (1) Read the code or question twice. (2) Eliminate options that break a basic rule (e.g. arrays starting at 1). (3) Check types and precedence. (4) If two options look right, look for the hidden detail: '\0', integer division, missing break, = vs ==. (5) If the code modifies a variable twice in one expression, pick "undefined behavior".
Memory trick: Treat each MCQ like spot the difference: the four options differ in one small detail, and that detail is the rule being tested. Find it before you calculate.
Set 1: Basics and Data Types
Which of these is a valid identifier?
A) 2total B) total_2 C) total-2 D) int
The size of
charis always:A) 1 byte B) 2 bytes C) 4 bytes D) Compiler dependent
Which format specifier prints a double?
A) %d B) %c C) %lf or %f D) %s
Which is not a C keyword?
A) auto B) volatile C) main D) register
The range of an unsigned char is:
A) −128 to 127 B) 0 to 255 C) 0 to 127 D) −255 to 255
Show answers: Set 1
- B: identifiers can't start with a digit, contain '-', or be keywords.
- A:
sizeof(char)is 1 by definition of the C standard. - C: printf uses %f for double (%lf also works); scanf needs %lf for double.
- C: main is a function name, not a keyword.
- B: 8 bits without a sign gives 0 to 255.
Set 2: Operators and Expressions
Value of
10 + 4 * 2 - 6 / 3?A) 26 B) 16 C) 12 D) 10
Value of
5 > 3 && 2 > 4 || 1?A) 0 B) 1 C) 2 D) Error
Value of
17 % 5?A) 3 B) 2 C) 3.4 D) 12
Which operator has the highest precedence?
A) + B) = C) () D) &&
Value of
~0for a signed int (two's complement)?A) 0 B) 1 C) −1 D) 255
Show answers: Set 2
- B: 4*2 = 8 and 6/3 = 2 first: 10 + 8 − 2 = 16.
- B: && before ||: (1 && 0) || 1 = 0 || 1 = 1.
- B: 17 = 3 × 5 + 2.
- C: function call/brackets have the highest precedence.
- C: all bits become 1, which is −1 in two's complement.
Set 3: Control Flow and Loops
How many times does
for (i = 0; i < 10; i += 3)run?A) 3 B) 4 C) 10 D) 9
The switch expression cannot be of type:
A) int B) char C) float D) enum
Which statement skips the rest of the current iteration?
A) break B) exit C) continue D) return
while (0) printf("hi");prints "hi":A) Once B) Never C) Forever D) Error
Which loop is guaranteed to run at least once?
A) for B) while C) do-while D) None
Show answers: Set 3
- B: i = 0, 3, 6, 9 → 4 times.
- C: switch needs an integer type.
- C: continue.
- B: the condition is false from the start.
- C: do-while checks after the body.
Example 1: Proving Sets 2 and 3
#include <stdio.h>
int main(void)
{
int i, count = 0;
printf("10 + 4 * 2 - 6 / 3 = %d\n", 10 + 4 * 2 - 6 / 3);
printf("5 > 3 && 2 > 4 || 1 = %d\n", 5 > 3 && 2 > 4 || 1);
printf("~0 = %d\n", ~0);
for (i = 0; i < 10; i += 3)
count++;
printf("loop ran %d times\n", count);
return 0;
}
Output
10 + 4 * 2 - 6 / 3 = 16 5 > 3 && 2 > 4 || 1 = 1 ~0 = -1 loop ran 4 times
Code Explanation
- The first line confirms that * and / are evaluated before + and −.
- The second line confirms that && is evaluated before ||.
~0flips all bits of 0, giving −1 on two's complement machines (all common computers).- The counter proves the loop runs for i = 0, 3, 6 and 9.
Set 4: Arrays, Strings and Functions
For
int a[5];, the valid indexes are:A) 1 to 5 B) 0 to 5 C) 0 to 4 D) 1 to 4
sizeof("Hello")is:A) 5 B) 6 C) 4 D) 8
strcmp("apple", "banana")returns:A) 0 B) A positive value C) A negative value D) 1 always
By default, arguments in C are passed:
A) By reference B) By value C) By name D) By pointer always
A function that calls itself is:
A) Inline B) Recursive C) Static D) Variadic
Show answers: Set 4
- C: indexes start at 0, so the last is 4.
- B: 5 letters plus the hidden '\0'.
- C: "apple" comes before "banana" alphabetically.
- B: C always passes copies; passing a pointer copies the address.
- B: recursive.
Set 5: Pointers, Structures and Memory
If
int x = 5, *p = &x;, what is*p + 1?A) Address + 1 B) 6 C) 5 D) Error
Which operator accesses a member through a structure pointer?
A) . B) -> C) :: D) &
Which function allocates memory initialised to zero?
A) malloc B) calloc C) realloc D) alloc
For
union u { int i; char c[8]; };(4-byte int),sizeof(union u)is:A) 12 B) 8 C) 4 D) 1
Using memory after freeing it creates a:
A) NULL pointer B) Void pointer C) Dangling pointer D) Function pointer
Show answers: Set 5
- B: *p is 5, plus 1 is 6.
- B: ->.
- B: calloc.
- B: a union is as big as its largest member (8 bytes here).
- C: dangling pointer.
Set 6: Files, Preprocessor and Advanced
Which mode opens a file for reading and writing without erasing it?
A) "w+" B) "r+" C) "a" D) "w"
fopenreturns what when it fails?A) 0 bytes B) NULL C) EOF D) −1
Macros are expanded by the:
A) Compiler B) Linker C) Preprocessor D) Loader
In
int main(int argc, char *argv[]),argv[0]is:A) The first argument B) The program name C) NULL D) argc
Which header declares
va_list?A) stdlib.h B) stdarg.h C) stdio.h D) varargs.h only
Show answers: Set 6
- B: "r+" keeps the content; "w+" erases it.
- B: NULL.
- C: the preprocessor.
- B: the program name.
- B: stdarg.h.
Example 2: Proving Sets 4 and 5
#include <stdio.h>
#include <string.h>
union u
{
int i;
char c[8];
};
int main(void)
{
int x = 5, *p = &x;
printf("sizeof(\"Hello\") = %zu\n", sizeof("Hello"));
printf("strcmp(\"apple\", \"banana\") is %s\n",
strcmp("apple", "banana") < 0 ? "negative" : "not negative");
printf("*p + 1 = %d\n", *p + 1);
printf("sizeof(union u) = %zu\n", sizeof(union u));
return 0;
}
Output
sizeof("Hello") = 6
strcmp("apple", "banana") is negative
*p + 1 = 6
sizeof(union u) = 8
- A string literal's size includes the terminating
'\0'. - strcmp's exact negative value can differ between systems; only its sign matters.
- The union's size equals its largest member, the 8-byte array.
Important Points to Remember
- Precedence: () > unary > * / % > + − > relational > && > || > =.
- Strings take one extra byte for '\0'.
- Arrays index from 0 to n − 1.
- C passes arguments by value only.
- union size = largest member; struct size ≥ sum of members.
- Scoring: 26–30 excellent; 20–25 revise the weak set; below 20 revise the full module.
Common Mistakes Beginners Make
| Trap | Common wrong answer | Right answer |
|---|---|---|
sizeof("Hi") | 2 | 3 |
Last index of int a[10] | 10 | 9 |
| Is main a keyword? | Yes | No |
| "w+" keeps old content? | Yes | No, it erases the file |
| C supports call by reference directly? | Yes | No; it simulates it with pointers (passed by value) |
MCQ Practice Questions
- What is
sizeof(int) * 2on a system with 4-byte int? - What does
printf("%d", 'A' + 1);print? - How many times does
for (i = 5; i > 0; i -= 2)run? - What is
strlen("C\0Program")? - What is
(int) 3.99?
Show answers
- 8.
- 66.
- 3 times (i = 5, 3, 1).
- 1: strlen stops at the first '\0'.
- 3: casting to int truncates toward zero.
Coding Exercise
Write a tiny MCQ checker: given the answer key "BCABD" and a student's answers "BCDBD", print each question's result and the score out of 5.
Show solution
#include <stdio.h>
int main(void)
{
const char key[] = "BCABD", given[] = "BCDBD";
int i, score = 0;
for (i = 0; key[i] != '\0'; i++)
{
int ok = key[i] == given[i];
score += ok;
printf("Q%d: %c %s\n", i + 1, given[i], ok ? "correct" : "wrong");
}
printf("Score: %d/5\n", score);
return 0;
}
Q1: B correct Q2: C correct Q3: D wrong Q4: B correct Q5: D correct Score: 4/5
Quick Quiz: Mixed C MCQs
-
printf("%d", 3 + 5 / 2);prints:A) 4 B) 5 C) 5.5 D) 4.0
-
Which is the correct way to declare a pointer to int?
A)
int p*;B)int *p;C)*int p;D)pointer int p; -
Which header declares
malloc?A) malloc.h only B) stdlib.h C) memory.h D) stdio.h
-
A structure variable
saccesses memberageas:A) s->age B) s.age C) s:age D) s[age]
-
EOFis defined in:A) stdio.h B) stdlib.h C) string.h D) ctype.h
Show answers
- B: 5 / 2 = 2, so 3 + 2 = 5.
- B:
int *p; - B: stdlib.h.
- B: s.age.
- A: stdio.h.
Quick Revision for Exams and Interviews
Read this before your test. The highlighted words are the answer.
Must-remember facts
| sizeof(char) | Always 1 |
|---|---|
| Array indexes | 0 to n − 1 |
| String size | Length + 1 ('\0') |
| && vs || | && first |
| switch type | Integer only |
| union size | Largest member |
| "r+" vs "w+" | Keeps vs erases |
Frequently asked questions
- How do I prepare for C MCQ tests?
- Revise exact rules (precedence, sizes, '\0', integer division) and practise timed sets of MCQs.
- Is main a keyword in C?
- No. main is a function name; keywords are words like int, return and while.
- Does C support call by reference?
- Not directly: C passes everything by value, but passing a pointer lets a function change the original.
Remember
- Each MCQ is spot the difference: find the one detail.
- Below 20/30? Revise that set's module.
Next Topic
Next lesson: C Cheat Sheet. Finish with a one-page cheat sheet of the whole C language for quick revision.