C Interview Questions

These 100 C programming interview questions and answers cover a wide range of topics and concepts in C programming. They should help you prepare for your C interview and showcase your knowledge and problem-solving skills.

1. What is C programming, and why is it popular?

Answer: C is a general-purpose programming language known for its simplicity, efficiency, and portability. It’s popular because it allows low-level memory manipulation and provides high-level abstractions, making it suitable for a wide range of applications.

2. Explain the difference between malloc() and calloc() in C.

Answer: malloc() allocates memory but doesn’t initialize it, while calloc() allocates and initializes memory to zero.

3. What is a pointer in C?

Answer: A pointer is a variable that stores the memory address of another variable. It allows indirect access to the value of the variable it points to.

4. How is memory allocated for a variable in C?

Answer: Memory can be allocated for a variable in C using either stack memory (automatic allocation) or heap memory (dynamic allocation using malloc() or calloc()).

5. Explain the difference between const and volatile qualifiers in C.

Answer: const is used to declare constants, indicating that a variable’s value cannot be modified. volatile indicates that a variable may be changed by external factors, so it should not be optimized by the compiler.

6. What is a function pointer in C, and how is it used?

Answer: A function pointer is a pointer that points to a function instead of a data variable. It allows dynamic function invocation and is often used for callbacks and function tables.

7. Describe the purpose of the #include directive in C.

Answer: The #include directive is used to include header files in a C program. It allows access to external functions and declarations defined in those header files.

8. How is a structure declared and used in C?

Answer: A structure in C is declared using the struct keyword and allows the grouping of variables of different data types into a single unit. It is used to create user-defined data types.

9. What is the difference between ++i and i++ in C?

Answer: Both ++i and i++ increment i by 1, but ++i increments before the current value is used, while i++ increments after the current value is used in an expression.

10. Explain the role of the main() function in a C program.

Answer: The main() function is the entry point of a C program. It is where program execution begins and is required in every C program. It returns an integer value to the operating system indicating the program’s exit status.

11. How do you pass arguments to a C function?

Answer: Arguments can be passed to a C function by value or by reference (using pointers). By value means a copy of the argument’s value is passed, while by reference means the memory address of the argument is passed.

12. What is recursion in C, and when should it be used?

Answer: Recursion in C is the technique of a function calling itself. It is used when a problem can be broken down into smaller, similar subproblems, and it simplifies code in certain cases.

13. How do you open and close a file in C?

Answer: To open a file in C, you use the fopen() function, and to close a file, you use the fclose() function. You should check for errors when opening files.

14. What are pointers to functions in C, and when are they used?

Answer: Pointers to functions are variables that store the memory address of a function. They are used for dynamic function dispatch, callback mechanisms, and function tables.

15. Explain the purpose of the break and continue statements in C.

Answer: The break statement is used to exit from a loop or switch statement prematurely. The continue statement is used to skip the remaining code inside a loop iteration and continue to the next iteration.

16. What is the difference between NULL and '\0' in C?

Answer: NULL is a pointer that points to no memory location, often used for pointer initialization. '\0' is the null character used to terminate strings in C.

17. What is the difference between scanf() and printf() in C?

Answer: scanf() is used to read input from the user or a file, while printf() is used to display output on the screen or write to a file.

18. How do you dynamically allocate memory for an array in C?

Answer: You can dynamically allocate memory for an array in C using the malloc() function. For example, int* arr = (int*)malloc(n * sizeof(int)); allocates memory for an integer array of size n.

19. Explain the sizeof operator in C.

Answer: The sizeof operator returns the size (in bytes) of a data type or variable. It is often used for memory allocation and buffer sizing.

20. What is a macro in C, and how is it defined?

Answer: A macro in C is a preprocessor directive that defines a symbol or constant. It is defined using the #define directive, and its occurrences are replaced by their values during preprocessing.

21. What is a union in C, and how is it different from a structure?

Answer: A union in C is similar to a structure but uses the same memory location for all its members. Only one member can be active at a time, unlike a structure where all members have their separate memory.

22. How do you pass an array to a function in C?

Answer: You can pass an array to a function in C by specifying the array name as a parameter. It is passed by reference, so changes made to the array inside the function affect the original array.

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

Answer: The volatile keyword in C is used to indicate that a variable can be modified by external factors outside the program’s control. It prevents the compiler from optimizing accesses to that variable.

24. Explain the register keyword in C.

Answer: The register keyword in C is used to suggest that a variable should be stored in a CPU register for faster access. The compiler may or may not honor this suggestion.

25. How do you define a constant in C?

Answer: Constants in C are defined using the const keyword. For example, const int max_value = 100; defines a constant integer with a value of 100.

26. What is the purpose of the static keyword in C?

Answer: The static keyword in C can be used to specify storage duration, scope, and linkage of variables and functions. It can also make a variable persist across function calls.

27. Explain the purpose of the typedef keyword in C.

Answer: The typedef keyword is used to create user-defined data types or aliases for existing data types. It improves code readability and portability.

28. What is a header file in C, and why is it used?

Answer: A header file in C contains declarations of functions, variables, and macros that can be included in multiple source files. It promotes code modularization and reusability.

29. How do you compare two strings in C?

Answer: You can compare two strings in C using the strcmp() function. It returns 0 if the strings are equal, a negative value if the first string is lexicographically smaller, and a positive value if the first string is lexicographically larger.

30. Explain the difference between #include and #include <> in C.

Answer: #include "filename" is used to include a user-defined header file, and #include <filename> is used to include a system header file. The preprocessor searches for system header files in standard directories.

31. What are the bitwise operators in C, and how are they used?

Answer: Bitwise operators (&, |, ^, ~, <<, >>) in C are used to manipulate individual bits in integers. They are often used in low-level operations and bit masking.

32. How is memory deallocated in C?

Answer: Memory allocated using malloc(), calloc(), or realloc() should be deallocated using the free() function to avoid memory leaks.

33. What is a pointer-to-pointer in C, and why is it used?

Answer: A pointer-to-pointer (double pointer) in C is a pointer that points to another pointer. It is often used for dynamic memory allocation and passing pointers by reference.

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

Answer: A const pointer in C is a pointer that cannot be used to modify the value it points to. It ensures that the data it references remains constant.

35. How do you define an array of pointers in C?

Answer: To define an array of pointers in C, you declare an array where each element is a pointer to a specific data type. For example, int* arr[5]; defines an array of five integer pointers.

36. What is the purpose of the #ifdef and #ifndef directives in C preprocessor directives?

Answer: #ifdef checks if a macro is defined, and #ifndef checks if a macro is not defined. They are used for conditional compilation.

37. Explain the enum data type in C.

Answer: An enum in C is a user-defined data type used to define a set of named integer constants. It improves code readability by giving names to integral values.

38. How do you declare a constant pointer in C?

Answer: To declare a constant pointer in C, you use the const keyword before the pointer’s data type. For example, const int* ptr; declares a constant pointer to an integer.

39. What is the difference between static and extern variables in C?

Answer: static variables have local scope and static storage duration within a function or file, while extern variables have global scope and can be accessed across multiple files.

40. Explain the purpose of the volatile keyword in C.

Answer: The volatile keyword in C is used to indicate that a variable may change its value at any time outside the program’s control. It prevents the compiler from optimizing accesses to that variable.

41. How are multidimensional arrays implemented in C?

Answer: Multidimensional arrays in C are implemented as arrays of arrays. For example, a 2D array is an array of 1D arrays, and a 3D array is an array of 2D arrays.

42. What is the purpose of the inline keyword in C?

Answer: The inline keyword suggests that a function should be expanded in place by the compiler, instead of generating a function call. It can improve performance for small functions.

43. Explain the purpose of the restrict keyword in C.

Answer: The restrict keyword in C is a hint to the compiler that a pointer does not alias (point to) any other pointer. It can help the compiler optimize code.

44. What is a function prototype in C, and why is it used?

Answer: A function prototype in C is a declaration of a function’s name, return type, and parameters before the function’s actual definition. It informs the compiler about the function’s interface and is used for type checking.

45. What is the difference between a NULL pointer and a void pointer in C?

Answer: A NULL pointer is a pointer that doesn’t point to any memory location. A void pointer (void*) is a generic pointer that can point to any data type but lacks type information.

46. How do you swap two variables in C without using a temporary variable?

Answer: You can swap two variables in C without using a temporary variable using bitwise XOR operations or arithmetic operations. For example:

cCopy code

a = a + b; b = a - b; a = a - b;

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

Answer: The sizeof operator in C returns the size (in bytes) of a data type or variable. It is often used for memory allocation and buffer sizing.

48. Explain the difference between malloc() and free() in C.

Answer: malloc() is used to dynamically allocate memory, while free() is used to deallocate or release memory previously allocated using malloc(). It helps prevent memory leaks.

49. How do you use the const keyword in C?

Answer: The const keyword is used to declare constants, indicating that a variable’s value cannot be modified. It can be applied to variables, function parameters, and function return values.

50. What is the purpose of the goto statement in C, and when should it be used?

Answer: The goto statement in C is used for unconditional branching to a labeled statement within the same function. It is generally discouraged due to its potential to create unstructured code and make code harder to understand and maintain.

51. Explain the purpose of the #pragma directive in C.

Answer: The #pragma directive in C is used to provide hints, instructions, or settings to the compiler. It is compiler-specific and can be used for various purposes, such as optimization or configuration.

52. What is the difference between a stack and a heap in memory allocation in C?

Answer: In C, the stack is used for local variables and function call management, while the heap is used for dynamic memory allocation using functions like malloc() and calloc(). Stack memory is limited and has a fixed size, while heap memory is more flexible but requires manual memory management.

53. How is a structure different from a union in C?

Answer: A structure in C is used to group variables of different data types into a single entity, and each member has its memory location. A union, on the other hand, shares memory locations for its members, allowing only one member to be active at a time.

54. Explain the purpose of the sizeof operator in C.

Answer: The sizeof operator in C returns the size (in bytes) of a data type or variable. It is often used for memory allocation and buffer sizing.

55. What is the difference between a NULL pointer and a void pointer in C?

Answer: A NULL pointer is a pointer that doesn’t point to any memory location. A void pointer (void*) is a generic pointer that can point to any data type but lacks type information.

56. How do you swap two variables in C without using a temporary variable?

Answer: You can swap two variables in C without using a temporary variable using bitwise XOR operations or arithmetic operations. For example:

cCopy code

a = a + b; b = a - b; a = a - b;

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

Answer: The sizeof operator in C returns the size (in bytes) of a data type or variable. It is often used for memory allocation and buffer sizing.

58. Explain the difference between malloc() and free() in C.

Answer: malloc() is used to dynamically allocate memory, while free() is used to deallocate or release memory previously allocated using malloc(). It helps prevent memory leaks.

59. How do you use the const keyword in C?

Answer: The const keyword is used to declare constants, indicating that a variable’s value cannot be modified. It can be applied to variables, function parameters, and function return values.

60. What is the purpose of the goto statement in C, and when should it be used?

Answer: The goto statement in C is used for unconditional branching to a labeled statement within the same function. It is generally discouraged due to its potential to create unstructured code and make code harder to understand and maintain.

61. Explain the purpose of the #pragma directive in C.

Answer: The #pragma directive in C is used to provide hints, instructions, or settings to the compiler. It is compiler-specific and can be used for various purposes, such as optimization or configuration.

62. What is the difference between a stack and a heap in memory allocation in C?

Answer: In C, the stack is used for local variables and function call management, while the heap is used for dynamic memory allocation using functions like malloc() and calloc(). Stack memory is limited and has a fixed size, while heap memory is more flexible but requires manual memory management.

63. How is a structure different from a union in C?

Answer: A structure in C is used to group variables of different data types into a single entity, and each member has its memory location. A union, on the other hand, shares memory locations for its members, allowing only one member to be active at a time.

64. Explain the purpose of the volatile keyword in C.

Answer: The volatile keyword in C is used to indicate that a variable may change its value at any time outside the program’s control. It prevents the compiler from optimizing accesses to that variable.

65. How do you define a constant pointer in C?

Answer: To define a constant pointer in C, you use the const keyword before the pointer’s data type. For example, const int* ptr; declares a constant pointer to an integer.

66. What is the purpose of the restrict keyword in C?

Answer: The restrict keyword in C is a hint to the compiler that a pointer does not alias (point to) any other pointer. It can help the compiler optimize code.

67. What is a function prototype in C, and why is it used?

Answer: A function prototype in C is a declaration of a function’s name, return type, and parameters before the function’s actual definition. It informs the compiler about the function’s interface and is used for type checking.

68. What is the difference between a NULL pointer and a void pointer in C?

Answer: A NULL pointer is a pointer that doesn’t point to any memory location. A void pointer (void*) is a generic pointer that can point to any data type but lacks type information.

69. How do you swap two variables in C without using a temporary variable?

Answer: You can swap two variables in C without using a temporary variable using bitwise XOR operations or arithmetic operations. For example:

cCopy code

a = a + b; b = a - b; a = a - b;

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

Answer: The sizeof operator in C returns the size (in bytes) of a data type or variable. It is often used for memory allocation and buffer sizing.

71. Explain the difference between malloc() and free() in C.

Answer: malloc() is used to dynamically allocate memory, while free() is used to deallocate or release memory previously allocated using malloc(). It helps prevent memory leaks.

72. How do you use the const keyword in C?

Answer: The const keyword is used to declare constants, indicating that a variable’s value cannot be modified. It can be applied to variables, function parameters, and function return values.

73. What is the purpose of the goto statement in C, and when should it be used?

Answer: The goto statement in C is used for unconditional branching to a labeled statement within the same function. It is generally discouraged due to its potential to create unstructured code and make code harder to understand and maintain.

74. Explain the purpose of the #pragma directive in C.

Answer: The #pragma directive in C is used to provide hints, instructions, or settings to the compiler. It is compiler-specific and can be used for various purposes, such as optimization or configuration.

75. What is the difference between a stack and a heap in memory allocation in C?

Answer: In C, the stack is used for local variables and function call management, while the heap is used for dynamic memory allocation using functions like malloc() and calloc(). Stack memory is limited and has a fixed size, while heap memory is more flexible but requires manual memory management.

76. How is a structure different from a union in C?

Answer: A structure in C is used to group variables of different data types into a single entity, and each member has its memory location. A union, on the other hand, shares memory locations for its members, allowing only one member to be active at a time.

77. Explain the purpose of the volatile keyword in C.

Answer: The volatile keyword in C is used to indicate that a variable may change its value at any time outside the program’s control. It prevents the compiler from optimizing accesses to that variable.

78. How do you define a constant pointer in C?

Answer: To define a constant pointer in C, you use the const keyword before the pointer’s data type. For example, const int* ptr; declares a constant pointer to an integer.

79. What is the purpose of the restrict keyword in C?

Answer: The restrict keyword in C is a hint to the compiler that a pointer does not alias (point to) any other pointer. It can help the compiler optimize code.

80. What is a function prototype in C, and why is it used?

Answer: A function prototype in C is a declaration of a function’s name, return type, and parameters before the function’s actual definition. It informs the compiler about the function’s interface and is used for type checking.

81. What is the difference between a NULL pointer and a void pointer in C?

Answer: A NULL pointer is a pointer that doesn’t point to any memory location. A void pointer (void*) is a generic pointer that can point to any data type but lacks type information.

82. How do you swap two variables in C without using a temporary variable?

Answer: You can swap two variables in C without using a temporary variable using bitwise XOR operations or arithmetic operations. For example:

cCopy code

a = a + b; b = a - b; a = a - b;

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

Answer: The sizeof operator in C returns the size (in bytes) of a data type or variable. It is often used for memory allocation and buffer sizing.

84. Explain the difference between malloc() and free() in C.

Answer: malloc() is used to dynamically allocate memory, while free() is used to deallocate or release memory previously allocated using malloc(). It helps prevent memory leaks.

85. How do you use the const keyword in C?

Answer: The const keyword is used to declare constants, indicating that a variable’s value cannot be modified. It can be applied to variables, function parameters, and function return values.

86. What is the purpose of the goto statement in C, and when should it be used?

Answer: The goto statement in C is used for unconditional branching to a labeled statement within the same function. It is generally discouraged due to its potential to create unstructured code and make code harder to understand and maintain.

87. Explain the purpose of the #pragma directive in C.

Answer: The #pragma directive in C is used to provide hints, instructions, or settings to the compiler. It is compiler-specific and can be used for various purposes, such as optimization or configuration.

88. What is the difference between a stack and a heap in memory allocation in C?

Answer: In C, the stack is used for local variables and function call management, while the heap is used for dynamic memory allocation using functions like malloc() and calloc(). Stack memory is limited and has a fixed size, while heap memory is more flexible but requires manual memory management.

89. How is a structure different from a union in C?

Answer: A structure in C is used to group variables of different data types into a single entity, and each member has its memory location. A union, on the other hand, shares memory locations for its members, allowing only one member to be active at a time.

90. Explain the purpose of the volatile keyword in C.

Answer: The volatile keyword in C is used to indicate that a variable may change its value at any time outside the program’s control. It prevents the compiler from optimizing accesses to that variable.

91. How do you define a constant pointer in C?

Answer: To define a constant pointer in C, you use the const keyword before the pointer’s data type. For example, const int* ptr; declares a constant pointer to an integer.

92. What is the purpose of the restrict keyword in C?

Answer: The restrict keyword in C is a hint to the compiler that a pointer does not alias (point to) any other pointer. It can help the compiler optimize code.

93. What is a function prototype in C, and why is it used?

Answer: A function prototype in C is a declaration of a function’s name, return type, and parameters before the function’s actual definition. It informs the compiler about the function’s interface and is used for type checking.

94. What is the difference between a NULL pointer and a void pointer in C?

Answer: A NULL pointer is a pointer that doesn’t point to any memory location. A void pointer (void*) is a generic pointer that can point to any data type but lacks type information.

95. How do you swap two variables in C without using a temporary variable?

Answer: You can swap two variables in C without using a temporary variable using bitwise XOR operations or arithmetic operations. For example:

cCopy code

a = a + b; b = a - b; a = a - b;

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

Answer: The sizeof operator in C returns the size (in bytes) of a data type or variable. It is often used for memory allocation and buffer sizing.

97. Explain the difference between malloc() and free() in C.

Answer: malloc() is used to dynamically allocate memory, while free() is used to deallocate or release memory previously allocated using malloc(). It helps prevent memory leaks.

98. How do you use the const keyword in C?

Answer: The const keyword is used to declare constants, indicating that a variable’s value cannot be modified. It can be applied to variables, function parameters, and function return values.

99. What is the purpose of the goto statement in C, and when should it be used?

Answer: The goto statement in C is used for unconditional branching to a labeled statement within the same function. It is generally discouraged due to its potential to create unstructured code and make code harder to understand and maintain.

100. Explain the purpose of the #pragma directive in C.

Answer: The #pragma directive in C is used to provide hints, instructions, or settings to the compiler. It is compiler-specific and can be used for various purposes, such as optimization or configuration.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button