CC

C quiz – 45 questions with answers

Try it interactively

With scoring, random questions, and saved progress – or go straight to the timed exam with certificate.

Beginner (15 questions)

Where does the execution of a C program begin?

  1. In the first function in the file
  2. In the function main
  3. At the first #include line
  4. In the function start
Show answer

Correct: In the function main

The entry point of every C program is main – no matter where in the source code it appears.

Which command compiles hallo.c into the executable file hallo?

  1. gcc run hallo.c
  2. c hallo.c --exe
  3. gcc -o hallo hallo.c
  4. compile hallo.c hallo
Show answer

Correct: gcc -o hallo hallo.c

gcc -o hallo hallo.c translates and links the file; -o sets the name of the output file.

What does this code print?

printf("%d\n", 7 / 2);
  1. 3.5
  2. 4
  3. 3
  4. 2
Show answer

Correct: 3

With two int operands, / is an integer division: 7 / 2 = 3, the remainder is dropped.

What does this code print?

double d = 7 / 2;
printf("%.1f\n", d);
  1. 3.5
  2. 3.0
  3. 4.0
  4. 3
Show answer

Correct: 3.0

The division 7 / 2 is evaluated as an integer division first (3) and only then converted to double – so %.1f shows 3.0. For 3.5, one operand must be a double (7 / 2.0).

What does this code print?

printf("%d %d\n", -7 / 2, -7 % 2);
  1. -4 1
  2. -3 1
  3. -4 -1
  4. -3 -1
Show answer

Correct: -3 -1

Since C99, integer division rounds toward 0: -7 / 2 = -3. The remainder has the sign of the dividend: -7 % 2 = -1 (because -3 * 2 + (-1) = -7).

What does this code print?

char s[] = "abc";
printf("%zu %zu\n", sizeof(s), strlen(s));
  1. 3 3
  2. 4 3
  3. 3 4
  4. 4 4
Show answer

Correct: 4 3

The array contains four bytes (a, b, c and the null byte), while strlen only counts the three characters before the null byte.

What does every C string end with in memory?

  1. With a newline \n
  2. With the null byte \0
  3. With a space
  4. With the character $
Show answer

Correct: With the null byte \0

C strings are null-terminated: the byte with the value 0 marks the end. If you forget it, string functions run past the end of the memory.

What does this code print?

int x = 5;
if (x = 0) printf("A");
else printf("B");
  1. A
  2. B
  3. AB
  4. Compile error
Show answer

Correct: B

x = 0 is an assignment that yields the value 0 – and 0 is false. So B is printed. With -Wall, the compiler warns about this classic typo.

What does this code print?

int n = 2;
switch (n) {
    case 1: printf("A");
    case 2: printf("B");
    case 3: printf("C"); break;
    default: printf("D");
}
  1. B
  2. C
  3. BC
  4. BCD
Show answer

Correct: BC

Without break, execution continues into the next case (fallthrough): B is followed by C, then the break stops it.

How do you compute the number of elements of a real array int a[10]?

  1. len(a)
  2. sizeof(a)
  3. a.length
  4. sizeof(a) / sizeof(a[0])
Show answer

Correct: sizeof(a) / sizeof(a[0])

sizeof(a) returns the total size in bytes; divided by the size of one element, this gives the count. This only works as long as a really is an array – not for function parameters.

Which format specifier in printf belongs to a double?

  1. %d
  2. %c
  3. %f
  4. %s
Show answer

Correct: %f

%f prints double (%.2f with two decimal places). %d is for int, %c for a character, %s for strings.

What does this code print?

for (int i = 0; i < 5; i++) {
    if (i == 2) continue;
    if (i == 4) break;
    printf("%d ", i);
}
  1. 0 1 3
  2. 0 1 2 3
  3. 0 1 3 4
  4. 0 1
Show answer

Correct: 0 1 3

At i == 2, continue jumps to the next iteration; at i == 4, break ends the loop. 0, 1 and 3 are printed.

What index does the last element of int a[10] have?

  1. 10
  2. 11
  3. 9
  4. -1
Show answer

Correct: 9

Indices run from 0 to n-1. The access a[10] already lies beyond the array (undefined behavior). Negative indices as in Python do not exist.

What does this code print?

char c = 'A';
printf("%d %c\n", c + 1, c + 1);
  1. 66 B
  2. B 66
  3. 65 A
  4. 66 66
Show answer

Correct: 66 B

char is a numeric type: 'A' has the ASCII value 65. c + 1 is 66 – as a number with %d, as the character B with %c.

What does this program print?

void increment(int x) { x++; }

int main(void) {
    int n = 5;
    increment(n);
    printf("%d\n", n);
}
  1. 6
  2. 5
  3. 0
  4. Compile error
Show answer

Correct: 5

C passes arguments as a copy (call by value). increment only changes its own copy of n – the original stays 5. To change it, you need a pointer.

Intermediate (15 questions)

What does this code print?

int x = 10;
int *p = &x;
*p += 5;
printf("%d\n", x);
  1. 10
  2. 15
  3. 5
  4. The address of x
Show answer

Correct: 15

p points to x; *p += 5 changes the value at that address, i.e. x itself: 15.

What does this code print?

int a[] = {10, 20, 30};
int *p = a;
printf("%d %d\n", *(p + 1), p[2]);
  1. 10 20
  2. 20 30
  3. 20 20
  4. 30 30
Show answer

Correct: 20 30

*(p + 1) is the element with index 1 (20), p[2] is by definition *(p + 2) (30). Pointer arithmetic counts in elements, not in bytes.

What does f print on a 64-bit system when main calls it with int a[10] (f(a))?

void f(int arr[]) {
    printf("%zu\n", sizeof(arr));
}
  1. 40
  2. 10
  3. 8
  4. 4
Show answer

Correct: 8

An array parameter is really a pointer (int *arr). sizeof(arr) therefore returns the pointer size (8 bytes on 64-bit), not the 40 bytes of the array. That is why you always have to pass the length along.

What is the problem here?

char *s = "abc";
s[0] = 'x';
  1. Nothing, the string then contains "xbc"
  2. String literals are read-only: undefined behavior (usually a crash)
  3. s has to be created with malloc first
  4. Characters may never be overwritten individually
Show answer

Correct: String literals are read-only: undefined behavior (usually a crash)

The pointer points to a string literal, which usually lives in read-only memory. Writing to it is undefined behavior. Better use const char *s or an array char s[] = "abc";.

What is the problem here?

char ziel[5];
strcpy(ziel, "Hello World");
  1. Nothing – strcpy truncates automatically
  2. Buffer overflow: 12 bytes do not fit into 5
  3. strcpy only works with malloc memory
  4. You would have to use strcat instead of strcpy
Show answer

Correct: Buffer overflow: 12 bytes do not fit into 5

strcpy does not check the destination size. "Hello World" needs 12 bytes (including the null byte), the array has 5 – the rest overwrites foreign memory. Safe: snprintf(ziel, sizeof ziel, "%s", quelle).

What does malloc return when no memory can be allocated?

  1. The last valid pointer
  2. An exception
  3. The int value -1
  4. NULL
Show answer

Correct: NULL

malloc returns NULL on failure. This check is mandatory – otherwise the next access leads to a crash.

What is risky about this code?

int *p = malloc(4 * sizeof *p);
p = realloc(p, 8 * sizeof *p);
  1. realloc can only shrink
  2. If realloc fails, p becomes NULL and the old block is lost as a memory leak
  3. realloc fills everything with 0 and is therefore slow
  4. Nothing, this is the standard way
Show answer

Correct: If realloc fails, p becomes NULL and the old block is lost as a memory leak

On failure, realloc returns NULL, but the old block stays valid. If you overwrite p directly, the address is lost. Correct: store it in tmp first, check, then p = tmp.

What happens here?

int *p = malloc(sizeof *p);
free(p);
free(p);
  1. Nothing, free is idempotent
  2. Undefined behavior (often a crash or heap corruption)
  3. p is automatically set to NULL
  4. The compiler refuses to compile
Show answer

Correct: Undefined behavior (often a crash or heap corruption)

A block may only be freed once. A double free is undefined behavior and can be exploitable. After free(p), p = NULL; helps, because free(NULL) is harmless.

What does this code print?

struct P { int x; int y; };
struct P a = {1, 2};
struct P b = a;
b.x = 99;
printf("%d %d\n", a.x, b.x);
  1. 99 99
  2. 1 1
  3. 1 99
  4. 99 1
Show answer

Correct: 1 99

Assignment copies a struct field by field. b is an independent copy – a.x stays 1, b.x becomes 99.

What does p->x stand for when p is a pointer to a struct?

  1. *(p.x)
  2. &p.x
  3. (*p).x
  4. p * x
Show answer

Correct: (*p).x

p->x is shorthand for (*p).x: first dereference, then select the field.

What does this code print?

char s[] = "Hello";
s[2] = '\0';
printf("%s %zu\n", s, strlen(s));
  1. Hello 5
  2. Hel 3
  3. He 5
  4. He 2
Show answer

Correct: He 2

The null byte at index 2 ends the string: printf and strlen only see "He" (2 characters). The remaining bytes stay in the array but no longer belong to the string.

Which function was removed from the C11 standard because of its inherent buffer overflow danger?

  1. scanf
  2. gets
  3. fgets
  4. puts
Show answer

Correct: gets

gets cannot limit the length of the input and is therefore fundamentally unsafe. Replacement: fgets(puffer, sizeof puffer, stdin).

How do you check whether two strings a and b are equal?

  1. a == b
  2. a.equals(b)
  3. strcmp(a, b) == 1
  4. strcmp(a, b) == 0
Show answer

Correct: strcmp(a, b) == 0

a == b only compares the addresses. strcmp returns 0 for equality, and a negative or positive value for less or greater (not necessarily -1 or 1).

What does this code print?

#define QUAD(x) x * x
printf("%d\n", QUAD(2 + 1));
  1. 9
  2. 5
  3. 6
  4. 7
Show answer

Correct: 5

The preprocessor only replaces text: 2 + 1 * 2 + 1 = 2 + 2 + 1 = 5. With #define QUAD(x) ((x) * (x)), the result would be 9.

What does this code print?

enum Farbe { ROT, GRUEN = 5, BLAU };
printf("%d %d\n", ROT, BLAU);
  1. 0 2
  2. 0 6
  3. 0 5
  4. 1 6
Show answer

Correct: 0 6

Without a value, an enum counts up from 0; with a value, counting continues from there: ROT = 0, GRUEN = 5, BLAU = 6.

Advanced (15 questions)

What happens with int i = INT_MAX; i++;?

  1. i is guaranteed to become INT_MIN
  2. Undefined behavior: overflow of signed integers
  3. The compiler always reports an error
  4. i stays INT_MAX
Show answer

Correct: Undefined behavior: overflow of signed integers

Overflow with signed types is undefined behavior. The compiler may assume it never occurs and optimize code away. With unsigned, on the other hand, overflow is well-defined (wrap-around).

What does this code print?

unsigned char c = 255;
c++;
printf("%d\n", c);
  1. 256
  2. -1
  3. 255
  4. 0
Show answer

Correct: 0

Wrap-around is defined for unsigned types: 255 + 1 does not fit into 8 bits, so the result is computed modulo 256: 0.

What does this code print?

printf("%d %d %d\n", 6 & 3, 6 | 3, 6 ^ 3);
  1. 2 7 4
  2. 3 7 5
  3. 2 7 5
  4. 2 6 5
Show answer

Correct: 2 7 5

6 = 110, 3 = 011: AND = 010 (2), OR = 111 (7), XOR = 101 (5).

How large is struct S typically (int = 4 bytes, alignment 4)?

struct S { char a; int b; char c; };
printf("%zu\n", sizeof(struct S));
  1. 6
  2. 8
  3. 9
  4. 12
Show answer

Correct: 12

a (1 byte) + 3 bytes of padding so that b sits at an address divisible by 4, then b (4), c (1) + 3 bytes of padding at the end: 12. If you sort the fields (int b; char a; char c;), it is only 8.

What does int (*f)(int, int); declare?

  1. A function that returns an int pointer
  2. An array of two pointers
  3. A pointer f to a function with two int parameters and an int return value
  4. A function call
Show answer

Correct: A pointer f to a function with two int parameters and an int return value

The parentheses around *f make f a pointer; only then does the parameter list follow. Without parentheses (int *f(int, int)), it would be a function returning an int *.

What does this code print?

int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

int (*ops[])(int, int) = {add, mul};
printf("%d %d\n", ops[0](3, 4), ops[1](3, 4));
  1. 12 7
  2. 7 12
  3. 3 4
  4. 7 7
Show answer

Correct: 7 12

ops is an array of function pointers: ops[0] is add (3 + 4 = 7), ops[1] is mul (3 * 4 = 12).

What is the problem with this code?

char *s = malloc(6);
strcpy(s, "Hello");
free(s);
printf("%s\n", s);
  1. Nothing, the memory stays valid
  2. Use-after-free: accessing freed memory is undefined behavior
  3. strcpy must not be used with malloc memory
  4. printf needs a null byte after the string
Show answer

Correct: Use-after-free: accessing freed memory is undefined behavior

After free, the memory belongs to the allocator. Reading or writing is undefined behavior – often it "works" by chance, until it leads to data loss or a security vulnerability.

What does const int *p mean?

  1. p itself is constant and cannot be redirected
  2. The value p points to must not be changed via p
  3. Both are constant
  4. p must point to a macro
Show answer

Correct: The value p points to must not be changed via p

const int *p = pointer to a constant int: *p = 1 is forbidden, p = &y is allowed. For a constant pointer, you write int *const p.

What does undefined behavior (UB) mean in C?

  1. The compiler aborts the translation
  2. The program returns a random but stable value
  3. The standard places no requirements – anything is allowed, including seeming to work
  4. The program is safely terminated with an error message
Show answer

Correct: The standard places no requirements – anything is allowed, including seeming to work

With UB, anything may happen: correct results, a crash, data corruption or security vulnerabilities. Compilers optimize under the assumption that UB never occurs – which is why bugs may only become visible with -O2.

What is the problem with this code?

char eingabe[100];
fgets(eingabe, sizeof eingabe, stdin);
printf(eingabe);
  1. Nothing
  2. Format string vulnerability: user input containing %s or %n is interpreted as a format; the correct form is printf("%s", eingabe)
  3. fgets reads too few characters
  4. printf does not print a newline
Show answer

Correct: Format string vulnerability: user input containing %s or %n is interpreted as a format; the correct form is printf("%s", eingabe)

If the user types %x %x %s, printf prints stack contents or crashes, and %n can even write to memory. The format string must always be fixed text.

What is volatile meant for?

  1. The variable is guaranteed to live in a register
  2. The compiler must not optimize accesses away because the value can change outside the program flow (hardware, signal handlers)
  3. The variable is thread-safe
  4. The variable is automatically initialized to 0
Show answer

Correct: The compiler must not optimize accesses away because the value can change outside the program flow (hardware, signal handlers)

volatile forces every access to actually take place (e.g. with hardware registers). It does not guarantee atomicity and does not replace synchronization between threads – <stdatomic.h> exists for that.

What does this code print?

int a[4] = {1, 2, 3, 4};
int *p = a;
int *q = &a[3];
printf("%td\n", q - p);
  1. 12
  2. 3
  3. 4
  4. 1
Show answer

Correct: 3

Subtracting two pointers into the same array yields the distance in elements, not in bytes: index 3 minus index 0 = 3.

What is the problem with this code?

int i = 1;
int r = i++ + i++;
  1. Nothing, r is 2
  2. Two modifications of i without a sequence point in between: undefined behavior
  3. r is always 3
  4. i++ is only allowed in for loops
Show answer

Correct: Two modifications of i without a sequence point in between: undefined behavior

The standard does not specify the evaluation order of operands. If i is modified several times in one expression, the result is undefined. An expression should modify a variable at most once.

Why is calloc(n, size) safer than malloc(n * size) with regard to overflow?

  1. calloc is always faster
  2. calloc receives both factors separately and can check the product for overflow (NULL instead of a block that is too small)
  3. calloc allocates the memory on the stack
  4. calloc frees the memory automatically
Show answer

Correct: calloc receives both factors separately and can check the product for overflow (NULL instead of a block that is too small)

With malloc(n * size), the multiplication can overflow and yield a much too small block, into which too much data is then written. Good calloc implementations detect this and return NULL.

When do you have to use memmove instead of memcpy?

  1. When source and destination can overlap
  2. When the data are strings
  3. When more than 1 KB is copied
  4. When the destination memory is on the heap
Show answer

Correct: When source and destination can overlap

memcpy requires separate memory regions; with overlap, the behavior is undefined. memmove detects the overlap and copies correctly, e.g. when shifting elements in an array.