50 Most Common C Language Interview Questions and Answers
50 Most Common C Language Interview Questions and Answers (With Examples)
C programming is one of the most widely used languages in system-level development and embedded systems. Whether you’re a student, fresher, or an experienced candidate preparing for technical interviews, understanding C thoroughly is essential. Here are 50 of the most frequently asked C language interview questions and answers, explained in detail, complete with real-world examples to make learning effective and memorable.
1. What is C language?
C is a procedural programming language developed in the early 1970s by Dennis Ritchie. It is used for system and application software, embedded firmware, and compilers.
Example:
1 2 3 4 5 6 |
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; } |
2. What are the features of C language?
- Procedural language
- Portable
- Low-level access to memory via pointers
- Rich set of built-in operators
- Modular programming support
- Fast and efficient
Example:
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { printf("%d", add(3, 5)); return 0; } |
3. What is the difference between C and C++?
Feature | C | C++ |
---|---|---|
Type | Procedural | Object-Oriented |
Encapsulation | Not Supported | Supported via Classes |
Function Overloading | Not Supported | Supported |
Memory Handling | Manual via malloc/free | Can use new/delete |
Example (C):
1 2 3 4 |
int sum(int a, int b) { return a + b; } |
Example (C++):
1 2 3 4 5 6 7 |
class Calculator { public: int sum(int a, int b) { return a + b; } }; |
4. What is a variable?
A variable is a named location in memory that stores a value. The type of the variable determines what kind of data it can hold.
Example:
1 2 3 4 |
int age = 25; char grade = 'A'; float height = 5.9; |
5. What are data types in C?
There are several fundamental data types in C:
int
: for integersfloat
: for decimal numberschar
: for charactersdouble
: for high-precision decimal numbersvoid
: represents the absence of a value
Example:
1 2 3 4 5 |
int a = 10; float b = 10.5; char c = 'Z'; double d = 10.54321; |
6. What are operators in C?
Operators perform operations on variables and values. Categories include:
- Arithmetic operators (+, -, *, /, %)
- Relational operators (==, !=, >, <)
- Logical operators (&&, ||, !)
- Bitwise operators (&, |, ^, ~)
Example:
1 2 3 |
int x = 5, y = 10; printf("Sum: %d", x + y); |
7. Explain the use of if-else in C.
if-else
is used for decision making. If the condition is true, the if
block executes, otherwise else
runs.
Example:
1 2 3 4 5 6 |
int num = 10; if (num > 0) printf("Positive number\n"); else printf("Non-positive number\n"); |
8. What is a loop? What are the types?
Loops are used to execute a block of code repeatedly. Types:
for
loopwhile
loopdo-while
loop
Example (for loop):
1 2 3 |
for (int i = 0; i < 5; i++) printf("%d ", i); |
9. What is the difference between while and do-while loop?
while
: condition is checked before executiondo-while
: executes at least once regardless of the condition
Example (do-while):
1 2 3 4 5 6 |
int i = 0; do { printf("%d ", i); i++; } while(i < 3); |
10. What is the use of the break
statement in C?
The break
statement is used to exit from loops or switch cases prematurely.
Example:
1 2 3 4 5 |
for (int i = 0; i < 10; i++) { if (i == 5) break; printf("%d ", i); } |
11. What is the use of the continue
statement?
The continue
statement skips the current iteration of a loop and moves to the next one.
Example:
1 2 3 4 5 |
for (int i = 0; i < 5; i++) { if (i == 2) continue; printf("%d ", i); } |
12. What is a function in C?
A function is a block of reusable code that performs a specific task.
Example:
1 2 3 4 5 6 7 8 |
int add(int a, int b) { return a + b; } int main() { printf("%d", add(5, 10)); return 0; } |
13. What is a pointer?
A pointer is a variable that stores the address of another variable.
Example:
1 2 3 4 |
int a = 10; int *p = &a; printf("%d", *p); // Outputs: 10 |
14. How is memory allocated dynamically in C?
Using functions like malloc()
, calloc()
, realloc()
, and free()
.
Example:
1 2 3 |
int *arr = (int*) malloc(5 * sizeof(int)); free(arr); |
15. What is the difference between malloc()
and calloc()
?
malloc()
allocates memory without initializing.calloc()
allocates and initializes memory to zero.
Example:
1 2 3 |
int *a = malloc(5 * sizeof(int)); int *b = calloc(5, sizeof(int)); |
16. What is recursion?
Recursion is when a function calls itself.
Example:
1 2 3 4 5 |
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } |
17. What is a structure?
A structure is a user-defined data type that groups variables of different types.
Example:
1 2 3 4 5 |
struct Student { int id; char name[50]; }; |
18. What is a union?
A union is similar to a structure but shares memory for all its members.
Example:
1 2 3 4 5 |
union Data { int i; float f; }; |
19. What is the difference between structure and union?
- In a structure, each member has its own memory.
- In a union, all members share the same memory.
Example:
1 2 3 4 5 6 7 8 9 |
struct Test1 { int x; float y; }; union Test2 { int x; float y; }; |
20. What are storage classes in C?
They define the scope, visibility, and lifetime of variables. Types:
- auto
- static
- extern
- register
Example:
1 2 |
static int count = 0; |
21. What is the difference between call by value and call by reference?
In call by value, a copy of the variable is passed to the function. Changes inside the function do not affect the original variable.
In call by reference, the address (pointer) of the variable is passed, so changes inside the function affect the original variable.
Example:
22. What is the purpose of the sizeof
operator?
The sizeof
operator returns the size in bytes of a data type or variable. It helps in memory allocation and understanding system architecture.
Example:
23. What are header files in C?
Header files contain declarations of functions and macros. They are included using the #include
directive.
Example:
24. Explain the difference between struct
and typedef struct
.
struct
defines a structure type.typedef struct
defines a new name (alias) for a structure type, simplifying its usage.
Example:
Also Read,
C Programming: A Complete Guide from Beginners to Advanced
How to Crack Campus Placements β A Smart Studentβs Guide
25. What is a null pointer?
A pointer that points to nothing (address 0). It is used to signify that the pointer is not currently pointing to any valid memory.
Example:
26. What is pointer arithmetic?
Operations like addition or subtraction done on pointers to traverse memory locations based on the size of the data type pointed to.
Example:
27. What is the difference between ++i
and i++
?
-
++i
is pre-increment: increments the value before using it. -
i++
is post-increment: uses the current value then increments.
Example:
28. What is a dangling pointer?
A pointer that points to memory that has been freed or deallocated. Using it can cause undefined behavior.
Example:
29. What is the difference between struct
and class
in C++?
Although not a C concept, in C++,
-
struct
members are public by default. -
class
members are private by default.
30. What are the different storage classes in C?
-
auto
: default for local variables -
static
: persists for the program lifetime -
extern
: external linkage -
register
: suggests to store variable in CPU register
31. How to declare and use an array in C?
Arrays store multiple elements of the same data type.
Example:
32. What is the difference between an array and a pointer?
An array is a collection of elements stored contiguously, while a pointer holds a memory address. Arrays decay to pointers when passed to functions.
33. What is a constant in C?
A value that cannot be altered during program execution.
34. How do you define a constant?
Using #define
or const
keyword.
Example:
35. What is the difference between #include <file>
and #include "file"
?
-
<file>
searches for the header in standard system directories. -
"file"
searches in the current directory first.
36. What is the use of the return
statement in a function?
It returns a value to the caller and ends the function execution.
37. What is recursion? Provide an example.
Recursion is when a function calls itself to solve a problem by breaking it down into smaller subproblems.
Example:
38. What is the difference between printf
and scanf
?
-
printf
prints output to the console. -
scanf
reads input from the user.
39. What is the difference between ++
and --
operators?
-
++
increments a variable by 1. -
--
decrements a variable by 1.
40. What is an enum in C?
An enum is a user-defined type consisting of named integer constants.
Example:
41. What is a segmentation fault?
An error caused when a program accesses memory that it’s not allowed to.
42. What is the purpose of the void
keyword?
-
For functions that return no value.
-
For pointers to unknown data type (
void *
).
43. How can you prevent memory leaks in C?
By freeing dynamically allocated memory using free()
after use.
44. What is a memory leak?
When allocated memory is not freed, causing wasted memory resources.
45. What is the difference between fgets
and gets
?
-
fgets
reads a line with a limit to buffer size (safe). -
gets
reads input until newline but unsafe and deprecated.
46. What is the difference between static
and dynamic
memory allocation?
-
Static allocation occurs at compile-time (e.g., arrays).
-
Dynamic allocation occurs at runtime (e.g., malloc).
47. What is a macro?
Macros are preprocessor directives to define reusable code or constants.
Example:
48. How does a switch
statement work?
It selects code to execute based on the value of an expression.
49. What is the difference between struct
and union
?
In a struct
, members have separate memory; in a union
, members share the same memory location.
50. How do you handle errors in C?
Using error codes, return values, or setting errno
and checking it after library calls.
π― Final Thoughts
These 50 C language interview questions and answers give you a complete toolkit for cracking interviews confidently. β¨ Keep practicing, test these examples, and review real-world applications to strengthen your foundation.
π€ Stay Updated with NextGen Careers Hub
π± Follow us onΒ Instagram
πΊ Subscribe us onΒ YouTube
Please share our website with others:Β NextGenCareersHub.in
Comments are closed.