C language Interview questions

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

1. What is C Language?

Answer: C is a general-purpose, procedural programming language developed by Dennis


Ritchie in 1972. It is widely used for system programming, embedded systems, and
developing operating systems because of its low-level access to memory and
efficient performance.

2. What is a pointer in C?
Answer: A pointer is a variable that stores the memory address of another variable.
It allows direct memory access and manipulation.

3. What is a null pointer?


Answer: A null pointer is a pointer that doesn’t point to any valid memory
location. In C, it is usually defined as NULL.

4. What is the difference between =, ==, and === in C?


Answer:

= is the assignment operator.


== is the equality comparison operator.
=== is not valid in C; it's used in languages like JavaScript.
5. What are the data types in C?
Answer: The basic data types in C are:

int (integer)
float (floating point)
double (double-precision floating point)
char (character)
void (no data)
6. What is sizeof() operator?
Answer: sizeof() is a compile-time operator that returns the size of a data type or
variable in bytes.

7. Explain the difference between malloc() and calloc().


Answer:

malloc() allocates memory without initializing it.


calloc() allocates memory and initializes all bytes to zero.

8. What is a structure in C?
Answer: A structure is a user-defined data type in C that allows grouping variables
of different data types together.

9. What is union in C?
Answer: A union is similar to a structure, but in a union, all members share the
same memory location, meaning only one member can hold a value at any given time.

10. Explain the concept of recursion in C.


Answer: Recursion is a process where a function calls itself directly or
indirectly. It is used to solve problems that can be divided into similar
subproblems.

11. What is the difference between break and continue?


Answer:

break exits the nearest enclosing loop or switch statement.


continue skips the current iteration of the loop and moves to the next iteration.

12. What is a static variable in C?


Answer: A static variable retains its value between function calls and is
initialized only once. It has a local scope but lifetime throughout the program
execution.

13. What is a static function in C?


Answer: A static function is a function with file scope, meaning it can only be
accessed within the file in which it is declared.

14. Explain the difference between a declaration and a definition in C.


Answer:

Declaration introduces a variable or function to the compiler without allocating


memory (e.g., extern int x;).
Definition allocates memory and assigns an initial value (e.g., int x = 10;).

15. What is #include in C?


Answer: #include is a preprocessor directive that includes the contents of a file
(usually a header file) in the program during compilation.

16. What is #define in C?


Answer: #define is a preprocessor directive used to define constants or macros.

17. Explain the difference between ++i and i++.


Answer:
++i is the pre-increment, which increments the value of i before using it.
i++ is the post-increment, which increments the value of i after using it.

18. What are command-line arguments in C?


Answer: Command-line arguments allow passing arguments to the main() function when
executing a program from the command line. They are defined as int main(int argc,
char *argv[]).

19. What is a function prototype?


Answer: A function prototype is a declaration of a function that specifies its
return type, name, and parameters without the body.

20. What is the difference between int main() and void main()?
Answer:
int main() is the standard declaration, where the program returns an integer value
(typically 0 for successful execution).

void main() is non-standard and does not return any value.

21. What is the use of the return statement?


Answer: The return statement is used to exit a function and return a value to the
calling function.

22. What is a dangling pointer?


Answer: A dangling pointer is a pointer that points to a memory location that has
been freed or deleted, leading to undefined behavior.

23. What is a memory leak in C?


Answer: A memory leak occurs when dynamically allocated memory is not freed after
use, causing memory wastage.

24. What is a function pointer?


Answer: A function pointer is a pointer that points to the address of a function,
allowing functions to be passed as arguments.

25. What is the difference between stack memory and heap memory?
Answer:
Stack memory is used for static memory allocation (local variables) and is limited
in size.
Heap memory is used for dynamic memory allocation (e.g., via malloc()) and is much
larger.

26. What is the use of free() in C?


Answer: free() is used to deallocate memory that was previously allocated using
malloc() or calloc().

27. What is type casting in C?


Answer: Type casting is the process of converting a variable from one data type to
another.

28. Explain the difference between implicit and explicit type casting.
Answer:
Implicit casting is automatically done by the compiler when assigning a value to a
variable of a different type.
Explicit casting requires the programmer to manually cast a value, e.g., (int) 3.5.

29. What is a void pointer?


Answer: A void pointer is a generic pointer that can point to any data type. It
must be typecast before dereferencing.

30. Explain the use of the ternary operator in C.


Answer: The ternary operator ?: is a shorthand for the if-else statement, used as
condition ? true_value : false_value.

31. What is an array in C?

Answer: An array is a collection of elements of the same data type, stored in


contiguous memory locations.

32. What is the difference between an array and a pointer?


Answer:
An array holds multiple values in contiguous memory.
A pointer holds the address of a single variable or array element.

33. What is strlen() in C?


Answer: strlen() is a library function that returns the length of a string,
excluding the null terminator.

34. What is the difference between strcpy() and strncpy()?


Answer:
strcpy() copies the entire source string to the destination.
strncpy() copies only the specified number of characters from the source to the
destination.

35. What is a macro in C?


Answer: A macro is a fragment of code defined using #define, which is expanded by
the preprocessor before compilation.

36. What are bitwise operators?


Answer: Bitwise operators perform operations on bits, such as AND (&), OR (|), XOR
(^), left shift (<<), and right shift (>>).

37. What is the difference between -> and . operators?


Answer:
. is used to access members of a structure.
-> is used to access members of a structure through a pointer.

38. What is a segmentation fault in C?


Answer: A segmentation fault occurs when a program tries to access memory that it
is not allowed to, such as dereferencing a null or invalid pointer.

39. What is dynamic memory allocation in C?


Answer: Dynamic memory allocation is the process of allocating memory at runtime
using functions like malloc(), calloc(), realloc(), and freeing it with free().

40. What is the purpose of const keyword?


Answer: const is used to declare variables whose values cannot be changed after
initialization.

41. What are storage classes in C?


Answer: Storage classes in C determine the scope, lifetime, and visibility of
variables. Common storage classes include auto, static,

42. Explain the difference between auto and register.


Answer:

auto is the default storage class for local variables, stored in the stack.
register requests that the variable be stored in a CPU register for faster access.
43. What is a volatile variable?
Answer: A volatile variable tells the compiler that the value of the variable can
change at any time, and thus it must not optimize it.

44. What is an enumerator in C?


Answer: An enumerator (enum) is a user-defined data type that assigns names to a
set of integer values, improving code readability.

45. What is the difference between exit() and _Exit()?


Answer:

exit() performs cleanup tasks like flushing buffers before terminating the program.
_Exit() terminates the program immediately without cleanup.

46. Explain what an lvalue and rvalue are in C.


Answer:

An lvalue (left value) refers to an object that persists beyond a single expression
(e.g., a variable).
An rvalue (right value) is a temporary value that does not persist beyond the
expression (e.g., a constant or expression result).

47. What is a preprocessor in C?


Answer: The preprocessor is a tool that processes the source code before
compilation, handling directives like #include, #define, and macros.

48. What is the difference between int* p and int *p[]?


Answer:
int* p is a pointer to an integer.
int *p[] is an array of pointers to integers.

49. What is the purpose of realloc()?


Answer: realloc() changes the size of dynamically allocated memory without losing
the previously stored data.

50. What is a function pointer array?


Answer: A function pointer array is an array where each element is a pointer to a
function. It allows calling different functions using array indexing.

51. What is the role of the main() function in C?


Answer: The main() function is the entry point of a C program, where execution
begins.

52. What is EOF in C?


Answer: EOF stands for End of File, a special value returned by functions like
getchar() when the end of the input file is reached.

53. What is a buffer in C?


Answer: A buffer is a temporary storage area in memory used to hold data while it’s
being transferred between two places (e.g., between the program and a file).

54. What is recursion depth in C?


Answer: Recursion depth refers to the number of times a recursive function calls
itself before reaching the base case.

55. What is fflush() in C?


Answer: fflush() is used to flush a file stream’s output buffer, ensuring that any
buffered data is written to the file.

56. What is a segmentation fault in C?


Answer: A segmentation fault occurs when a program tries to access memory it
doesn't have the right to access, usually due to dereferencing a null or invalid
pointer.

57. What is a linked list in C?


Answer: A linked list is a data structure where each element (node) contains data
and a pointer to the next node, allowing dynamic memory allocation.

58. What is a circular linked list?


Answer: A circular linked list is a linked list where the last node points back to
the first node, forming a circular structure.

59. Explain the difference between a singly linked list and a doubly linked list.
Answer:

A singly linked list has nodes with one pointer to the next node.
A doubly linked list has nodes with two pointers: one to the next node and one to
the previous node.

60. What is a dangling pointer in C?


Answer: A dangling pointer is a pointer that points to memory that has been freed,
leading to potential undefined behavior if accessed.

61. What is a circular buffer?


Answer: A circular buffer (or ring buffer) is a fixed-size buffer where the end of
the buffer wraps around to the beginning, allowing continuous data flow.

62. Explain the difference between shallow copy and deep copy in C.
Answer:
A shallow copy copies the reference (address) to an object, meaning changes to the
original affect the copy.
A deep copy duplicates the object itself, so the copy and original are independent.

63. What is the const keyword used for in C?


Answer: const declares a variable as constant, meaning its value cannot be modified
after initialization.

64. What is the scope of a variable?


Answer: The scope of a variable defines where in the program the variable can be
accessed, such as local, global, or file scope.

65. What is the difference between global and local variables?


Answer:+
Global variables are declared outside of functions and can be accessed from
anywhere in the program.
Local variables are declared inside a function and can only be accessed within that
function.

66. What is a memory leak, and how do you prevent it?


Answer: A memory leak occurs when dynamically allocated memory is not freed. It can
be prevented by using free() to release memory when it is no longer needed.

67. What is an infinite loop?


Answer: An infinite loop is a loop that never terminates because its exit condition
is never met.

68. What is an iterator in C?


Answer: C does not have built-in iterators, but an iterator can be implemented
using pointers or loop constructs to traverse a data structure like an array or
linked list.

69. What is pointer arithmetic?


Answer: Pointer arithmetic involves performing arithmetic operations (such as
addition or subtraction) on pointers, which affects the memory addresses they point
to.

70. What is a const pointer in C?


Answer: A const pointer is a pointer that cannot be used to modify the value it
points to, ensuring read-only access to the variable.

71. What is the use of the typedef keyword?


Answer: typedef is used to create an alias or new name for an existing data type,
improving code readability and simplicity.

72. What is a multi-dimensional array in C?


Answer: A multi-dimensional array is an array of arrays, where each element itself
is an array. The most common example is a 2D array, used to represent matrices.

73. What is the difference between exit() and return?


Answer:

return is used to return control from a function to the calling function.


exit() terminates the entire program and can be used anywhere within the program.

74. What are the four types of storage classes in C?


Answer: The four types of storage classes in C are auto, register, static, and
extern.

75. What is the difference between static and extern?


Answer:

static restricts a variable or function’s scope to the file in which it is


declared.
extern extends the visibility of a variable or function to other files.
76. What is recursion in C?
Answer: Recursion is when a function calls itself. It is used to solve problems
that can be broken down into simpler subproblems.

77. What is the base case in recursion?


Answer: The base case is the condition in a recursive function where the function
stops calling itself, preventing infinite recursion.

78. Explain the concept of call by value and call by reference.


Answer:
In call by value, a copy of the actual argument is passed to the function, and
changes made to the parameter inside the function do not affect the original value.

In call by reference, a reference (address) to the actual argument is passed, so


changes made inside the function do affect the original value.

79. What is a function prototype in C?


Answer: A function prototype is a declaration of a function that informs the
compiler about the function’s name, return type, and parameters without providing
the function body.

80. What are storage classes in C?


Answer: Storage classes define the scope (visibility), lifetime, and initial value
of variables. The main storage classes in C are:

auto: Default for local variables.


static: Persists the value of a variable between function calls.
extern: Refers to a global variable defined in another file.
register: Suggests storing the variable in a CPU register for faster access.

81. What is the purpose of the volatile keyword?


Answer: volatile tells the compiler not to optimize a variable because its value
can change unexpectedly, such as in memory-mapped I/O or multithreading.

82. What is the restrict keyword?


Answer: restrict is a pointer qualifier that tells the compiler the object pointed
to will only be accessed through that pointer, allowing optimization.

83. What is the difference between a global variable and a static global variable?
Answer:
A global variable can be accessed from any file.
A static global variable can only be accessed within the file in which it is
defined.

84. What is a preprocessor in C?


Answer: The preprocessor processes directives like #include, #define, and #if
before compilation to perform tasks such as file inclusion and macro substitution.

85. What is a segmentation fault and what causes it?


Answer: A segmentation fault occurs when a program attempts to access memory that
it is not allowed to, such as dereferencing a null or invalid pointer.

86. What are header files in C?


Answer: Header files (.h) contain declarations of functions and macros, which are
shared between different source files using the #include directive.

87. What is a file pointer in C?


Answer: A file pointer is a pointer of type FILE* used to handle file operations,
such as opening, reading, writing, and closing files.

88. Explain the difference between fopen() and fclose().


Answer:

fopen() opens a file and returns a pointer to the file.


fclose() closes an opened file, releasing the resources associated with it.

89. What is the difference between fread() and fwrite()?


Answer:
fread() reads data from a file into memory.
fwrite() writes data from memory into a file.

90. What is the difference between printf() and sprintf()?


Answer:
printf() prints formatted output to the console.
sprintf() writes formatted output to a string buffer.

91. What is the difference between exit() and _exit()?


Answer:

exit() performs cleanup tasks such as flushing buffers before terminating the
program.
_exit() terminates the program immediately without performing cleanup.

92. What is an infinite loop in C?


Answer: An infinite loop is a loop that continues indefinitely because the
termination condition is never met. Example:

while(1) {
// infinite loop
}

93. What is a segmentation fault in C?


Answer: A segmentation fault is an error that occurs when a program tries to access
memory that it’s not permitted to. This often happens with invalid pointers.

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


Answer: The sizeof operator returns the size, in bytes, of a data type or variable
at compile time.

95. What is the difference between ++i and i++?


Answer:

++i (pre-increment) increments the value before using it.


i++ (post-increment) increments the value after using it.

96. What is a function pointer in C?


Answer: A function pointer is a pointer that stores the address of a function,
allowing the function to be passed as an argument or returned from other functions.

97. What are command-line arguments?


Answer: Command-line arguments are parameters passed to the main() function when a
program is executed, typically as int main(int argc, char *argv[]).

98. What is the difference between gets() and fgets()?


Answer:
typedef creates a new name for an existing data type.
#define creates a macro, typically for constants or code replacement.
99. What is the purpose of the sizeof operator in C?
Answer: The sizeof operator returns the size, in bytes, of a data type or a
variable at compile time.

100. What is a segmentation fault?


Answer: A segmentation fault occurs when a program tries to access memory that it
does not have permission to access, usually due to an invalid pointer dereference.

You might also like