Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

C Language Interview Questions

C Language

Top 25 C Language Interview Questions

Curated questions covering pointers, memory management, data structures, preprocessor directives, and core C concepts.

01

What are the basic data types in C?

C provides four basic data types: int (integer), float (single-precision floating point), double (double-precision floating point), and char (single character). These can be modified with signed, unsigned, short, and long qualifiers.

Example
int age = 25;
float pi = 3.14f;
double precise = 3.14159265358979;
char grade = 'A';
unsigned int count = 100;
02

What is a pointer in C?

A pointer is a variable that stores the memory address of another variable. Pointers enable dynamic memory allocation, efficient array handling, and passing variables by reference to functions.

Example
int x = 10;
int *ptr = &x;   // ptr holds address of x
printf("%d\n", *ptr); // dereference: prints 10
*ptr = 20;
printf("%d\n", x);    // x is now 20
03

What is the difference between arrays and pointers in C?

An array name is a constant pointer to its first element. However, arrays and pointers differ: sizeof(array) gives the total array size; sizeof(pointer) gives the pointer size. Arrays cannot be reassigned; pointers can.

Example
int arr[] = {1, 2, 3};
int *ptr = arr;
printf("%d\n", arr[1]);  // 2
printf("%d\n", *(ptr+1)); // 2 — pointer arithmetic
04

What are strings in C and how are they stored?

Strings in C are arrays of characters terminated by a null character ('\0'). They are stored in contiguous memory. The standard library <string.h> provides functions like strlen(), strcpy(), strcat(), and strcmp().

Example
char name[] = "Alice";
// Stored as: A l i c e \0
printf("Length: %zu\n", strlen(name)); // 5
char copy[10];
strcpy(copy, name);
05

What is the difference between pass by value and pass by reference in C?

C is strictly pass-by-value — a copy of the argument is passed. To simulate pass-by-reference, pass a pointer to the variable, allowing the function to modify the original value.

Example
void addOne(int *n) { (*n)++; }

int main() {
    int x = 5;
    addOne(&x);
    printf("%d\n", x); // 6
    return 0;
}
06

What is recursion in C? Give an example.

Recursion is when a function calls itself to solve a smaller instance of the same problem. Every recursive function needs a base case to stop the recursion.

Example
int factorial(int n) {
    if (n <= 1) return 1;       // base case
    return n * factorial(n - 1); // recursive call
}
// factorial(5) = 120
07

What is a structure in C?

A structure (struct) groups variables of different data types under a single name. It is used to represent a record or entity with multiple attributes.

Example
struct Student {
    char name[50];
    int age;
    float gpa;
};

struct Student s1 = {"Alice", 20, 3.8f};
printf("%s is %d years old\n", s1.name, s1.age);
08

What is the difference between struct and union in C?

  • struct — allocates memory for all members; total size is the sum of all member sizes (plus padding).
  • union — all members share the same memory location; size equals the largest member. Only one member holds a valid value at a time.
  • Use union to save memory when only one field is needed at a time.
Example
union Data {
    int i;
    float f;
    char str[20];
};
// sizeof(Data) == sizeof(str) == 20
09

What is dynamic memory allocation in C?

  • malloc(size) — allocates size bytes; memory is uninitialised.
  • calloc(n, size) — allocates n*size bytes; memory is zero-initialised.
  • realloc(ptr, size) — resizes a previously allocated block.
  • free(ptr) — releases allocated memory back to the heap.
Example
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) { /* handle error */ }
for (int i = 0; i < 5; i++) arr[i] = i + 1;
arr = (int *)realloc(arr, 10 * sizeof(int));
free(arr);
10

What is a dangling pointer and how do you avoid it?

A dangling pointer points to memory that has been freed or gone out of scope. Dereferencing it causes undefined behaviour. Avoid it by setting the pointer to NULL after freeing.

Example
int *ptr = (int *)malloc(sizeof(int));
*ptr = 42;
free(ptr);
ptr = NULL; // prevent dangling pointer
if (ptr != NULL) { *ptr = 10; } // safe check
11

What is a memory leak in C?

A memory leak occurs when dynamically allocated memory is never freed, causing the program to consume increasing amounts of memory. Always pair every malloc/calloc with a corresponding free().

12

What are preprocessor directives in C?

Preprocessor directives are instructions processed before compilation. They begin with #. Common directives include #include (file inclusion), #define (macro definition), #ifdef/#ifndef (conditional compilation), and #pragma.

Example
#include &lt;stdio.h&gt;
#define MAX 100
#define SQUARE(x) ((x) * (x))

#ifdef DEBUG
    printf("Debug mode\n");
#endif
13

What is typedef in C?

typedef creates an alias for an existing data type, improving code readability and portability.

Example
typedef unsigned long long ull;
typedef struct {
    int x, y;
} Point;

ull bigNum = 1000000000ULL;
Point p = {3, 4};
14

What are storage classes in C?

  • auto — default for local variables; stored on the stack; destroyed when scope ends.
  • register — hints to store in a CPU register for faster access; cannot take address.
  • static — preserves value between function calls; local static variables persist for the program lifetime.
  • extern — declares a variable defined in another file; enables sharing across translation units.
15

What are bitwise operators in C?

  • & (AND) — sets bit if both bits are 1.
  • | (OR) — sets bit if either bit is 1.
  • ^ (XOR) — sets bit if bits differ.
  • ~ (NOT) — inverts all bits.
  • << (left shift) — shifts bits left, multiplying by 2.
  • >> (right shift) — shifts bits right, dividing by 2.
Example
int a = 12; // 1100
int b = 10; // 1010
printf("%d\n", a & b);  // 8  (1000)
printf("%d\n", a | b);  // 14 (1110)
printf("%d\n", a ^ b);  // 6  (0110)
printf("%d\n", a << 1); // 24 (11000)
16

What is the difference between stack and heap memory?

  • Stack — automatically managed; stores local variables and function call frames; limited size; LIFO order.
  • Heap — manually managed via malloc/free; larger; used for dynamic allocation; slower access.
  • Stack overflow occurs when the stack exceeds its limit (e.g., infinite recursion).
17

What is a void pointer in C?

A void pointer (void *) is a generic pointer that can point to any data type. It must be cast to the appropriate type before dereferencing. Used in generic functions like malloc() and memcpy().

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

What are function pointers in C?

Function pointers store the address of a function and allow calling functions dynamically. They are used for callbacks, dispatch tables, and implementing polymorphism in C.

Example
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }

int (*op)(int, int) = add;
printf("%d\n", op(3, 2)); // 5
op = sub;
printf("%d\n", op(3, 2)); // 1
19

What is file I/O in C?

C provides file operations through <stdio.h>. fopen() opens a file, fclose() closes it. fread()/fwrite() handle binary I/O; fprintf()/fscanf() handle text I/O.

Example
FILE *fp = fopen("data.txt", "w");
if (fp) {
    fprintf(fp, "Hello, File!\n");
    fclose(fp);
}
fp = fopen("data.txt", "r");
char buf[100];
fgets(buf, sizeof(buf), fp);
fclose(fp);
20

What is an enum in C?

An enum (enumeration) defines a set of named integer constants, improving code readability.

Example
enum Day { MON=1, TUE, WED, THU, FRI, SAT, SUN };
enum Day today = WED;
printf("Day number: %d\n", today); // 3
21

What is the difference between #define and const in C?

  • #define — preprocessor macro; no type checking; replaced textually before compilation.
  • const — typed constant; subject to type checking; has a memory address; preferred in modern C.
22

What is a null pointer in C?

A null pointer is a pointer that does not point to any valid memory location. It is represented by NULL (defined as 0 or (void*)0). Always initialise pointers to NULL and check before dereferencing.

23

What is the difference between C and C++?

  • C is a procedural language; C++ is multi-paradigm (procedural + OOP + generic).
  • C++ adds classes, objects, inheritance, polymorphism, templates, and the STL.
  • C++ supports function/operator overloading, references, and exception handling.
  • C is simpler and closer to hardware; C++ provides higher-level abstractions.
24

What is the scope of a variable in C?

  • Local scope — variable declared inside a function/block; accessible only within that block.
  • Global scope — variable declared outside all functions; accessible throughout the file.
  • File scope — static global variable; accessible only within the file it is declared in.
25

What is the use of the volatile keyword in C?

volatile tells the compiler that a variable's value may change at any time (e.g., by hardware, an interrupt, or another thread) and should not be optimised away or cached in a register. Commonly used in embedded systems and device drivers.


Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.