interview-questions
  1. c-interview-questions

C Interview Questions & Answers


Basics of C Programming:

  1. What is C programming language?

    • C is a general-purpose programming language that was created in 1972 by Dennis Ritchie at Bell Labs. It is widely used for system programming, embedded systems, and application development.
  2. Explain the structure of a C program.

    • A C program consists of a series of functions, and at least one of them must be named main. The main function is the entry point of the program, and the program execution begins from there.
  3. What are the basic data types in C?

    • Basic data types in C include int (integer), float (floating-point), double (double-precision floating-point), char (character), and void (represents no value).
  4. Explain the use of printf and scanf functions in C.

    • printf is used for output (printing to the console), and scanf is used for input (reading from the console). For example:
      printf("Hello, World!\n");
      scanf("%d", &number);
      
  5. What is the difference between ++i and i++ in C?

    • Both ++i and i++ increment the value of i by 1, but ++i is the pre-increment operator, and i++ is the post-increment operator. The key difference is when the increment actually takes place.

Control Flow and Looping:

  1. Explain the difference between if and switch statements in C.

    • if is used for conditional branching based on a boolean expression, while switch is used for multi-way branching based on the value of an expression.
  2. What is the purpose of the break statement in C?

    • The break statement is used to exit a loop or switch statement prematurely. It is often used to terminate the loop or switch when a certain condition is met.
  3. How does the continue statement work in C?

    • The continue statement is used to skip the rest of the code inside a loop and jump to the next iteration. It is often used to skip specific iterations based on a condition.
  4. Explain the concept of the goto statement in C.

    • The goto statement allows jumping to a labeled statement in the code. However, the use of goto is generally discouraged due to its potential to create unreadable and unmaintainable code.
  5. What is the purpose of the ternary operator (? :) in C?

    • The ternary operator is a shorthand for an if-else statement. It is used to evaluate a boolean expression and return one of two values based on whether the expression is true or false.

Functions in C:

  1. What is a function prototype in C?

    • A function prototype is a declaration of a function that tells the compiler about the function's name, return type, and the types of its parameters. It enables the compiler to perform type checking.
  2. Explain the difference between call by value and call by reference in C.

    • In call by value, the actual value of the variable is passed to the function, while in call by reference, the address (reference) of the variable is passed. Changes to the parameter inside the function affect the original variable in call by reference.
  3. What is recursion, and how does it work in C?

    • Recursion is a programming concept where a function calls itself. In C, a recursive function consists of a base case and a recursive case. The base case defines when the recursion should stop.
  4. What is the purpose of the static keyword in C?

    • The static keyword in C has different meanings depending on its context. When used with a global variable or function, it restricts the scope to the file where it is defined. When used with a local variable, it retains its value between function calls.
  5. Explain the difference between malloc and calloc in C.

    • Both malloc and calloc are used for dynamic memory allocation. malloc allocates a specified number of bytes of memory, while calloc allocates space for an array of elements and initializes them to zero.

Arrays and Pointers:

  1. How do you declare a one-dimensional array in C?

    • A one-dimensional array in C is declared using the syntax:
      int numbers[5];
      
  2. What is the purpose of the sizeof operator in C?

    • The sizeof operator is used to determine the size, in bytes, of a variable or data type. It is often used in memory allocation and pointer arithmetic.
  3. Explain the concept of a pointer in C.

    • A pointer in C is a variable that stores the memory address of another variable. It allows direct manipulation of memory and is used for dynamic memory allocation, passing by reference, and efficient array access.
  4. How do you declare a pointer in C?

    • A pointer in C is declared using the syntax:
      int *ptr;
      
  5. What is the purpose of the arrow operator (->) in C?

    • The arrow operator is used to access the members of a structure or a union through a pointer. For example:
      struct Point {
          int x;
          int y;
      };
      
      struct Point p;
      struct Point *ptr = &p;
      ptr->x = 10;
      

Strings and Character Handling:

  1. How do you declare and initialize a string in C?

    • A string in C is typically declared as an array of characters:
      char myString[] = "Hello, World!";
      
  2. What is the difference between strcmp and strncmp in C?

    • strcmp compares two strings character by character until a difference is found or the end of one of the strings is reached. strncmp compares a specified number of characters, allowing developers to compare only a portion of the strings.
  3. Explain the concept of string concatenation in C.

    • String concatenation in C involves combining two strings into a single string. This can be achieved using functions like strcat or by manually copying characters from one string to another.
  4. What is the purpose of the strlen function in C?

    • The strlen function is used to determine the length of a string (number of characters) excluding the null-terminator ('\0'). It is often used in loop conditions and memory allocation.

Structures and Unions:

  1. What is a structure in C, and how is it defined?
    • A structure in C is a composite data type that groups together variables of different types under a single name. It is defined using the struct keyword. For example:
      struct Point {
          int x;
      

int y; }; ```

  1. Explain the concept of a union in C.

    • A union in C is a special data type that allows storing different data types in the same memory location. Unlike a structure, a union uses the same memory location for all its members.
  2. How do you access members of a structure in C?

    • Members of a structure in C are accessed using the dot (.) operator. For example:
      struct Point {
          int x;
          int y;
      };
      
      struct Point p;
      p.x = 10;
      
  3. What is the purpose of the typedef keyword in C?

    • The typedef keyword is used to create aliases for existing data types. It is often used to improve code readability and simplify complex type declarations.
  4. How do you define a structure pointer in C?

    • A structure pointer in C is defined using the syntax:
      struct Point {
          int x;
          int y;
      };
      
      struct Point *ptr;
      
  5. Explain the difference between a structure and a union in C.

    • A structure in C allows storing different data types under a single name, with each member having its own memory location. A union, on the other hand, shares the same memory location for all its members.

File Handling:

  1. How do you open a file in C?

    • The fopen function is used to open a file in C. It takes two arguments: the name of the file and the mode (e.g., "r" for read, "w" for write).
  2. What is the purpose of the fclose function in C?

    • The fclose function is used to close a file that was previously opened using fopen. Closing a file is important to free up system resources.
  3. How do you read from a file in C?

    • File reading in C is often done using functions like fscanf or fgets. For example:
      FILE *file = fopen("example.txt", "r");
      char buffer[100];
      fscanf(file, "%s", buffer);
      
  4. How do you write to a file in C?

    • File writing in C is often done using functions like fprintf or fputs. For example:
      FILE *file = fopen("example.txt", "w");
      fprintf(file, "Hello, World!");
      

Memory Management:

  1. What is dynamic memory allocation in C?

    • Dynamic memory allocation in C involves allocating memory at runtime using functions like malloc, calloc, and realloc. It allows flexible memory management.
  2. Explain the purpose of the malloc function in C.

    • The malloc function is used to dynamically allocate a specified number of bytes of memory. It returns a pointer to the allocated memory.
  3. What is the difference between free and delete in C?

    • free is used in C to deallocate memory that was previously allocated using functions like malloc. In C++, the delete operator is used to deallocate memory allocated using the new operator.
  4. How do you use calloc for dynamic memory allocation in C?

    • The calloc function is used to dynamically allocate memory for an array of elements, initializing them to zero. For example:
      int *arr = (int *)calloc(5, sizeof(int));
      
  5. Explain the concept of memory leaks in C.

    • Memory leaks in C occur when dynamically allocated memory is not properly deallocated using functions like free. This leads to the gradual consumption of system memory and can cause programs to run out of memory.
  6. What is the purpose of the realloc function in C?

    • The realloc function is used to change the size of a previously allocated block of memory. It can be used to resize an array or reallocate memory for a growing data structure.

Bitwise Operations:

  1. What are bitwise operators in C?

    • Bitwise operators in C perform operations on individual bits of integers. The bitwise operators include & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift).
  2. Explain the purpose of the & (AND) bitwise operator in C.

    • The & bitwise operator performs a bitwise AND operation on each pair of corresponding bits. The result is 1 only if both bits are 1.
  3. What is the use of the | (OR) bitwise operator in C?

    • The | bitwise operator performs a bitwise OR operation on each pair of corresponding bits. The result is 1 if at least one of the bits is 1.
  4. How does the ^ (XOR) bitwise operator work in C?

    • The ^ bitwise operator performs a bitwise XOR (exclusive OR) operation on each pair of corresponding bits. The result is 1 if the bits are different.
  5. Explain the concept of bit manipulation in C.

    • Bit manipulation in C involves using bitwise operators to manipulate individual bits of integers. It is often used for tasks such as setting, clearing, or toggling specific bits.

Preprocessor Directives:

  1. What is a preprocessor directive in C?

    • Preprocessor directives in C are commands that are processed by the C preprocessor before actual compilation. They begin with a # symbol.
  2. Explain the purpose of the #include directive in C.

    • The #include directive is used to include the contents of another file (usually a header file) in the source code. It allows the use of external declarations and definitions.
  3. How do you use conditional compilation in C?

    • Conditional compilation in C is achieved using the #if, #else, and #endif directives. It allows compiling different parts of the code based on predefined macros or conditions.
  4. What is the purpose of the #define directive in C?

    • The #define directive is used to define macros in C. It allows creating symbolic names for constants, expressions, or code snippets.
  5. Explain the concept of macro expansion in C.

    • Macro expansion in C refers to the process of replacing occurrences of macro identifiers in the code with their corresponding macro definitions during the preprocessing phase.