Search quizzes/
Program code on a laptop screen
Photo: luis gomes, CC0, via Wikimedia Commons

C Language MCQ Questions with Answers

20 C Language MCQs covering the key concepts, each with the correct answer and a one-line explanation. Take the timed test, see your score instantly, then review every answer.

20 questionsMedium–Hard~4 minNo timer

Updated:

If you liked this

Trending today

About this C Language MCQ quiz

This C MCQ test has 20 questions on the C language, written in the style of university exams and placement tests. It covers the basics (loops, arrays, strings, sizeof, format specifiers), memory and storage (malloc, static, global initialisation), structures and pointers, and a run of output questions on post-increment, integer division, assignment in conditions, pointer arithmetic, bitwise operators, short-circuit evaluation and macros.

Use these C language MCQ questions to revise before a semester exam, a campus placement round or a coding interview. Every answer follows standard C99 behaviour and comes with a one-line explanation, so you learn why the output is what it is. Code snippets are short enough to trace in your head, and the full answer key is printed below.

One thing to take away: integer division in C throws the fraction away before anything else happens. float c = 5 / 2; stores 2.0, not 2.5, because the division is done in int first. Cast one operand to float to keep the fraction.

C Language MCQ questions with answers

Show answers
  1. Who created the C programming language at Bell Labs in the early 1970s?

    • Dennis Ritchie
    • Bjarne Stroustrup
    • James Gosling
    • Ken Thompson

    Answer: Dennis Ritchie. Dennis Ritchie developed C around 1972 from Ken Thompson's B language, to write Unix. Stroustrup later built C++ on top of C, and Gosling created Java.

  2. Which loop always runs its body at least once?

    • for
    • while
    • do-while
    • None of them

    Answer: do-while. A do-while loop tests its condition after the body, so the body runs once even if the condition is false at the start. for and while test first.

  3. An array is declared as int arr[5];. Which indices are valid?

    • 1 to 5
    • 0 to 4
    • 0 to 5
    • 1 to 4

    Answer: 0 to 4. C arrays start at index 0, so 5 elements are arr[0] to arr[4]. Reading arr[5] goes past the end, which is undefined behaviour.

  4. Which character marks the end of a string in C?

    • '\n'
    • '.'
    • EOF
    • '\0'

    Answer: '\0'. A C string is a char array that ends with the null character '\0'. Functions such as strlen and printf stop when they reach it.

  5. What does sizeof(char) always evaluate to in C?

    • 1
    • 2
    • 4
    • It depends on the compiler

    Answer: 1. The C standard defines sizeof(char) as exactly 1. Other types, such as int and long, vary between systems.

  6. Which printf format specifier prints an unsigned int in hexadecimal?

    • %o
    • %x
    • %d
    • %e

    Answer: %x. %x prints base 16 with lowercase letters, and %X uses capitals. %o is octal, and %e prints a floating-point number in scientific notation.

  7. What does malloc() return if it cannot allocate the requested memory?

    • A block of 0 bytes
    • A random address
    • NULL
    • -1

    Answer: NULL. malloc returns a null pointer when it fails, so always check the result before using it. Memory from malloc is released with free().

  8. Which operator accesses a structure member through a pointer to the structure?

    • .
    • ->
    • &
    • *

    Answer: ->. p->x is shorthand for (*p).x. The dot operator is used on a structure variable itself, not on a pointer to it.

  9. Which storage class lets a local variable keep its value between calls to the same function?

    • auto
    • register
    • extern
    • static

    Answer: static. A static local variable is initialised once and lives for the whole run of the program, so it remembers its value between calls. An auto variable is created fresh each time.

  10. A global int variable is declared but not initialised. What is its initial value?

    • 0
    • A garbage value
    • -1
    • The program will not compile

    Answer: 0. Objects with static storage duration, which includes globals, are set to zero before main starts. Uninitialised local (auto) variables are the ones with indeterminate, garbage values.

  11. What does this print? int i = 5; printf("%d", i++);

    • 6
    • 5
    • 4
    • It will not compile

    Answer: 5. i++ is post-increment: the expression gives the old value, 5, and i becomes 6 afterwards. ++i would print 6.

  12. What does this print? int a = 5, b = 2; float c = a / b; printf("%.1f", c);

    • 2.5
    • 3.0
    • 2.0
    • 2

    Answer: 2.0. a / b divides two ints, so the result is 2 and the fraction is dropped before the value reaches c. Writing (float)a / b gives 2.5.

  13. What does this print? int x = 0; if (x = 5) printf("yes"); else printf("no");

    • no
    • Compile error
    • Nothing
    • yes

    Answer: yes. x = 5 is an assignment, not a comparison. Its value is 5, which is non-zero, so the condition is true. Compilers often warn here, because x == 5 was probably meant.

  14. What does this print? int a[] = {10, 20, 30}; int *p = a; printf("%d", *(p + 2));

    • 12
    • 20
    • 30
    • An address

    Answer: 30. p + 2 moves forward two ints, to a[2], and * reads its value: 30. Pointer arithmetic counts in elements, not bytes.

  15. What does printf("%d", 5 & 3); print?

    • 1
    • 7
    • 6
    • 8

    Answer: 1. & is bitwise AND: 101 & 011 = 001, which is 1. 5 | 3 would give 7, and 5 ^ 3 would give 6.

  16. What does printf("%zu", sizeof("hello")); print?

    • 5
    • 8
    • 6
    • 4

    Answer: 6. The string literal "hello" is an array of 6 chars: five letters plus the closing '\0'. strlen("hello") would give 5.

  17. What is the value of b after this code runs? int a = 0, b = 5; if (a && ++b) { }

    • 6
    • 0
    • 1
    • 5

    Answer: 5. && stops as soon as its left side is false. a is 0, so ++b is never evaluated and b stays 5. This is short-circuit evaluation.

  18. In C99, what is the value of the expression -7 / 2?

    • -4
    • -3.5
    • 3
    • -3

    Answer: -3. C99 integer division truncates toward zero, so -3.5 becomes -3, and -7 % 2 is -1. Under C89 the rounding direction for negative operands was implementation-defined, so -4 was allowed.

  19. What does this print? #define SQ(x) x * x printf("%d", SQ(2 + 3));

    • 25
    • 11
    • 13
    • 10

    Answer: 11. A macro pastes text, so SQ(2 + 3) becomes 2 + 3 * 2 + 3, which is 11. Defining it as ((x) * (x)) gives the expected 25.

  20. In C99 (hosted), if int main(void) reaches its closing brace without a return statement, what status does the program return?

    • 0
    • 1
    • A garbage value
    • -1

    Answer: 0. C99 says reaching the } that ends main returns 0, which means success. In C89 the status was undefined, which is why older code always ends with return 0;.

Frequently asked questions

Where can I find C Language questions with answers?

Right here. Take the test to see each answer and its explanation as you go, or scroll to the answer key to read all 20 C MCQ questions and answers at once.

How many questions are in this C Language quiz?

There are 20 multiple-choice questions. They run from the basics (who created C, do-while loops) to output questions on macros, short-circuit evaluation and C99 integer division, and most people finish in about 15 minutes.

Can I play the C Language quiz online for free without signing up?

Yes. The test is free and needs no account or login. You see your score at the end, can review every answer, and can retake it as often as you like.