C is one of the most popular programming languages and a must-know for any developer With its low-level access, high performance, and usage across operating systems, C opens the door to many programming jobs However, C interviews can be tricky with complex technical questions. In this article, we provide the top 50 C interview questions to help you prepare.
Frequently Asked C Interview Questions for Beginners
-
What are the features of C as a programming language?
C is a mid-level procedural language that combines high-level abstraction with low-level control Key features include
- Portability – works across operating systems
- Speed and efficiency – compiled language
- Low-level manipulation – pointers, memory access
- Modularity – functions, headers
- Flexibility – works for varied applications
-
What are the basic data types in C?
C has basic data types for integers, characters, floats and doubles. Some common ones
- int – stores integers
- char – stores single characters
- float – stores fractional numbers
- double – stores fractional numbers with higher precision
-
What is the difference between call by value and call by reference?
In call by value, the value of the argument is copied. In call by reference, the address of the argument is passed so that changes inside the function affect the original variable.
-
What are pointers and how are they useful?
Pointers store memory addresses and allow direct access to memory for efficient manipulation. Uses include memory allocation, array access, function arguments, etc.
-
What is recursion and when is it used?
Recursion is when a function calls itself. It is used to solve problems that can be broken down into smaller sub-problems, like certain algorithms and data structure traversals.
-
What are preprocessor directives in C?
Preprocessor directives give instructions to the compiler before compilation. Examples are #include for header files, #define for macros, #if for conditional compilation.
-
What is the difference between macros and functions?
Macros are processed by the preprocessor but functions are compiled. Macros are faster but do not check types or scopes. Functions are type-safe and support modularity.
-
What are unions and structures?
Unions and structures allow grouping different data types. Union members share the same memory. Structure members have their own memory allocated.
-
What are header files and their uses?
Header files contain declarations/definitions used across multiple code files. Examples: stdio.h for I/O, math.h for mathematical functions. They promote modularity and reuse.
-
What is dynamic memory allocation?
Dynamically allocated memory is allocated at runtime using library functions like malloc(), calloc(), realloc(), and free(). This allows memory allocation as needed.
Intermediate C Interview Questions
-
What are static, automatic and manual variables?
- Static: Retain value even after function returns. Default initialized to zero.
- Automatic: Created when function is called, destroyed when exits. Garbage value initially.
- Manual: Global variables, declared outside functions. Default initialized to zero.
-
What is the difference between arrays and pointers?
Arrays contain multiple elements of the same type in a contiguous block of memory. Pointers store the address of a variable or memory location. An array name can decay to a pointer to its first element.
-
How can strings be declared and defined in C?
Strings can be declared as array of characters – char str[10];. They can be initialized by assigning a string literal in quotes – char str[10] = “hello”; The end is marked by a null character .
-
What are storage classes in C?
Storage classes like auto, register, static, extern define scope, visibility, life and location of variables and functions. They specify where storage will be allocated.
-
What is a structure? How is it different from a union?
A structure groups related data types. Each member has its own memory. A union groups different data types in one memory location. A structure preserves all member data while a union only saves one member data.
-
What are enumerations?
Enumerations define custom symbolic constants. They make code more readable by allowing meaningful names. Values are auto-incremented: enum week {Mon, Tue, Wed…};
-
How can you pass arguments by reference in C?
By passing pointers to the arguments. The address of the argument is passed instead of the value, allowing changes to reflect in the calling function.
-
What are dynamic memory functions in C?
Functions like malloc(), calloc(), realloc() and free() are used for dynamic allocation and deallocation of memory at runtime. This manages memory manually.
-
What is the difference between break and continue statements?
Break terminates the current loop and transfers execution to the next statement after the loop. Continue skips the current iteration and goes to the next iteration of the loop.
-
How are multidimensional arrays represented and accessed in C?
2D arrays are stored linearly with elements from rows put one after another. They can be accessed with two indices, first for row and second for column.
Advanced C Interview Questions
-
What are near, far and huge pointers?
Near, far and huge are special pointer types used in 16-bit architectures to define segments. Near points to same segment, far to another segment, huge to large data structures beyond 64KB.
-
What is function prototyping and why is it useful?
Function prototyping declares function parameters before use. It ensures correct argument types passed, avoiding implicit type conversions. It helps detect errors during compilation.
-
What is a buffer overflow? How can it be avoided?
Buffer overflow occurs when data written to buffer exceeds its size. It can overwrite and corrupt data. It can be avoided by boundary checks for arrays and strings and using functions like fgets() instead of gets().
-
What is a dangling pointer? How can you avoid it?
A pointer pointing to memory freed by delete/free() is left dangling. It can cause crashes or data corruption. Avoid by assigning NULL after freeing memory. Always check for NULL before dereferencing.
-
What are the differences between C and C++?
C is a procedural language while C++ supports OOP concepts like classes, inheritance, templates, exceptions. C++ has stronger type checking than C. C++ supports function overloading, namespaces, and references.
-
How can you generate random numbers in C?
Use rand() and srand() functions along with time(0) seed value to generate pseudo-random numbers. Include stdlib.h header file.
-
What are variable arguments or varargs in C?
Varargs allow functions to accept variable number of arguments using ellipses (…) notation. va_list, va_start, va_arg, va_end macros are used to access arguments. stdarg.h header is required.
-
How does free() function work?
Free() releases dynamically allocated memory created by malloc(), calloc() or realloc(). It takes the pointer to memory block to be freed as argument. It makes the memory available for future allocations.
-
What are memory leaks and how can you avoid them?
Memory leaks happen when dynamically allocated memory is not freed after use, consuming RAM over time. Avoid by freeing unreferenced memory. Use tools like valgrind to check leaks.
-
What is preprocessor and how does it work?
Preprocessor processes directives like #include, #define before actual compilation. Directives allow inclusion of header files, macros for code reuse. Preprocessed code is then compiled.
C Interview Questions on Programming
-
Write a program to swap two numbers without a temporary variable.
Use bitwise XOR operator to swap numbers. XOR swaps values when applied twice.
cvoid swap (int *a, int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b;} -
Write a program to find if a number is a power of 2.
Keep dividing the number by 2 and check if the final remainder is 1. A power of 2 will divide completely by 2 leaving 1.
cbool isPowerOf2(int n) { if(n <= 0) return false; while(n > 1) { if(n % 2 != 0) return false; n = n / 2; } return true;} -
Write a program to find the factorial of a number.
Use recursion to calculate factorial by reducing the problem. Base case returns 1 when number is 0 or 1.
cint factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1);} -
Write a program to check if a string is a palindrome.
Use two pointers starting at extremes, increment one and decrement other while comparing characters.
cbool isPalindrome(char str[]) { int l = 0; int h = strlen(str) - 1; while(h > l) { if(str[l++] != str[h
What are the features of the C language?
- A middle-level language lets you access memory at a low level and make decisions at a higher level, making it both efficient and easy to use.
- The code written in C can run on different platforms with only minor changes.
- Efficiency: Offers efficient memory management and execution speed.
- Roch Standard Library: Offers a complete set of functions for different tasks
- Procedural Language: Follows a structured, step-by-step approach to solving problems.
4 Write a program to check an Armstrong number.
This C program checks if a given number is an Armstrong number or not. It calculates the sum of cubes of its digits and compares it with the original number. If they are equal, its an Armstrong number. 1^3 + 5^3 + 3^3 = 153, which matches the original number.