
What are some common but not easy to get interview questions related to C programming language?
When preparing for an interview focused on C programming, here are some advanced and challenging questions that you might encounter:
Void Pointers and Pointer Manipulation
- What is a void pointer? Can you dereference a void pointer without knowing its type? A void pointer is a generic pointer that can point to any data type, but it cannot be dereferenced without casting it to the appropriate type15.
Memory Management and Pointers
- What are dangling pointers? How are dangling pointers different from memory leaks? Dangling pointers point to memory locations that have already been freed or are no longer valid. This is different from memory leaks, where memory is allocated but never freed15.
- How to free a block of memory previously allocated without using free()?
This is generally not recommended, but in some contexts, you might use other library functions or system calls to manage memory, though
free()
is the standard way to deallocate memory in C1.
Preprocessor Directives and Macros
- Explain the #pragma directive.
The
#pragma
directive is used to provide additional information to the compiler beyond what is conveyed in the language itself. It can be used for various purposes such as controlling optimization, warning levels, or other compiler-specific features1.
Data Structures and Bitwise Operations
- Consider the two structures Struct A and Struct B given below. What will be the size of these structures? This question tests understanding of bit fields and structure padding. The size can vary based on the compiler's alignment rules and the specific bit fields defined1.
Advanced Syntax and Semantics
- What will be the output when the following code is executed? Explain.
#include <stdio.h> #define SQUARE(a) (a)*(a) int main() { printf("%d\n", SQUARE(4)); int x = 3; printf("%d\n", SQUARE(++x)); return 0; }
This code tests the understanding of macro expansion and operator precedence. The output will be
16
and then an incorrect result forSQUARE(++x)
due to the order of operations in macro expansion1.
Input/Output and Formatting
- What is the use of %n in printf()?
The
%n
format specifier inprintf()
stores the number of characters written so far into an integer pointer argument. It is used to keep track of the number of characters printed1.
Algorithmic Thinking
- What is Bubble Sort Algorithm? Explain with a program.
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. Here is an example implementation:
#include <stdio.h> void bubbleSort(int arr[], int n) { int i, j; for (i = 0; i < n-1; i++) { for (j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } }
This tests the candidate's understanding of basic sorting algorithms1.
Memory and Bitwise Operations
- What is the output of the following program if run on a 32-bit operating system?
#include <stdio.h> int main() { int a = -2; printf("%x", a >> 3); return 0; }
This question tests the understanding of bitwise shift operations and how they behave with negative numbers on different architectures1.
Function and Parameter Concepts
- What is the difference between an actual parameter and a formal parameter? Actual parameters are the values passed to a function when it is called, while formal parameters are the variables declared in the function definition to receive these values4.
String and Character Operations
- How can you print a string with the symbol % in it?
To print a string containing the
%
symbol, you need to use%%
because%
is a special character inprintf()
format strings1.
These questions cover a range of advanced C programming concepts, including memory management, data structures, and complex syntax, which can help assess a candidate's deep understanding and expertise in C programming.