Skip to content
Laviyx Learn

Try

C course menu

Beginner C Interview Questions and Answers

Common C interview questions for freshers with clear answers and runnable examples: features of C, compiling and linking, data types and format specifiers, = vs ==, integer division, break vs continue, while vs do-while, local vs global variables, arrays, strings, and pass by value.

Beginner12 min readUpdated
On this page

These questions come up in nearly every entry-level C interview and in college viva exams. For each one, try answering out loud first, then open the solution. A short, precise answer with a tiny example beats a memorised paragraph.

Concepts#

1. What are the main features of C?#

Show solution
  • Compiled to fast native machine code.
  • Procedural: programs are built from functions.
  • Low-level access: pointers, direct memory management and bitwise operations, which is why operating systems, embedded firmware and databases are written in C.
  • Small and portable: a small language with a standard library, and compilers for almost every processor.
  • Manual memory management with malloc and free, with no garbage collector.

2. What happens when you compile a C program?#

Show solution

Four stages, which gcc main.c -o main runs together:

  1. Preprocessing: handles #include, #define and #if (gcc -E).
  2. Compilation: translates C to assembly (gcc -S).
  3. Assembly: turns assembly into an object file of machine code (gcc -c).
  4. Linking: joins object files and libraries into an executable, resolving calls such as printf.

A missing semicolon is a compiler error; a declared but never defined function is a linker error ("undefined reference").

3. What is the difference between = and ==?#

Show solution

= assigns a value; == compares two values. Writing if (x = 5) assigns 5 to x and the condition is always true. GCC warns about this with -Wall:

C
int x = 3;
if (x == 5) printf("five\n");
else printf("not five\n");
Output
not five

4. What does 7 / 2 give in C, and how do you get 3.5?#

Show solution

Dividing two integers gives an integer: the fraction is discarded (truncated towards zero). Make one operand a floating-point value:

C
int a = 7, b = 2;
printf("%d\n", a / b);
printf("%d\n", -7 / 2);
printf("%d\n", a % b);
printf("%.1f\n", (double)a / b);
Output
3
-3
1
3.5

5. What are the basic data types and their format specifiers?#

Show solution
TypeTypical sizeprintfscanf
char1 byte%c%c
int4 bytes%d%d
long long8 bytes%lld%lld
float4 bytes%f%f
double8 bytes%f%lf
size_t8 bytes (64-bit)%zu%zu
char * (string)%s%s

Sizes are platform-dependent; the standard only guarantees minimums (for example, int is at least 16 bits). Use sizeof to check.

6. What is the difference between break and continue?#

Show solution

break leaves the loop (or switch) completely; continue skips the rest of the current iteration and goes on to the next:

C
for (int i = 1; i <= 6; i++) {
    if (i == 3) continue;
    if (i == 5) break;
    printf("%d ", i);
}
printf("\n");
Output
1 2 4 

7. What is the difference between while and do-while?#

Show solution

while checks the condition first and may run zero times. do-while checks after the body, so it always runs at least once:

C
int n = 10;
while (n < 5) printf("while ran\n");
do { printf("do-while ran\n"); } while (n < 5);
Output
do-while ran

8. What is the difference between local, global and static variables?#

Show solution
  • Local: declared in a block, visible only there, created on each entry, garbage if not initialised.
  • Global: declared outside functions, visible to the rest of the file (and others via extern), lives for the whole program, starts at 0.
  • Static local: visible only in its block but keeps its value between calls, starts at 0.
C
#include <stdio.h>

int calls_total;                    /* global */

void f(void) {
    int local = 0;
    static int kept = 0;
    local++;
    kept++;
    calls_total++;
    printf("%d %d %d\n", local, kept, calls_total);
}

int main(void) {
    f();
    f();
    return 0;
}
Output
1 1 1
1 2 2

9. How are arguments passed to functions in C?#

Show solution

Always by value: the function receives a copy. To change a caller's variable, pass its address (a pointer):

C
#include <stdio.h>

void by_value(int x) { x = 99; }
void by_pointer(int *x) { *x = 99; }

int main(void) {
    int a = 1, b = 1;
    by_value(a);
    by_pointer(&b);
    printf("%d %d\n", a, b);
    return 0;
}
Output
1 99

Arrays seem to be passed "by reference" because the array name decays to a pointer to its first element.

10. How is a string stored in C?#

Show solution

As an array of char ending with the null character '\0'. "cat" needs 4 bytes. strlen counts the characters before '\0'; sizeof an array gives its full size:

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char s[10] = "cat";
    printf("%zu %zu\n", strlen(s), sizeof s);
    return 0;
}
Output
3 10

11. Why can't you compare strings with ==?#

Show solution

== on two strings compares their addresses, not their characters. Use strcmp, which returns 0 when the contents are equal:

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char a[] = "hello";
    char b[] = "hello";
    printf("%d %d\n", a == b, strcmp(a, b) == 0);
    return 0;
}
Output
0 1

GCC even warns comparison between two arrays on a == b.

12. What is the difference between a header file and a source file?#

Show solution

A header (.h) holds declarations shared between files: function prototypes, types, macros. A source file (.c) holds definitions: function bodies and variables. Source files #include headers, and each source file is compiled separately, then linked. See Multi-file Programs.

Short coding questions#

13. Swap two numbers#

Show solution

The clear way uses a temporary variable and pointers:

C
#include <stdio.h>

void swap(int *a, int *b) {
    int t = *a;
    *a = *b;
    *b = t;
}

int main(void) {
    int x = 4, y = 9;
    swap(&x, &y);
    printf("%d %d\n", x, y);
    return 0;
}
Output
9 4

Interviewers sometimes ask for a swap "without a temporary" (a ^= b; b ^= a; a ^= b;). It works for distinct variables but breaks if both pointers point to the same variable, and it is not faster.

14. Check whether a number is prime#

Show solution

Only divisors up to √n need checking:

C
#include <stdio.h>

int is_prime(int n) {
    if (n < 2) return 0;
    for (int d = 2; d * d <= n; d++) {
        if (n % d == 0) return 0;
    }
    return 1;
}

int main(void) {
    for (int n = 1; n <= 30; n++) {
        if (is_prime(n)) printf("%d ", n);
    }
    printf("\n");
    return 0;
}
Output
2 3 5 7 11 13 17 19 23 29 

15. Reverse a number and check for a palindrome#

Show solution
C
#include <stdio.h>

int reverse(int n) {
    int r = 0;
    while (n > 0) {
        r = r * 10 + n % 10;
        n /= 10;
    }
    return r;
}

int main(void) {
    int nums[] = {12321, 4567};
    for (int i = 0; i < 2; i++) {
        int r = reverse(nums[i]);
        printf("%d -> %d %s\n", nums[i], r, r == nums[i] ? "palindrome" : "not palindrome");
    }
    return 0;
}
Output
12321 -> 12321 palindrome
4567 -> 7654 not palindrome

Keep going#

Finished this lesson?