C Interview Questions

C programming interviews test your understanding of language fundamentals, memory management, pointers, and problem-solving ability. This topic covers the most commonly asked C interview questions with clear explanations and code examples — organized from basic to advanced.

Basic Level Questions

Q1. What is the difference between int, short, long, and long long?

These are all integer types but differ in size and the range of values they can store.

TypeTypical SizeRange (signed)
short2 bytes-32,768 to 32,767
int4 bytes-2,147,483,648 to 2,147,483,647
long4 or 8 bytesPlatform-dependent
long long8 bytes~-9.2 × 10¹⁸ to 9.2 × 10¹⁸

Q2. What is the difference between == and = in C?

= is the assignment operator — it sets a value. == is the equality operator — it compares two values and returns 1 (true) or 0 (false).

int x = 5;      // assignment — x gets the value 5
if (x == 5)     // comparison — checks if x equals 5
    printf("Five\n");

// Common bug: using = in a condition
if (x = 10)     // always true! x is set to 10, then evaluated as non-zero
    printf("This always prints\n");

Q3. What is the difference between ++i and i++?

  • ++i (pre-increment): increments first, then returns the new value
  • i++ (post-increment): returns the current value first, then increments
int i = 5;
printf("%d\n", ++i);   // Output: 6 (incremented first)
printf("%d\n", i++);   // Output: 6 (returns 6, then i becomes 7)
printf("%d\n", i);     // Output: 7

Q4. What is a NULL pointer?

A NULL pointer is a pointer that does not point to any valid memory address. It is used to indicate that a pointer is not initialized or currently points to nothing.

int *ptr = NULL;   // safe initialization

if (ptr == NULL)
    printf("Pointer is not pointing to anything\n");

// Dereferencing NULL causes a segmentation fault:
// *ptr = 10;   // CRASH — never dereference NULL

Q5. What is the difference between char *str and char str[]?

char str1[] = "Hello";   // stored on stack, modifiable
char *str2  = "Hello";   // pointer to read-only string literal — do not modify

str1[0] = 'h';   // OK — modifiable
str2[0] = 'h';   // undefined behavior — string literal is read-only

Intermediate Level Questions

Q6. What is the difference between struct and union?

Featurestructunion
MemoryEach member has its own memoryAll members share the same memory
SizeSum of all member sizes (+ padding)Size of the largest member
AccessAll members accessible at the same timeOnly one member holds valid data at a time
struct S { int a; float b; };   // sizeof = 4 + 4 = 8 bytes
union  U { int a; float b; };   // sizeof = 4 bytes (largest member)

Q7. What is a dangling pointer?

A dangling pointer is a pointer that points to memory that has already been freed. Using it causes undefined behavior — crashes or corrupt data.

#include <stdlib.h>

int *ptr = (int*) malloc(sizeof(int));
*ptr = 42;
free(ptr);   // memory released

// ptr is now dangling — pointing to freed memory
*ptr = 10;   // UNDEFINED BEHAVIOR — do not do this

// Fix: set to NULL after free
free(ptr);
ptr = NULL;  // safe — dereferencing NULL is a clear crash, not silent corruption

Q8. What does static do in C?

The static keyword has two distinct uses depending on context:

  • Inside a function: The variable retains its value between calls (not destroyed when function exits)
  • Outside a function (global scope): The variable/function is restricted to the current file only
void count()
{
    static int n = 0;   // initialized only once
    n++;
    printf("%d\n", n);
}
// count() prints: 1, 2, 3, 4 ... across multiple calls

Q9. What is the difference between malloc() and calloc()?

Featuremalloc()calloc()
Syntaxmalloc(size)calloc(count, size)
InitializationMemory is NOT zeroed — contains garbageMemory IS zeroed — all bytes set to 0
Use whenYou will initialize values yourselfYou need a zero-initialized array
int *a = (int*) malloc(5 * sizeof(int));    // garbage values
int *b = (int*) calloc(5, sizeof(int));     // all zeros: {0,0,0,0,0}

Q10. What is a void pointer?

A void * is a generic pointer that can point to any data type. It must be cast to the appropriate type before dereferencing.

#include <stdio.h>

void printValue(void *ptr, char type)
{
    if (type == 'i') printf("%d\n", *(int*)ptr);
    if (type == 'f') printf("%.2f\n", *(float*)ptr);
}

int main()
{
    int x = 42;
    float y = 3.14;

    printValue(&x, 'i');   // Output: 42
    printValue(&y, 'f');   // Output: 3.14

    return 0;
}

Advanced Level Questions

Q11. What is the output of this code?

#include <stdio.h>

int main()
{
    int a = 5, b = 10;
    printf("%d %d\n", a++, ++b);
    printf("%d %d\n", a, b);
    return 0;
}

Answer:

5 11
6 11

a++ returns 5 (then a becomes 6). ++b increments b to 11 first, then returns 11.

Q12. What is the difference between const int *p, int * const p, and const int * const p?

DeclarationPointer isValue pointed to is
const int *pChangeableRead-only
int * const pRead-only (fixed address)Changeable
const int * const pRead-onlyRead-only
int val = 10, other = 20;

const int *p1 = &val;    // cannot do *p1 = 5, but can do p1 = &other
int * const p2 = &val;   // can do *p2 = 5, but cannot do p2 = &other

Q13. What is the sizeof operator and is it a function?

sizeof is a compile-time operator, not a function. It returns the size in bytes of a type or variable at compile time — no parentheses are actually required for variables (though they are needed for types).

printf("%lu\n", sizeof(int));      // 4
printf("%lu\n", sizeof(double));   // 8
printf("%lu\n", sizeof(char));     // 1

int arr[10];
printf("%lu\n", sizeof(arr));      // 40 (10 * 4)
printf("%lu\n", sizeof(arr) / sizeof(arr[0]));  // 10 — number of elements

Q14. What are memory leaks and how do you prevent them?

A memory leak occurs when memory allocated with malloc() is never freed with free(). The program uses more and more memory over time, eventually running out.

// Leak: allocates on every call but never frees
void createLeak()
{
    int *ptr = malloc(100 * sizeof(int));
    // forgot free(ptr) — memory lost when function returns
}

// Fix: always free before returning
void noLeak()
{
    int *ptr = malloc(100 * sizeof(int));
    if (!ptr) return;
    // ... use ptr ...
    free(ptr);   // memory returned to the OS
}

Q15. Write a program to reverse a string without using a library function.

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

void reverseString(char str[])
{
    int left  = 0;
    int right = strlen(str) - 1;

    while (left < right)
    {
        char temp  = str[left];
        str[left]  = str[right];
        str[right] = temp;
        left++;
        right--;
    }
}

int main()
{
    char word[] = "Hello";
    reverseString(word);
    printf("%s\n", word);   // Output: olleH
    return 0;
}

Quick Reference: Common Interview Topics

TopicKey Points to Know
PointersDeclaration, dereferencing, pointer arithmetic, NULL pointer
Memorymalloc/calloc/realloc/free, stack vs heap, memory leaks
StringsNull terminator, string vs char array, strcmp vs ==
Storage Classesauto, static, extern, register and their lifetimes
Structuresstruct vs union, typedef, nested structs, pointer to struct
Preprocessor#define, #include, #ifdef, macros with arguments
Bit OperationsAND, OR, XOR, NOT, shifts, masking bits
RecursionBase case, stack frame, factorial, Fibonacci, tree traversal

Summary

C interviews focus on core language knowledge: data types and their sizes, pointer behavior, memory management, storage classes, and common programming patterns. The most frequently tested areas are pointers (especially null, dangling, void, and const pointers), the difference between stack and heap memory, string handling without library functions, and the behavior of operators like pre/post increment and sizeof. Understanding struct vs union, malloc vs calloc, and static in different contexts sets strong candidates apart. Practice writing small programs — like reversing a string, checking for palindromes, swapping values, and implementing sorting — without library help. Solid fundamentals and clean, correct code are the two things interviewers look for most.

Leave a Comment

Your email address will not be published. Required fields are marked *