C Interview Questions and Answers

  1. What is a pointer in C?

A pointer is a variable that stores the memory address of another variable. It allows direct manipulation of data in memory, enabling efficient memory management and access.

  1. What is the difference between an array and a pointer in C?

In C, an array is a collection of elements of the same data type, stored contiguously in memory. A pointer, on the other hand, is a variable that stores the memory address of another variable.

  1. What is the difference between a structure and a union in C?

In C, a structure is a collection of different data types, stored sequentially in memory. A union, on the other hand, is a special data type that allows the same memory location to be used for different purposes at different times.

  1. What is the difference between pass by value and pass by reference in C?

In C, pass by value involves passing a copy of a variable's value to a function, while pass by reference involves passing the memory address of a variable to a function, enabling direct manipulation of the variable's value.

  1. What is a typedef in C?

A typedef is a keyword in C that is used to create a new type name for an existing data type, making it easier to use and understand the code.

  1. What is the difference between a local and a global variable in C?

In C, a local variable is a variable declared within a function or block, and its scope is limited to that function or block. A global variable, on the other hand, is a variable declared outside of any function or block, and its scope is global, meaning it can be accessed from any part of the program.

  1. What is a static variable in C?

A static variable in C is a variable that retains its value between function calls. It is initialized only once, at the beginning of the program, and is preserved until the end of the program.

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

The ++i and i++ operators both increment the value of the variable i by 1. However, the ++i operator increments the value of i before using it, while the i++ operator increments the value of i after using it.

  1. What is a macro in C?

A macro in C is a preprocessor directive that is used to define a constant or a function-like construct that can be used throughout the program.

  1. What is the difference between a function declaration and a function definition in C?

In C, a function declaration is a statement that tells the compiler the name, return type, and parameter list of a function, while a function definition provides the actual implementation of the function.

  1. What is the difference between an array and a pointer in C?
    An array is a collection of elements of the same data type that are stored in contiguous memory locations. A pointer, on the other hand, is a variable that holds the memory address of another variable. An array can be accessed using its index while a pointer is accessed using the dereferencing operator (*).

 

  1. What is the difference between the "++i" and "i++" operators in C?
    The "++i" operator increments the value of "i" and returns the new value, while the "i++" operator returns the value of "i" and then increments it. In other words, the "++i" operator is a pre-increment operator while the "i++" operator is a post-increment operator.

 

  1. What is a function pointer in C? How is it declared and used?
    A function pointer is a variable that stores the address of a function. It can be declared using the syntax "return_type (*pointer_name)(parameter_list)" where return_type is the data type returned by the function, pointer_name is the name of the pointer, and parameter_list is the list of parameters taken by the function. A function pointer can be used to call the function it points to.

 

  1. What is the purpose of the "volatile" keyword in C?
    The "volatile" keyword is used to indicate to the compiler that a variable may change at any time, even if the code does not modify it. This is useful for variables that are modified by external hardware or by another thread.

 

  1. What is a memory leak in C? How can it be prevented?
    A memory leak occurs when a program fails to release memory that is no longer needed. This can lead to the program consuming increasing amounts of memory until it crashes. Memory leaks can be prevented by ensuring that all dynamically allocated memory is released when it is no longer needed.

 

  1. What is the difference between "malloc" and "calloc"?
    "malloc" is used to allocate a block of memory of a specified size while "calloc" is used to allocate a block of memory of a specified size and initialize it to zero. The syntax of "malloc" is "void* malloc(size_t size)" while the syntax of "calloc" is "void* calloc(size_t nmemb, size_t size)".
  1. What is the difference between a structure and a union in C?

    A structure is a user-defined data type that groups related data items of different data types. A union is also a user-defined data type that groups related data items of different data types, but unlike structures, unions share the same memory space for all their members. This means that only one member of a union can be accessed at a time.
  1. What is the difference between "const" and "volatile" in C?

    The "const" keyword is used to indicate that a variable's value cannot be modified once it has been initialized. The "volatile" keyword is used to indicate that a variable's value may change at any time, even if the code does not modify it.
     
  2. What is a typedef in C?

    A typedef is used to create a new name for an existing data type. It can be used to make code more readable and easier to maintain.
     
  3. What is the difference between a static variable and an automatic variable in C?

    A static variable retains its value even after the function that declared it has returned, while an automatic variable is created and destroyed every time the function that declared it is called and returns.
     
  4. What is a macro in C? How is it defined and used?

    A macro is a piece of code that is defined using the "#define" directive. Macros are used to simplify code and to make it more readable. They are replaced by the preprocessor before the code is compiled.
     
  5. What is a pointer in C? How is it declared and used?

    A pointer is a variable that holds the memory address of another variable. It can be declared using the syntax "data_type *pointer_name" where data_type is the data type of the variable being pointed to, and pointer_name is the name of the pointer. Pointers are used to manipulate memory directly and to create complex data structures such as linked lists and trees.
  1. What is the difference between a stack and a queue in C?
  • A stack is a linear data structure in which elements are added and removed from one end called the top. A queue is also a linear data structure in which elements are added at the back and removed from the front.
  1. What is the difference between "int" and "long" data types in C?
  • Both "int" and "long" are integer data types in C, but "long" is larger than "int". The size of "int" varies depending on the implementation, but it is usually 2 or 4 bytes, while "long" is typically 4 or 8 bytes.
  1. What is a header file in C?
  • A header file is a file that contains declarations for functions, variables, and other constructs that are used in a C program. Header files are typically included at the beginning of a source file using the "#include" directive.
  1. What is the difference between a while loop and a do-while loop in C?
  • A while loop checks the loop condition at the beginning of the loop, while a do-while loop checks the loop condition at the end of the loop. This means that a do-while loop always executes at least once, while a while loop may not execute at all if the loop condition is false.
  1. What is the difference between "== " and " = " operators in C?
  • The "==" operator is used to compare two values for equality, while the "=" operator is used to assign a value to a variable.
  1. What is the difference between a local variable and a global variable in C?
  • A local variable is a variable that is declared within a function and is only accessible within that function. A global variable is a variable that is declared outside of any function and is accessible from any part of the program.

Comments

Leave a Reply

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

73808