Interview prep
C interview questions
C interview questions for freshers cover data types and operators, pointers and arrays, strings, structures and unions, storage classes, dynamic memory with malloc and free, the preprocessor and file handling. Service-company technical rounds also ask you to predict the output of short programs and to write simple ones, such as reversing a string.
Kaabil AI builds and scores your resume, and its Career Agent emails recruiters for jobs you approve. 7 days free, no card.Start free trial
Code samples are standard C. Sizes shown are typical on 64-bit compilers; the C standard only sets minimums.
Basic C interview questions for freshers
What is C, and why is it still used?
BasicC is a general-purpose, procedural programming language created by Dennis Ritchie at Bell Labs in the early 1970s. It is fast, gives direct access to memory and hardware, and is used for operating systems, embedded systems, compilers and device drivers.
What are the basic data types in C?
Basicchar, int, float and double, with modifiers short, long, signed and unsigned. On most 64-bit compilers, char is 1 byte, int is 4 bytes, float is 4 bytes and double is 8 bytes; use sizeof to check on your system.
What is the difference between a declaration and a definition?
BasicA declaration tells the compiler a name and its type (extern int count;). A definition also creates it: it allocates memory for a variable or provides the body of a function. A variable can be declared many times but defined only once.
What is the difference between i++ and ++i?
BasicBoth add 1 to i. ++i (pre-increment) increments first and then gives the new value; i++ (post-increment) gives the old value and then increments.
int i = 5; int a = i++; /* a = 5, i = 6 */ int b = ++i; /* b = 7, i = 7 */What is the difference between = and ==?
Basic= assigns a value. == compares two values and gives 1 if they are equal, 0 if not. Writing if (x = 5) by mistake assigns 5 and is always true; compilers warn about it.
What is the difference between while and do-while?
BasicA while loop checks its condition first, so its body may never run. A do-while loop runs the body once before checking, so it always runs at least once.
What is the difference between break and continue?
Basicbreak exits the loop (or switch) immediately. continue skips the rest of the current iteration and moves to the next one.
Why is a break needed in each switch case?
BasicWithout break, execution "falls through" into the next case and runs its statements too. Fall-through is sometimes used on purpose, but usually it is a bug.
Pointer interview questions in C
What is a pointer?
BasicA variable that stores the memory address of another variable. & gives the address of a variable and * (dereference) gives the value at an address.
int x = 10; int *p = &x; printf("%d", *p); /* prints 10 */What are NULL, void, dangling and wild pointers?
Intermediate- NULL pointer: points to nothing; check for NULL before dereferencing.
- void pointer: a generic pointer (void *) that can hold any address but must be cast before dereferencing.
- Dangling pointer: still points to memory that has been freed or has gone out of scope.
- Wild pointer: declared but never initialised, so it points anywhere.
Does C support call by reference?
IntermediateC passes every argument by value. To let a function change the caller's variable, you pass its address (a pointer) by value, which gives the effect of call by reference.
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } /* call: swap(&x, &y); */What is pointer arithmetic?
IntermediateAdding 1 to a pointer moves it to the next element of its type, not the next byte. If p is an int pointer and int is 4 bytes, p + 1 points 4 bytes further.
What is the difference between an array and a pointer?
IntermediateAn array is a fixed block of elements; its name usually converts to a pointer to the first element, but you cannot reassign it. A pointer is a variable that can point anywhere. sizeof an array gives the whole array's size; sizeof a pointer gives the pointer's size.
What is a pointer to a pointer?
IntermediateA variable that stores the address of another pointer, declared with two asterisks (int **pp). It is used for dynamic 2D arrays and for functions that must change a pointer, such as allocating memory for the caller.
What is a function pointer?
AdvancedA pointer that stores the address of a function, so the function can be passed as an argument or chosen at run time. qsort, which takes a compare function, is the common example.
int add(int a, int b) { return a + b; } int (*op)(int, int) = add; int r = op(2, 3); /* r = 5 */
Memory, storage class and structure questions
What is the difference between malloc and calloc?
Basicmalloc(size) allocates one block of the given size and leaves it uninitialised. calloc(n, size) allocates memory for n elements and sets every byte to zero. Both return NULL if allocation fails, and both blocks are released with free().
What does realloc do?
IntermediateIt changes the size of a block allocated earlier, keeping its contents up to the smaller size. It may move the block, so always use the pointer it returns, and keep the old one until you know realloc did not return NULL.
What is a memory leak?
BasicMemory allocated with malloc, calloc or realloc that is never freed, usually because the last pointer to it was lost. A long-running program that leaks keeps using more memory.
What is the difference between stack and heap memory?
IntermediateLocal variables live on the stack: allocation is automatic and fast, and they disappear when the function returns. The heap is used for dynamic memory: you allocate it with malloc and must free it yourself, and it lasts until you do.
What are the storage classes in C?
Basicauto (the default for local variables), register (a hint to keep the variable in a CPU register), static (keeps its value between calls, or limits a global to its file) and extern (declares a variable defined in another file). C11 added _Thread_local (thread_local since C23), which gives each thread its own copy of a variable.
What does a static local variable do?
BasicIt is initialised once and keeps its value between function calls, unlike a normal local variable.
void counter(void) { static int n = 0; n++; printf("%d ", n); } /* three calls print: 1 2 3 */What is the difference between a structure and a union?
BasicIn a structure, every member has its own memory, so the size is at least the sum of the members (plus padding). In a union, all members share the same memory, so its size is at least that of the largest member (plus any padding for alignment), and only one member holds a valid value at a time.
What is structure padding?
AdvancedThe compiler inserts unused bytes between structure members so each one sits at an address suited to its type, which makes memory access faster. That is why sizeof a structure can be larger than the sum of its members.
What do const and volatile mean?
Intermediateconst makes a variable read-only after initialisation. volatile tells the compiler the value can change outside the program's control, such as a hardware register, so it must be read from memory every time and not optimised away.
Preprocessor, string and file questions
What is the difference between a macro and a function?
IntermediateA macro (#define) is replaced by the preprocessor as text before compilation: no type checking and no call overhead, but arguments can be evaluated twice. A function is compiled once, checks types and is called at run time.
#define SQUARE(x) ((x) * (x)) /* SQUARE(i++) increments i twice: a classic macro trap */What is the difference between #include <file.h> and #include "file.h"?
BasicAngle brackets search the system include directories, for standard headers such as stdio.h. Quotes are for your own headers: most compilers first search the directory of the file that contains the #include, then the system directories. The exact order is implementation-defined.
How is a string stored in C?
BasicAs an array of characters ending with the null character \0. strlen counts the characters before \0, so a char array for "hello" needs 6 bytes.
What do strcpy, strcat and strcmp do?
Basicstrcpy copies one string into another, strcat appends one string to the end of another, and strcmp compares two strings: 0 if equal, a negative or positive value depending on which comes first. The destination must be large enough; strncpy and strncat limit how much is copied.
What are the file opening modes in C?
Intermediate"r" read, "w" write (creates the file or empties it), "a" append, and "r+", "w+" and "a+" for reading and writing. Add "b" for binary files. fopen returns NULL if the file cannot be opened, so always check it, and close the file with fclose.
What are argc and argv?
IntermediateThe arguments of main when a program is run from the command line: argc is the number of arguments, and argv is an array of strings holding them, with the program name in argv first.
C programs asked in interviews
Write these by hand before the interview. Interviewers watch your logic and edge cases as much as the syntax.
Write a program to reverse a string.
BasicSwap characters from both ends, moving towards the middle.
void reverse(char *s) { int i = 0, j = strlen(s) - 1; while (i < j) { char t = s[i]; s[i] = s[j]; s[j] = t; i++; j--; } }Write a program to check whether a number is prime.
BasicNumbers below 2 are not prime. Otherwise, check divisors only up to the square root of n.
int is_prime(int n) { if (n < 2) return 0; for (int i = 2; i * i <= n; i++) if (n % i == 0) return 0; return 1; }How do you swap two numbers without a third variable?
BasicWith addition and subtraction, or with XOR. The arithmetic method can overflow for very large values; in real code a temporary variable is clearer.
a = a + b; b = a - b; a = a - b;Write a recursive function for factorial.
BasicA recursive function calls itself with a smaller input and stops at a base case. Factorials grow fast: 13! no longer fits in a 32-bit int, so use unsigned long long.
unsigned long long fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }What is the output of this program?
IntermediateIt prints 10 20. The function receives copies of x and y, so swapping them inside the function does not change the variables in main.
void swap(int a, int b) { int t = a; a = b; b = t; } int main(void) { int x = 10, y = 20; swap(x, y); printf("%d %d", x, y); return 0; }
How to prepare for a C interview
Revise pointers until you can draw memory diagrams for pointers, arrays, strings and pointers to pointers. Most C rounds dig deepest here.
Write the common programs by hand without a compiler: reverse a string, palindrome, prime, factorial, Fibonacci, swap, largest of an array, bubble sort and a linked list insert.
Practise "what is the output" questions on i++ versus ++i, operator precedence, static variables, sizeof and call by value.
Compile with warnings on (gcc -Wall) and read every warning: the bugs it catches are the same ones interviewers ask about.
Learn the basics of data structures in C (arrays, linked lists, stacks, queues), because the same rounds often move on to them.
Why do IT companies ask C questions to freshers?
C shows whether you understand what a program does in memory: how variables are stored, how pointers work and what happens when you allocate and free memory. Even when the job uses Java or Python, many service-company technical rounds start with C basics, especially for electronics and electrical graduates. After C, interviewers often move to networking basics; see our computer networks interview questions.
Kaabil App
Prove your skills before the interview.
Take skill assessments in the Kaabil AI app. Passed checks become certificates from Kaabil AI by Redrob, with a verification link.
See the Kaabil App7 days free · No card
Questions freshers ask
Pointers, arrays and strings, structures and unions, storage classes (especially static), dynamic memory with malloc and free, call by value, and output-prediction questions. Practise short programs too.
C is a strong base, but most roles also expect one more language such as Java or Python, plus basic data structures, SQL and problem-solving. C helps most for embedded and systems roles.
C is procedural. C++ builds on C and adds object-oriented programming (classes, inheritance, polymorphism), templates, references, function overloading and a large standard library. Most C code compiles as C++, but not all of it.