Search Jobs

Ticker

6/recent/ticker-posts

C Interview Question

Top 50 commonly asked interview questions in C language along with answers and examples:


1. What is C?

   - C is a high-level programming language known for its simplicity and powerful features, developed by Dennis Ritchie.


2. What is the difference between printf and scanf ?

   - printf is used to print output, while scanf is used to take input. Example:

 

   printf("Hello, World!");

   int x;

   scanf("%d", &x);

  


3. Explain the difference between auto and static variables.

   - auto variables are local and have a scope limited to the block they are declared in, while

static variables retain their value between

function calls. Example:

 

   void func() {

    auto int a = 10;

    static int b = 5;

   }

  


4. What is the use of const in C?

   - const is used to declare constant values that cannot be modified. Example:

 

   const int pi = 3.14;

  


5. Explain the difference between break

and continue.

   - break is used to exit from loops, while continue

is used to skip the current iteration and continue with the next one. Example:

 

   for (int i = 1; i <= 5; i++) {

    if (i == 3) {

        break; // Exits the loop when i equals 3.

    }

    if (i == 2) {

        continue; // Skips the current

iteration when i equals 2.

    }

   }

  


6. What is a pointer in C?

   - A pointer is a variable that stores the memory address of another variable. Example:

 

   int x = 10;

   int *ptr = &x; // ptr stores the address of x.

  


7. Explain the use of malloc and free.

   - malloc is used to dynamically allocate memory, and free is used to deallocate memory. Example:

 

   int *arr = (int *)malloc(5 * sizeof(int));

   // Use arr...

   free(arr);

  


8. What is a structure in C?

   - A structure is a user-defined data type that groups related data members together. Example:

 

   struct Student {

    char name[20];

    int age;

   };

  


9. Explain the difference between strcpy and strcat.

   - strcpy is used to copy one string into another, while strcat is used to concatenate two strings. Example:

 

   char dest[20], src[10];

   strcpy(dest, "Hello");

   strcat(dest, " World");

  


10. What is recursion in C?

- Recursion is a technique where a function calls itself. Example (Factorial):

  

int factorial(int n) {

     if (n == 0) return 1;

     return n * factorial(n - 1);

}


11. What is the purpose of the switch statement?

- The switch statement is used to select one of many code blocks to be executed. Example:

  

switch (day) {

     case 1:

         printf("Sunday");

         break;

     case 2:

         printf("Monday");

         break;

     // ...

     default:

         printf("Invalid day");

}


12. What are preprocessor directives in C?

- Preprocessor directives are commands to the C preprocessor that modify the source code before actual compilation. Example (#include):

  

#include <stdio.h>

 

13. What is the typedef keyword used for?

   - typedef is used to create custom data types in C. Example:

  

typedef int Length;

Length x = 10; // x is of type int.


14. Explain the use of enum in C.

- enum is used to create a set of named integer constants. Example:

  

enum Days { Sunday, Monday, Tuesday, Wednesday,

Thursday, Friday, Saturday };


15. How can you reverse a string in C?

- You can reverse a string by using a loop. Example:

  

void reverseString(char *str) {

     int len = strlen(str);

     for (int i = 0, j = len - 1; i < j; i++, j--) {

         char temp = str[i];

         str[i] = str[j];

         str[j] = temp;

     }

}


16. What is the purpose of the sizeof operator in C?

   - The sizeof operator is used to determine the size (in bytes) of a data type or a variable. Example:

 

   int size = sizeof(int); // size contains

the size of an int.

  


17. Explain the concept of a pointer to a function in C.

   - A pointer to a function is a variable that stores the address of a function. It can be used to call functions dynamically. Example:

 

   int add(int a, int b) {

    return a + b;

   }


   int (*funcPtr)(int, int) = add;

   int result = funcPtr(2, 3); // Calls the add function

through the pointer.

  


18. What is the ternary operator in C, and how is it used?

   - The ternary operator ( ? : ) is a shorthand way to write an if-else statement in a single line. Example:

 

   int x = 10;

   int y = (x > 5) ? 20 : 30; // If x is greater

than 5, y is set to 20; otherwise, it's set to 30.

  


19. Explain the use of typedef with structures in C.

   - typedef can be used to create a custom type for a structure, simplifying its usage. Example:

   typedef struct {

    char name[20];

    int age;

   } Person;


   Person p1;

  


20. What is the purpose of the union in C?

- A union is a data structure that allows different data types to be stored in the same memory location. Example:

  

union Value {

     int intValue;

     float floatValue;

};


union Value v;

v.intValue = 5;


21. What is a file pointer in C, and how is it used for file operations?

- A file pointer is a variable used to control file operations such as reading, writing, and seeking in files. Example (reading from a file):

  

FILE *file = fopen("example.txt", "r");

char buffer[100];

fgets(buffer, sizeof(buffer), file);

fclose(file);


22. Explain the const pointer in C.

   - A const pointer is a pointer that points to a constant value. It can't be used to modify the value it points to. Example:

  

int value = 10;

const int *ptr = &value; // Cannot modify *ptr.


23. What is the purpose of the volatile keyword in C?

   - The volatile keyword is used to indicate that a variable's value may change unexpectedly, and the compiler should not optimize accesses to it. Example:

  

volatile int sensorValue;


24. Explain the #define directive in C.

- #define is used to create macros or symbolic constants in C. It's a way to replace text in your code with defined values. Example:

  

#define PI 3.14159265359


25. How do you dynamically allocate a 2D array in C?

- To dynamically allocate a 2D array, you can use a pointer to a pointer (double pointer). Example:

  

int rows = 3, cols = 4;

int **matrix = (int **)malloc(rows * sizeof(int *));

for (int i = 0; i < rows; i++) {

     matrix[i] = (int *)malloc(cols * sizeof(int));

}


26. Explain the difference between call by value and call by reference in C.C.**

- Call by value passes the value of a variable to a function, while call by reference passes the memory address of the variable. Changes to the parameter in a call by reference function affect the original variable. Example:

  

void callByValue(int x) {

     x = 20; // Changes won't affect the original

value.

}


void callByReference(int *x) {

     *x = 20; // Changes will affect the original

value.

}


27. What is a linked list in C, and how is it implemented?d?**

- A linked list is a data structure consisting of nodes, where each node contains data and a reference to the next node. Example (singly linked list):

  

struct Node {

     int data;

     struct Node *next;

};

   

20 more commonly asked interview questions in C language with answers and examples:


31. Explain what NULLPointer is in C.

   - AN ULLp ointer is a pointer that does not point to any memory location. It's often used to indicate the absence of a valid pointer.

 

   int *ptr = NULL;

  


32. What is the purpose of thee xit() function in C?

   - Thee xit()f unction is used to terminate a program and return an exit status to the operating system. It is typically used to indicate the success or failure of a program's execution.

 

   exit(0); // Indicates a successful program termination.

  


33. Explain the purpose of the const pointer in C.

   - A const pointer is a pointer whose value cannot be changed, meaning it always points to the same memory location.

 

   int x = 10;

   const int *ptr = &x; // Cannot change the address pointed to by ptr.

  


34. What are local and global variables in C?

   - Local variables are defined within a function and have local scope, while global variables are defined outside of any function and have global scope.

 

   int globalVar = 5; // Global variable

   void myFunction() {

    int localVar = 10; // Local variable

   }

  


35. Explain the purpose of the static keyword for functions and variables in C.

   - The static keyword for functions limits the scope of a function to the file in which it's defined. For variables, it makes them retain their values between function calls.

 

   static int count = 0; // Retains its value between function calls.

   static void localFunction() {

    // This function is visible only within this file.

   }

  


36. How is a character array different from a string in C?

   - A character array is a collection of characters, while a string in C is a character array terminated by a null character (

'\0'

) that signifies the end of the string.

 

   char charArray[] = {'H', 'e', 'l', 'l', 'o'}; // Character array

   char str[] = "Hello"; // String

  


37. What is the difference between fprintf and fscanf

in C for file I/O?

   -fprintf iis used to write data to a file, and

fscanf is used to read data from a file.

 

   FILE *file = fopen("data.txt", "w");

   fprintf(file, "Hello, World!");

   fclose(file);


   FILE *file = fopen("data.txt", "r");

   char buffer[100];

   fscanf(file, "%s", buffer);

   fclose(file);

  


38. Explain the purpose of const pointer to const

variable in C.

   - A const pointer to a const variable is a pointer that cannot be used to modify the value it points to, and it cannot be pointed to a different memory location.

 

   const int value = 42;

   const int *const ptr = &value;

  


39. What is the purpose of the sizeof

operator in C?

   - The sizeof operator is used to determine the size (in bytes) of a data type or a variable.

 

   int size = sizeof(int); // `size` contains the size of an int.

  


40. Explain the #include directive and its use in C.

   - The #include Directive is used to include header files in a C program, allowing you to use external functions and declarations.

 

   #include <stdio.h>

  


41. What is a memory leak in C, and how can it be avoided?

   - A memory leak occurs when memory is allocated but not deallocated. It can be avoided by using

free

to release dynamically allocated memory when it's no longer needed.

 

   int *arr = (int *)malloc(10 * sizeof(int));

   // Use `arr`...

   free(arr); // Release the allocated memory.

  


42. Explain the use of the volatile keyword in C.

   - The volatile keyword is used to indicate that a variable's value may change unexpectedly, and the compiler should not optimize accesses to it.

 

   volatile int sensorValue;

  





Post a Comment

0 Comments