Table of contents
1.
Introduction
2.
C Interview Questions for Freshers
2.1.
1. Mention the most important features of the C language.
2.2.
2. Why is C called a mid-level programming language?
2.3.
3. What is the meaning of #include in a C program?
2.4.
4. What are the enumerations in C?
2.5.
5. What are macros? List some advantages of macro over functions.
2.6.
6. What are format specifiers in C?
2.7.
7. What happens if you don’t use a break statement in a switch case?
2.8.
8. Why do we use default statements in switch cases in C?
2.9.
9. Can you write a running C code without using the main() function?
2.10.
10. What do you mean by Dangling Pointers?
2.11.
11. What is a token?
2.12.
12. What is the use of printf() and scanf() functions? Also, explain format specifiers.
3.
C Interview Questions for Intermediate 
3.1.
13. Specify different types of decision control statements.
3.2.
14. What is an r-value and l-value?
3.3.
15. What is call by reference in functions?
3.4.
16. What are goto statements in C? Should we use them?
3.5.
17. What are static variables in C?
3.6.
18. What is the difference between continue and break statements in C?
3.7.
19. What is the difference between pre-increment and post-increment in C?
3.8.
20. What do you mean by typedef in C?
3.9.
21. What is the difference between call by value and call by reference?
3.10.
22. What is the difference between getc(), getch(), getche(), and getchar()?
3.11.
23. What is the difference between a union and a structure in C?
3.12.
24. Can we convert a number to a string in C?
3.13.
25. How to convert a string to a number in C?
4.
C Interview Questions for Experienced 
4.1.
26. Explain Dynamic Memory Allocations.
4.2.
27. What is a memory leak in C?
4.3.
28. What is a volatile qualifier in C?
4.4.
29. What is the difference between malloc() and calloc()?
4.5.
30. What is printed by the following C program?
4.6.
31. Differentiate between the macros and the function
4.7.
32. Discuss the concept of function pointers as callback functions in C.
4.8.
33. Discuss the differences between deep copy and shallow copy in C.
4.9.
34. Explain the concept of function pointer arrays in C.
4.10.
35. Explain the concept of multi-threading in C and discuss the associated challenges.
4.11.
36. What are a near pointer and a far pointer in C?
5.
Frequently Asked Questions
5.1.
What is C programming language used for?
5.2.
Why C is called middle-level language?
5.3.
How to prepare for C language interview?
5.4.
Why is C known as a mother language?
6.
Conclusion
Last Updated: Apr 3, 2025
Medium

C Interview Questions

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In 1972, Dennis Ritchie developed the C language. The popularity of C is unaffected despite the emergence of various new languages, including Python, Java, PHP, JavaScript, and many more. Many of these languages' control structures and other core concepts are C-inspired.

C Interview Questions

The features of C make it a distinct and unique language, maintaining its popularity and relevance. IT companies are still interested in employing C professionals. This article is meant to help you with your upcoming C interview questions. This article uncovers the top 25 C interview questions.

C Interview Questions for Freshers

Here is a list of some basic C questions to provide a foundation and help you improve.

1. Mention the most important features of the C language.

Ans: Some of the main features of C language are as follows:

  • Mid-level programming language: C is considered a mid-level language because it supports the features of both low-level and high-level languages. One of the best examples is direct hardware manipulation. Unlike high-level programming languages, C supports direct hardware manipulation.
     
  • Fast: C is a statically typed programming language. This feature provides C with an edge over dynamically typed programming languages. Also, C is a compiler-based language resulting in faster code execution.
     
  • Easy and effective: C is a simple and easy-to-learn language. It is very effective to start your coding journey.
      
  • Extensible: You can easily extend a C program to add new functions to an already written code.
     
  • Portable: C is a portable language that means, based on your requirements, you can use a single code on multiple systems.
     
  • DMA (Dynamic Memory Allocation): You can use and control the data structure's size in runtime in C. Some of the inbuilt features that can help you to perform dynamic memory allocation are calloc(), malloc(), realloc(), free() etc.
     
  • Recursion and pointers: These are one of the main features offered by C. Pointers can directly interact with the memory location they are named for by pointing to it. Recursion helps in code reusability and backtracking.

2. Why is C called a mid-level programming language?

Ans: C is generally called a middle-level programming language as it binds the gap between high-level language and machine-level language. The reason behind this is that C language instructions resemble algebraic expressions along with some English keywords such as if, for, else, etc. In this way, C resembles a high-level language. But, it also has certain low-level features that help coders to perform operations available in machine or assembly language.

3. What is the meaning of #include in a C program?

Ans: The #include in a C program is a file inclusion directive. When you compile your code, the first step is the addition of some predefined functions or codes from reserved header files. The #include command is used to add that predefined functions. Some of the predefined functions are scanf()printf()getch(), etc. 

In other words, the #include statement written at the start of a C program is used to include some user-defined or standard files in your C program.

4. What are the enumerations in C?

Ans: Enumeration, also known as enum, is a user-defined data type in C. The enum in C assigns names to the integer variables resulting in simpler code. One can use the enum keyword to create an enum in C. Defining an enum is the same as declaring a structure. The syntax of the enum is as follows:

enum [enum_name]{
Value 1,
Value 2,
.
.
. 
Value n
};

 

Here is an example of enums in C:

#include<stdio.h>
 
enum year{Jan, Feb, March, April, May, June, July, August, Sep, Oct, Nov, Dec};
 
int main()
{
    enum year month;
    month = June;
    printf("%d", month);
    return 0;
}

 

Output:

5 

5. What are macros? List some advantages of macro over functions.

Ans: The macros in the C programming language are defined using the #define directive. The syntax of macros is as follows:

#define <macro_name> <macro_value>

 

The macro value is substituted for the macro name each time the compiler comes across the macro. In simpler terms, the macro is a name assigned to a macro value. It does not designate a location in memory. Also, each macro in your C program can have a different name.

Some examples of macros:

#define PI 3.14

#define Area(a) a*a

 

Advantages of macro over functions:

  • The macro is easy to use and prevents function call mistakes.
     
  • The macro call speeds up the program.
     
  • It makes the code compact and small.

6. What are format specifiers in C?

Ans: The format specifiers in c are used to instruct the compiler about the type of input and output functions. The format specifiers are used in scanf(), and printf() functions for taking user input and printing output, respectively. The identity of a format specifier is the % symbol.

An example shows various format specifiers in C.

#include <stdio.h> 
int main() { 
// Example of a character format specifier
  char ch = 'C'; 
  printf("%c\n",ch); 
  
//Example of an integer format specifier
  int year = 2023, pre_year = 2022; 
  printf("%d\n", year); 
  printf("%i\n", pre_year);
  
//Example of a string format specifier
  char str[] = "Coding Ninjas!"; 
  printf("%s \n", str); 
  
//Example of a float format specifier
  float num1 = 19.99; 
  printf("%f \n", num1); 
  
 //Example of a hexadecimal integer format specifier 
  int h = 28; 
  printf("%x \n", h); 
  
 //Example of an octal integer format specifier
   int num2 = 31; 
  printf("%o \n", num2); 
  
  return 0; 
}

 

Output

C

2023

2022

Coding Ninjas! 

19.990000 

1c 

37

7. What happens if you don’t use a break statement in a switch case?

Ans: In switch cases in C, the break statement is optional. If you are not using a break statement in a switch case, program execution will continue. All statements after that case statement are executed until a break statement is found.

See the below examples to understand the concept clearly.

Using the break statement:

#include <stdio.h>


int main()
{
        int x = 1;


        switch (x) {
            case 1:
                printf("Coding Ninjas\n");
                break;


            case 2:
                printf("Code Studio Library\n");
                break;


            default:
                printf("No such case");
        }


    return 0;
}

 

Output

Coding Ninjas

 

Without using a break statement:

#include <stdio.h>

int main()
{
        int x = 1;
        switch (x) {
            case 1:
                printf("Coding Ninjas\n");
            case 2:
                printf("Code Studio Library\n");
            default:
                printf("No such case");
        }


    return 0;
}

 

Output:

Coding Ninjas

Code Studio Library

No such case

 

8. Why do we use default statements in switch cases in C?

Ans: The default statements in switch cases are used at the end of all cases. In the absence of any case-expression matches, the default code block is executed.

Here is an example of a default statement:

#include <stdio.h>

int main()
{
        int x = 5;


        switch (x) {
            case 1:
                printf("Coding Ninjas\n");
                break;


            case 2:
                printf("Code Studio Library\n");
                break;
            
            case 3:
                printf("Welcome Ninja!\n");
                break;


            default:
                printf("No such case");
        }


    return 0;
}

 

Output

No such case

It’s time to uncover some medium-level C interview questions.

9. Can you write a running C code without using the main() function?

Ans: One can write a running C program without using the main() function by using the following methods:

a. Using #define directive

Your code is transformed before compilation by the compiler using a microprocessor called the C preprocessor. We don't need to use the main() function when writing a C programme. To do this, we need to employ the preprocessor command #define.

Here is an example:

#include<stdio.h>  
#define CN main  
void CN() 
{  
   printf("Coding Ninjas!!");  
} 

 

Output

Coding Ninjas!!

 

b. Using the token-passing method

When you are not even permitted to use the word main. You can use the token-passing method of the macros. In this method, the ## acts as a concatenation operator. Here is an example:

#include<stdio.h>  
#define CN m##a##i##n  
void CN() 
{  
   printf("Coding Ninjas!!");  
}

 

Output

Coding Ninjas!!

10. What do you mean by Dangling Pointers?

Ans: Let’s first understand what pointers are. In C, the pointers are used to interact with the memory locations. They are used to refer to a variable’s location in the memory. Suppose you have created a pointer referring to a variable X’s location. 

After that, you have deallocated the memory and deleted your variable X, but the pointer still points to that address. Such kinds of pointers are called Dangling pointers.

Here is an example of a Dangling Pointer:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
// Allocating memory location.
    int *p = (int *)malloc(sizeof(int));
 
// After the below free call( memory deallocation), the pointer p becomes a dangling pointer
    free(p);
 
// To handle dangling pointers, assign a NULL value to the pointer.
    p = NULL;
}

11. What is a token?

The individual elements of a program are called Tokens. The following 6 types of tokens are available in C:

  • Identifiers
     
  • Keywords
     
  • Constants
     
  • Operators
     
  • Special Characters
     
  • Strings 

12. What is the use of printf() and scanf() functions? Also, explain format specifiers.

The printf() function is used to display output, while scanf() is used to read formatted input. Here are some common format specifiers:

  • %d: Used for printing and scanning integer values.
     
  • %s: Used for printing and scanning strings.
     
  • %c: Used for displaying and scanning characters.
     
  • %f: Used to print and scan float values.

C Interview Questions for Intermediate 

13. Specify different types of decision control statements.

In C programming, control statements manage the flow of execution based on conditions. The main types include:

  • If-else: Executes code based on a true or false condition.
     
  • Else-if: Checks multiple conditions in sequence.
     
  • Nested if-else: Uses if-else within another if-else for complex decision-making.
     
  • Switch: Selects one of many code blocks to execute based on a specific value.
     

These control structures allow for dynamic decision-making in programs.

14. What is an r-value and l-value?

r-value:

  • Refers to a data value stored at a specific memory address.
     
  • Can only appear on the right side of an assignment operator (=).
     
  • Cannot have a value assigned to it.

l-value:

  • Represents a memory location identifying an object.
     
  • It can appear on both the left and right sides of an assignment operator (=).
     
  • Often used as an identifier.

15. What is call by reference in functions?

  • Call by Reference passes the addresses of the actual parameters to the function.
     
  • Changes made to the formal parameters affect the actual parameters.
     
  • Both the formal and actual parameters share the same memory location.
     
  • This allows direct modification of the actual parameters within the function.

16. What are goto statements in C? Should we use them?

Ans: The goto statement is a jump statement, often known as an unconditional jump statement. We can move program control to the designated label using the goto statement. You can jump from one label to another within a function using the goto statement. 

The syntax of goto statements:

goto label;
... .. ...
... .. ...
label: 
/* do something */

 

Goto statements are rarely helpful; you can write any C program without them. You can use the goto statement if it simplifies your code. Goto statements are helpful in some cases, for example, to break out nested loops. 

17. What are static variables in C?

Ans: Static variables can retain their actual values even after they quit their scope. You can declare static variables in C using the static keyword. When a function call, where the variable was defined, is completed, an auto or normal variable is destroyed. When the program runs, a static variable is still in memory for various function calls.

In other words, while a static variable's value is retained between function calls, the "static" keyword can only initialise a variable once.

Here is an example of a static variable in C:

#include<stdio.h>
int fun1()
{
  static int count = 0;
  count++;
  return count;
}


int fun2()
{
  int count = 0;
  count++;
  return count;
}
  
int main()
{
  printf("Example of static variable\n");
  printf("%d \n", fun1());
  printf("%d \n", fun1());
  
  printf("Example of auto variable\n");
  printf("%d \n", fun2());
  printf("%d", fun2());
  
  return 0;
}

 

Output

Example of static variable

Example of auto variable

1

18. What is the difference between continue and break statements in C?

Ans: The difference between continue and break statements are as follows:

Key Points

Continue

Break

Working

You can use Continue statements in loops, such as while loop, for loop, and do while loop. It is not used with switch cases. Break statements are commonly used with switch statements, but one can also use them in loops.

Syntax

continue;break;

Use

The break statement immediately terminates the enclosing switch case.The continue statement starts the next iteration of the loop.

Loop Construct

This statement does not enable a user to exit a loop structure.This statement enables a user to exit an entire loop structure.

19. What is the difference between pre-increment and post-increment in C?

Ans: The increment operator(++) in C is used to increase the value of a variable by one. There are two types of increment operators:

  • Pre-increment operator.
     
  • Post-increment operator.
     

The difference between pre-increment and post-increment is as follows:

Key Points

Pre-increment 

Post-increment

Basic definition

The pre-increment operator increments the variable's value before it is used in any expression.After using the variable's value in any expression, the post-increment operator increments the variable's value.

Syntax

Y = ++X;Y = X++;

Speed

It is faster than the post-increment operator.It is slower than the pre-increment operator.

Associativity

Right to left.Left to right.

 

Example of pre-increment:

#include<stdio.h>
int main(){
	int x = 4;

	//pre-increment operator will increase the value of 'x' then assign
	//it to 'y'.
	int y = ++x;
	printf("x = %d\ny = %d\n", x,y);
	return 0;
}

 

Output:

x = 5

y = 5

Example of post-increment:

#include<stdio.h>
int main(){
	int x = 4;
	//post-increment operator will assign the value of 'x' to y then
	//increment it.
	int y = x++;
	printf("x = %d\ny = %d\n", x,y);
	return 0;
}

 

Output:

x = 5

y = 4

20. What do you mean by typedef in C?

Ans:  The typedef keyword is used in C language to give current variables in a programme some meaningful names. Typedef is used with user-defined datatypes when it becomes tricky to use the names of datatypes in programmes.

In simple terms, we can state that the typedef keyword is used to change the name of a variable that already exists. It functions identically to create a command alias.

The syntax of typedef in C is as follows:

typedef <existing_variable_name> <alias_name>

 

Here is a simple example of the typedef keyword in C:

#include <stdio.h>  
int main()  
{  
	typedef unsigned int ui;  
	ui x,y;  
	x=4;  
	y=5;  
	printf("The value of x is %d",x);  
	printf("\n The value of y is %d",y);  
	return 0;  
}

 

Output:

The value of x is 4

The value of y is 5

21. What is the difference between call by value and call by reference?

Ans: The difference between call by value and call by reference is as follows:

Call by value:

  • In call by value, we pass variables' values to functions when we call them. 
     
  • In call by value method:
    • Actual parameter values are copied to the formal parameters of the function.
       
    • Both types of parameters are stored in separate parts of memory.
       
  • The values of the actual variables in the calling function are unaffected by changes made to the call function's formal variables.
     

Example of call by value method:

#include <stdio.h>

void swap_function(int a, int b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
    printf("a=%d b=%d\n", a, b);
}


// Main function
int main()
{
    int x = 5, y = 6;
    swap_function(x, y);
    printf("x=%d y=%d\n", x, y);
    return 0;
}

 

Output:

a=6 b=5

x=5 y=6

 

Call by reference:

  • In call by value, instead of passing variables' values,  we pass variables' addresses to functions when we call them. 
     
  • In the call-by-reference method, both types of parameters refer to the same memory location.
     
  • The values of the actual variables in the calling function are affected by changes made to the call function's formal variables.
     

Example of call by reference method:

#include <stdio.h>
void swap_function(int*a, int*b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    printf("a=%d b=%d\n", *a, *b);
}

// Main function
int main()
{
    int x = 5, y = 6;
    swap_function(&x, &y);

    printf("x=%d y=%d\n", x, y);
    return 0;
}

 

Output:

a=6 b=5

x=6 y=5

22. What is the difference between getc(), getch(), getche(), and getchar()?

Ans: The key difference between getc(), getch(), getche(), and getchar() is as follows:

  • getc(): The getc() function reads one character from any input.
    Successful Operation: Returns the ASCII value of that character.
    Failure: EOF(End of file).
    The syntax of the getc() method is as follows:
     

int getc(FILE *stream);

 

  • getch(): It also reads one character from any input. It doesn’t use any buffer. Therefore, the entered character returns immediately.
    The syntax of the getch() method is as follows:
     

int getch();

 

  • getche(): The conio.h file contains the getche(), another non-standard type of function. The getche() can read one character from any available keyboard. It uses a buffer but also reflects the output immediately on the screen.
    The syntax of the getch() method is as follows:
     

int getche(void);

 

  • getchar(): The getchar() function can only read from the standard input. The getchar() method is similar to the getc(stdin).
    The syntax of the getchar() method is as follows:
     

int getchar(void);

 

Here are some advanced-level interview questions for your interview preparation.

23. What is the difference between a union and a structure in C?

Ans: The differences between a union and a structure are as follows:

Key Points

Union

Structure

Definition

In C, a union is a user-defined data type. It can be used to store different data types in the same memory location.In C, a structure is a user-defined data type. It can hold members of various data types.

Keyword

The union keyword is used.The struct keyword is used.

Size

Its size is set to be the same as that of the largest data member.It requires a structure's size to be greater than or equal to the total size of its data members.

Syntax

union [name of your union]

{

  element_1;

  element_2;

  .

  .

  element_k;

} variable_1, variable_2, …;

struct [name of your structure]

{

  element_1;

  element_2;

  .

  .
element_k;

} variable_1, variable_2, …;

Initialization

A user can only initiate the first member of a Union at a time.A user can initialise several members of a Structure simultaneously.

Value Alteration

The values of other members are affected when you change the values of a single member.The values of other members are unaffected when you change the values of a single member.

Example

union CN {

   char name[30];

   int id;

   string article;  

};

 

struct CN {

   char name[30];

   int id;

   string article;  

};

24. Can we convert a number to a string in C?

Ans: Yes, you can convert a number to a string in C. The sprintf() function is used to perform that conversion. The syntax of the sprintf() function is as follows:

int sprintf(char* str, const char* format_string, argument1, argument2,..)

Here is the code showing the sprintf() function.

#include<stdio.h>
int main()
{
    char str[20];
    float number = 22.32;
    
    sprintf(str, "%f", number);
    printf("\n The resultant string for the given number is %s", number);
    puts(str);
    
    return 0;
}

 

Output:

The resultant string for the given number is 22.320000

25. How to convert a string to a number in C?

Ans: You can convert a string to a number in C using the atoi() function. The syntax of the atoi() function is as follows:

int atoi(const char* string);

Input: 

String of characters.

Return value: 

Numeric value of desired return type.

At the first character that it cannot identify as a component of a number, the function stops reading the input string. For example, the NULL character at the end of a string.

Here is the code showing the working of atoi() function.

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
    int num;  
    char str1[20] = "22345343";  
    num = atoi(str1);  
    printf("The string value is: %s\n", str1); 
    printf("The resultant integer value is: %d\n", num); 
    
    printf("-----------------------------------------------\n");
    
    char str2[20] = "Coding Ninjas"; 
    num = atoi(str2); 
    printf("The string value is: %s\n", str2); 
    printf("The resultant integer value is: %d\n", num); 
 
}

 

Output:

The string value is: 22345343
The resultant integer value is: 22345343
-----------------------------------------------
The string value is: Coding Ninjas
The resultant integer value is: 0

C Interview Questions for Experienced 

26. Explain Dynamic Memory Allocations.

Ans: Dynamic Memory Allocation(DMA) is one of the most important features of C. It enables the user to allocate memory locations at runtime. An example of DMA is the memory allocation of a linked list. Using the DMA property, one can increase the memory allocation during execution.

The <stdlib.h> header file is used for DMA in C language. The four predefined library functions used for DMA in C are as follows:

  • malloc(): You can allocate a single block of the specified memory using malloc().
     
  • calloc(): You can allocate a number of required memory blocks(let’s suppose N) using calloc().
     
  • free(): One can use the free() method to deallocate the memory blocks after program execution.
     
  • realloc(): One can reallocate the memory that is previously allocated by malloc() or calloc() using the realloc().
     

Here is an example showing DMA:

#include <stdio.h>
#include <stdlib.h>

int main() {
  int n;
  // ptr will point to the base address
  int * p;

  // User input
  printf("Enter your number of elements:");
  scanf("%d", & n);

  // DMA using malloc()
  p = (int * ) malloc(n * sizeof(int));

  // Checking for memory allocation
  if (p == NULL) {
    printf("Error: Unable to allocate specified memory.\n");
    exit(0);
  } 
  else {
    // successful operation
    printf("Memory successfully allocated using malloc.\n");
    printf("Entered number of elements by you: %d\n", n);
    for (int i = 0; i < n; i++) {
      p[i] = i + 1;
    }
    // Print the elements of the array
    printf("The array elements are ");
    for (int i = 0; i < n; i++) {
      printf("%d, ", p[i]);
    }
  }
  free(p);
  return 0;
}

 

Output:

Enter your number of elements:7

Memory successfully allocated using malloc.

Entered number of elements by you: 7

The array elements are 1, 2, 3, 4, 5, 6, 7,

27. What is a memory leak in C?

Ans: Suppose you have allocated the memory dynamically and forget to free that memory. The program no longer uses that memory, but the space remains occupied. This will result in a memory leak problem. It is extremely harmful when the servers and daemons are included in the program.

Some of the important causes of memory leaks are as follows:

a. When we don’t free the memory.
 

b. If the pointer is given a distinct value before being freed.
See the code snippet below:

// DMA 
int* p = (int*)malloc(sizeof int);


// Before executing the free function, assign null to the pointer.
p = NULL;
//Results in a memory leak.

 

c. When the pointer goes out of scope.

Void function(){
int* p = (int*)malloc(sizeof int);
//Once control exits the scope, the local pointer becomes invalid.
}
//Results in a memory leak.

28. What is a volatile qualifier in C?

Ans: The volatile qualifier is applied when declaring a variable in a C program. 

The volatile keyword prevents the compiler's optimisations from being applied to objects that are marked as volatile. It informs the compiler that the value declared as volatile is mutable.

When an object is marked as "volatile," memory registers rather than cache variables are used to retrieve its value. Any object marked as volatile is vulnerable to an abrupt change in value from the compiler, even if there hasn't been any work done on that object within the program.

Volatile qualifiers are generally used in multithreaded programs. Some important features of volatile qualifiers are as follows:

  • The volatile keyword cannot revoke the memory designation.
     
  • The sequence of assignments does not affect the value.
     
  • The values in the register cannot be cached.

29. What is the difference between malloc() and calloc()?

Ans: Both malloc() and calloc() are used in Direct Memory Allocation. The difference between malloc() and calloc() are as follows:

Key Points

malloc()

calloc()

Number of blocks

One can allocate a single block of a specified size.The calloc() function is used to allocate different blocks of memory.

Number of arguments

OneTwo

Syntax

ptr = (int*) malloc(100 * sizeof(int));ptr = (int*) calloc(25, sizeof(int));

Garbage value

Garbage value is present in allocated memory.The allocated memory contains Zero.

Time Efficiency

Time efficiency is high.Time efficiency is low.

Secure

It’s not secure.It’s secure.

30. What is printed by the following C program?

#include <stdio.h>
int func(int, int*, int**);
  
void main()
{
   int p, *q, **r;
   p = 5; 
   q = &p;
   r = &q;  
   printf( "%d", func(p,q,r));
   getchar();
}


int func(int x, int *py, int **pz)
{ 
  int y, z; 
  **pz += 1;  
   z  = **pz; 
  *py += 2; 
   y = *py;
   x += 3;
   return x + y + z;
}

 

Ans: The output of the above code is 22.

Explanation: The code  **pz += 1 sets the value of p to 6 because x(dummy variable) is a copy of p and has a distinct address from p. The value x is unaffected by this change.

When we perform z  = **pz, the value of z becomes 6. 

The code  *py += 2 sets the value of p as 8( 6 + 2).

Using y = *py, the value of y becomes 8.

Now, by using the code x += 3, the value of x is incremented by 3, resulting in a value of 8.

Finally, x+y+z will be 8+8+6, which is equal to 22. Therefore the output value of the above-mentioned code is 22.

31. Differentiate between the macros and the function

AspectMacrosFunctions
CompilationPreprocessedCompiled
Code LengthIncreases code lengthRemains the same
Execution SpeedFaster executionSlower execution
Use CaseUseful for small code snippets reused frequentlyBest for larger repetitive code blocks

32. Discuss the concept of function pointers as callback functions in C.

Ans: Function pointers can be used as callback functions in C to enable dynamic behavior and function invocation. By passing a function pointer as an argument to another function, that function can then call the callback function at a later time, allowing for customisable and flexible program behaviour.

33. Discuss the differences between deep copy and shallow copy in C.

Ans: A Deep copy in C ensures that each object has its own independent memory by making a distinct copy of dynamically allocated memory. In contrast, a shallow copy makes a fresh reference to the existing memory, allowing many objects to share the same memory. A deep copy needs to be explicitly implemented, but a shallow copy can be accomplished with a straightforward assignment.

34. Explain the concept of function pointer arrays in C.

Ans: Function addresses are kept in function pointer arrays. They enable the dynamic selection and invocation of functions based on runtime environments. Different functions can be called by indexing the array with the proper values, allowing for flexible algorithmic choices and program behaviour.

35. Explain the concept of multi-threading in C and discuss the associated challenges.

Ans: Multi-threading in C involves creating and managing multiple concurrent threads of execution within a program. It allows for parallelism and improved performance. However, challenges such as thread synchronisation, shared resource access, and race conditions need to be carefully handled to ensure thread safety and avoid unexpected behaviour.

36. What are a near pointer and a far pointer in C?

Ans: Near pointer in C is a pointer that works within the range of 64Kb data segment of memory. This means that the near pointer is used to store the 16-bit addresses; they can only reach the memory addresses within the ongoing segment on a 16-bit machine. The size of the near pointer is 2 bytes.

A far pointer in C is a 32-bit pointer that can access information that is outside the computer memory in a given segment. The compiler creates a segment register for the segment address and another register for the offset within the current segment. In the far pointer, the segment part cannot be changed as incrementing/decrementing; it only changes the offer but not the segment address. The size of the far pointer is 4 bytes.

Also read reverse a number.

Frequently Asked Questions

What is C programming language used for?

C programming is widely used for developing system software, application software, and embedded systems. It is pretty straightforward, and provides a powerful interface for programming languages.

Why C is called middle-level language?

C is called middle-level language as it actually binds the gap between a machine-level language and a high-level language. A programmer can use C language for Application programming as well as for System Programming.

How to prepare for C language interview?

Review the foundational ideas, do coding activities, and respond to sample questions to prepare for a C language interview. Revisit concepts like pointers, arrays, memory management, and data structures. Additionally, become acquainted with standard C programming idioms and good practices.

Why is C known as a mother language?

C is known as the "mother language" since it served as the model for the creation of numerous other programming languages. It is effective and versatile because it offers a straightforward syntax, low-level memory access, and a small runtime environment.

Conclusion

In this article, we have discussed the top 30 most asked C interview Questions. We looked at questions of different categories: beginner, intermediate and advanced. We also looked at some frequently asked questions. 

Recommended Readings:

Live masterclass