Mastering C: 100 Interview Questions and Answers
20 mins read

Mastering C: 100 Interview Questions and Answers

Mastering C: 100 Interview Questions and Answers

Basic C Questions:

  1. What is C?
    • Answer: C is a general-purpose programming language developed at Bell Labs in 1972 by Dennis Ritchie. It is known for its simplicity and efficiency.
  2. What is the difference between C and C++?
    • Answer: C is a procedural programming language, while C++ is an extension of C that supports object-oriented programming.
  3. What are the basic data types in C?
    • Answer: Basic data types in C include int, char, float, double, and void.
  4. Explain the sizeof operator in C.
    • Answer: The sizeof operator is used to determine the size in bytes of a data type or variable.
  5. What is a header file in C, and how is it used?
    • Answer: A header file contains declarations for functions, variables, and constants. It is included in C programs using #include.

Variables and Data Types:

  1. How do you declare a variable in C?
    • Answer: Declare a variable by specifying its type and name, e.g., int age;.
  2. What is the scope of a variable in C?
    • Answer: The scope of a variable determines where it can be accessed. Variables can have global or local scope.
  3. Explain the difference between int and float data types.
    • Answer: int is for integer values, while float is for floating-point (decimal) values.
  4. What is the difference between char and int in C?
    • Answer: char is used for character data, while int is used for integer data.
  5. What is a constant in C, and how is it defined?
    • Answer: A constant is a value that doesn’t change during program execution. It is defined using the const keyword.

Operators and Expressions:

  1. What is the assignment operator in C?
    • Answer: The assignment operator (=) is used to assign a value to a variable.
  2. Explain the concept of operator precedence in C.
    • Answer: Operator precedence determines the order in which operators are evaluated in an expression. For example, multiplication (*) has higher precedence than addition (+).
  3. What is the ternary conditional operator (?:) in C?
    • Answer: It’s a shorthand for an if-else statement, used for simple conditional assignments.
  4. Explain the difference between pre-increment and post-increment operators (++i and i++).
    • Answer: ++i is pre-increment (increments and then uses the value), while i++ is post-increment (uses the value and then increments).
  5. What is type casting in C, and how is it done?
    • Answer: Type casting is the explicit conversion of one data type to another using type-specific operators like (int) or (float).

Control Flow:

  1. What is an if statement, and how is it used in C?
    • Answer: An if statement is used for conditional execution. It executes a block of code if a specified condition is true.
  2. What is a for loop in C?
    • Answer: A for loop is used for iterating over a block of code a specific number of times.
  3. Explain the purpose of the switch statement in C.
    • Answer: switch is used to select one of many code blocks to be executed based on the value of an expression.
  4. What is the difference between while and do-while loops in C?
    • Answer: while checks the condition before executing the loop, while do-while checks it after, ensuring the loop runs at least once.
  5. What is the purpose of the break and continue statements in C?
    • Answer: break is used to exit a loop prematurely, and continue is used to skip the rest of the current iteration and continue with the next one.

Functions:

  1. How do you declare and define a function in C?
    • Answer: Declare the function’s prototype, define the function’s code, and then call it.
  2. What is the difference between call by value and call by reference in C?
    • Answer: Call by value passes a copy of the argument to the function, while call by reference passes a reference to the original argument.
  3. What is function recursion in C, and how is it used?
    • Answer: Function recursion is when a function calls itself. It’s used to solve problems that can be broken down into smaller, similar subproblems.
  4. What is a function pointer in C, and how is it declared?
    • Answer: A function pointer is a variable that stores the address of a function. It is declared using the function’s signature.
  5. Explain the concept of variable scope in C functions.
    • Answer: Variables in a function have local scope, meaning they are only accessible within that function. Global variables have broader scope.

Arrays and Pointers:

  1. How do you declare and initialize an array in C?
    • Answer: Declare an array with a specified size and initialize it with values in curly braces {}.
  2. What is a pointer in C, and how is it used?
    • Answer: A pointer is a variable that stores the memory address of another variable.
  3. How do you allocate memory dynamically for an array using malloc() in C?
    • Answer: Use malloc() to allocate memory for an array, like int* arr = (int*)malloc(5 * sizeof(int));.
  4. What is pointer arithmetic, and how is it used in C?
    • Answer: Pointer arithmetic involves performing arithmetic operations on pointers to navigate and manipulate memory.
  5. Explain the difference between an array and a pointer in C.
    • Answer: An array is a collection of elements with the same data type, while a pointer is a variable that stores a memory address.

Structures and Unions:

  1. What is a structure in C, and how is it defined?
    • Answer: A structure is a composite data type that groups together variables of different data types. It is defined using the struct keyword.
  2. What is the purpose of a union in C, and how is it defined?
    • Answer: A union is a composite data type that can store different types of data in the same memory location. It is defined using the union keyword.
  3. What is the difference between a structure and a union in C?
    • Answer: In a structure, all members have their own memory space, while in a union, all members share the same memory space.
  4. How do you access members of a structure in C?
    • Answer: Use the dot (.) operator to access members of a structure.
  5. What is the purpose of typedef in C, and how is it used?
    • Answer: typedef is used to create custom data type aliases. It improves code readability and portability.

File Handling:

  1. How do you open and close a file in C?
    • Answer: Use fopen() to open a file for reading or writing and fclose() to close it.
  2. What is the difference between text mode and binary mode when opening a file in C?
    • Answer: Text mode is for reading and writing text files, while binary mode is for reading and writing binary files.
  3. How do you read data from a file in C?
    • Answer: Use functions like fread() or fgets() to read data from a file.
  4. What is the purpose of the fprintf() function in C?
    • Answer: fprintf() is used to write formatted data to a file.
  5. How do you check if a file exists in C before opening it?
    • Answer: You can use the access() function or check the return value of fopen() to determine if a file exists.

Memory Management:

  1. What is dynamic memory allocation in C, and how is it done?
    • Answer: Dynamic memory allocation involves allocating and deallocating memory during program execution using functions like malloc() and free().
  2. How do you allocate memory for an array dynamically using malloc() in C?
    • Answer: Use malloc() to allocate memory, like int* arr = (int*)malloc(5 * sizeof(int));.
  3. What is a memory leak in C, and how can it be avoided?
    • Answer: A memory leak occurs when a program fails to deallocate memory, causing memory consumption to grow. To avoid it, always deallocate dynamically allocated memory using free().
  4. Explain the purpose of the malloc() and free() functions in C.
    • Answer: malloc() is used to allocate memory, and free() is used to deallocate memory.
  5. What is RAII (Resource Acquisition Is Initialization) in C?
    • Answer: RAII is a C programming idiom where resource management (e.g., memory allocation) is tied to the lifetime of an object. Resources are automatically released when the object goes out of scope.

Strings and Character Handling:

  1. What is a string in C, and how is it represented?
    • Answer: In C, a string is represented as an array of characters terminated by a null character ('\0').
  2. How do you compare two strings in C?
    • Answer: Use the strcmp() function to compare two strings in C. It returns 0 if the strings are equal.
  3. Explain the purpose of the strlen() function in C.
    • Answer: strlen() is used to calculate the length of a string, excluding the null character.
  4. What is the difference between a character array and a string in C?
    • Answer: A character array is a collection of characters, while a string is a character array terminated by a null character ('\0').
  5. How do you concatenate two strings in C?
    • Answer: Use the strcat() function to concatenate two strings in C.

Pointers and Functions:

  1. What is a pointer in C, and how is it used?
    • Answer: A pointer is a variable that stores the memory address of another variable.
  2. How do you declare a pointer in C?
    • Answer: Declare a pointer by specifying the data type it points to, e.g., int* ptr;.
  3. What is a null pointer in C, and why is it useful?
    • Answer: A null pointer points to no memory location. It is useful for indicating that a pointer doesn’t point to a valid object.
  4. Explain the difference between NULL and nullptr in C.
    • Answer: NULL is a macro defined as 0, while nullptr is a C++ keyword used to represent a null pointer.
  5. What is the purpose of a function pointer in C, and how is it declared?
    • Answer: A function pointer is a variable that stores the address of a function. It is declared using the function’s signature.

Structures and File Handling:

  1. What is a structure in C, and how is it defined?
    • Answer: A structure is a composite data type that groups together variables of different data types. It is defined using the struct keyword.
  2. How do you access members of a structure in C?
    • Answer: Use the dot (.) operator to access members of a structure.
  3. What is a typedef in C, and why is it used?
    • Answer: typedef is used to create custom data type aliases. It improves code readability and portability.
  4. How do you open and close a file in C?
    • Answer: Use fopen() to open a file for reading or writing and fclose() to close it.
  5. What is the purpose of the fprintf() function in C?
    • Answer: fprintf() is used to write formatted data to a file.

Dynamic Memory Allocation and Pointers:

  1. What is dynamic memory allocation in C, and how is it done?
    • Answer: Dynamic memory allocation involves allocating and deallocating memory during program execution using functions like malloc() and free().
  2. How do you allocate memory for an array dynamically using malloc() in C?
    • Answer: Use malloc() to allocate memory, like int* arr = (int*)malloc(5 * sizeof(int));.
  3. What is a memory leak in C, and how can it be avoided?
    • Answer: A memory leak occurs when a program fails to deallocate memory, causing memory consumption to grow. To avoid it, always deallocate dynamically allocated memory using free().
  4. Explain the purpose of the malloc() and free() functions in C.
    • Answer: malloc() is used to allocate memory, and free() is used to deallocate memory.
  5. What is RAII (Resource Acquisition Is Initialization) in C?
    • Answer: RAII is a C programming idiom where resource management (e.g., memory allocation) is tied to the lifetime of an object. Resources are automatically released when the object goes out of scope.

Strings and Character Handling:

  1. What is a string in C, and how is it represented?
    • Answer: In C, a string is represented as an array of characters terminated by a null character ('\0').
  2. How do you compare two strings in C?
    • Answer: Use the strcmp() function to compare two strings in C. It returns 0 if the strings are equal.
  3. Explain the purpose of the strlen() function in C.
    • Answer: strlen() is used to calculate the length of a string, excluding the null character.
  4. What is the difference between a character array and a string in C?
    • Answer: A character array is a collection of characters, while a string is a character array terminated by a null character ('\0').
  5. How do you concatenate two strings in C?
    • Answer: Use the strcat() function to concatenate two strings in C.

Pointer Arithmetic and Functions:

  1. What is a pointer in C, and how is it used?
    • Answer: A pointer is a variable that stores the memory address of another variable.
  2. How do you declare a pointer in C?
    • Answer: Declare a pointer by specifying the data type it points to, e.g., int* ptr;.
  3. What is a null pointer in C, and why is it useful?
    • Answer: A null pointer points to no memory location. It is useful for indicating that a pointer doesn’t point to a valid object.
  4. Explain the difference between NULL and nullptr in C.
    • Answer: NULL is a macro defined as 0, while nullptr is a C++ keyword used to represent a null pointer.
  5. What is the purpose of a function pointer in C, and how is it declared?
    • Answer: A function pointer is a variable that stores the address of a function. It is declared using the function’s signature.

Arrays and Pointers:

  1. How do you declare and initialize an array in C?
    • Answer: Declare an array with a specified size and initialize it with values in curly braces {}.
  2. What is pointer arithmetic, and how is it used in C?
    • Answer: Pointer arithmetic involves performing arithmetic operations on pointers to navigate and manipulate memory.
  3. What is an array of pointers in C, and why is it useful?
    • Answer: An array of pointers is an array where each element is a pointer. It’s useful for managing multiple strings or data structures.
  4. How do you pass an array to a function in C?
    • Answer: You can pass an array to a function by specifying its name as the argument. The function receives a pointer to the array.
  5. What is a two-dimensional (2D) array in C, and how is it declared?
    • Answer: A 2D array is an array of arrays. It is declared with two sets of square brackets, e.g., int matrix[3][3];.

Structures and Unions:

  1. What is a structure in C, and how is it defined?
    • Answer: A structure is a composite data type that groups together variables of different data types. It is defined using the struct keyword.
  2. What is the purpose of a union in C, and how is it defined?
    • Answer: A union is a composite data type that can store different types of data in the same memory location. It is defined using the union keyword.
  3. What is the difference between a structure and a union in C?
    • Answer: In a structure, all members have their own memory space, while in a union, all members share the same memory space.
  4. How do you access members of a structure or union in C?
    • Answer: Use the dot (.) operator to access members of a structure and the same operator for unions.
  5. Explain the concept of bit fields in C structures.
    • Answer: Bit fields allow you to specify the number of bits each member should occupy in a structure, conserving memory.

File Handling:

  1. How do you open and close a file in C?
    • Answer: Use fopen() to open a file for reading or writing and fclose() to close it.
  2. What is the difference between text mode and binary mode when opening a file in C?
    • Answer: Text mode is for reading and writing text files, while binary mode is for reading and writing binary files.
  3. How do you read data from a file in C?
    • Answer: Use functions like fread() or fgets() to read data from a file.
  4. What is the purpose of the fprintf() function in C?
    • Answer: fprintf() is used to write formatted data to a file.
  5. How do you check if a file exists in C before opening it?
    • Answer: You can use the access() function or check the return value of fopen() to determine if a file exists.

Memory Management:

  1. What is dynamic memory allocation in C, and how is it done?
    • Answer: Dynamic memory allocation involves allocating and deallocating memory during program execution using functions like malloc() and free().
  2. How do you allocate memory dynamically for an array using malloc() in C?
    • Answer: Use malloc() to allocate memory, like int* arr = (int*)malloc(5 * sizeof(int));.
  3. What is a memory leak in C, and how can it be avoided?
    • Answer: A memory leak occurs when a program fails to deallocate memory, causing memory consumption to grow. To avoid it, always deallocate dynamically allocated memory using free().
  4. Explain the purpose of the malloc() and free() functions in C.
    • Answer: malloc() is used to allocate memory, and free() is used to deallocate memory.
  5. What is RAII (Resource Acquisition Is Initialization) in C?
    • Answer: RAII is a C programming idiom where resource management (e.g., memory allocation) is tied to the lifetime of an object. Resources are automatically released when the object goes out of scope.

Strings and Character Handling:

  1. What is a string in C, and how is it represented?
    • Answer: In C, a string is represented as an array of characters terminated by a null character ('\0').
  2. How do you compare two strings in C?
    • Answer: Use the strcmp() function to compare two strings in C. It returns 0 if the strings are equal.
  3. Explain the purpose of the strlen() function in C.
    • Answer: strlen() is used to calculate the length of a string, excluding the null character.
  4. What is the difference between a character array and a string in C?
    • Answer: A character array is a collection of characters, while a string is a character array terminated by a null character ('\0').
  5. How do you concatenate two strings in C?Answer: Use the strcat() function to concatenate two strings in C.

Leave a Reply

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