35+ Best [ C ] Interview Questions & Answers [FREQUENTLY ASK]
C Interview Questions and Answers

35+ Best [ C ] Interview Questions & Answers [FREQUENTLY ASK]

Last updated on 04th Jul 2020, Blog, Interview Questions

About author

Ranjith (Sr Project Manager )

High level Domain Expert in TOP MNCs with 8+ Years of Experience. Also, Handled Around 20+ Projects and Shared his Knowledge by Writing these Blogs for us.

(5.0) | 15212 Ratings 855

Mastering C programming not only equips programmers with a powerful tool for developing efficient and portable software but also lays a solid foundation for understanding the intricacies of computer systems. It serves as a stepping stone for those venturing into other languages, making it a valuable skill for both aspiring and seasoned developers.C stands out due to its pointer concept, which enables direct manipulation of memory addresses. Though effective, using pointers requires caution to prevent mistakes and memory-related problems. With its support for multiple data types, C provides flexibility in managing a wide range of data types, from characters and integers to custom-defined structures.

1. What is C programming?

Ans:

C is a general-purpose programming language developed at Bell Labs in 1972.

2. What is the difference between ‘scanf’ and ‘printf’?

Ans:

Scanf:

Format Specifiers: Scanf uses format specifiers (e.g., %d, %f, %s) to indicate the type and format of the data to be read.

Return Value:Scanf returns the number of successfully matched and assigned input items.

Printf:

Format Specifiers:printf is used for formatted output to the standard output (console) or other output streams.

Return Value:printf returns the number of characters printed (excluding the null-terminating character).

3. What is the purpose of the ‘#include’ directive in C?

Ans:

The #include directive in the C programming language serves a crucial role in facilitating modular code development, promoting code reuse, and enabling the use of external libraries.

  • Header Files Inclusion
  • Modular Code Development
  • Preprocessor Directives
  • System Header Files
  • User-Defined Header Files

4. Explain the difference between malloc and calloc.

Ans:

  Feature malloc calloc
Function

Operation allots a certain amount of memory in bytes.

Allots a certain number of memory blocks, each with a specific size.
Initialization Starting overUninitialized; no specific value is assigned to the contents Initializes the allocated memory to zero(or null for pointers)
Common Errors Typical MistakesNot casting the result to the correct pointer typeNot casting the result to the correct pointer type
Memory Content

Data in memory content is erratic (garbage values).

Since it contains null pointers or zero values

5. What is a pointer and how does it differ from an array?

Ans:

Pointer vs. Array:

A pointer is a variable that stores the memory address of another variable. An array is a collection of variables of the same type stored in contiguous memory locations. Pointers can be used to iterate over array elements.

6.Explain the concept of function pointers in C. How are they declared and used?

Ans:

Function pointers in C store the address of a function. They are declared by specifying the function signature they point to. Example: int (*funcPointer)(int, int); declares a function pointer.

7. Difference between const char *myString and char const *myString:

Ans:

Both declare a pointer to constant characters:

The placement of const before or after char determines whether the characters or the pointer is constant.

Purpose of sizeof Operator:

Size of returns the size (in bytes) of a variable or data type. It is useful for dynamic memory allocation, ensuring the correct allocation of memory based on the size of the data type.

8. What is a memory leak in C?

Ans:

It occurs when allocated memory is not deallocated properly, leading to a gradual reduction in available memory.

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

Ans:

Difference between ++i and i++:

++i is pre-increment, where the value is incremented before its use. i + + is post-increment, where the current value is used before being incremented.

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

Ans:

An array is a collection of similar data types, while a pointer is a variable that stores the address of another variable.

11. How do you allocate memory dynamically in C?

Ans:

Dynamic Memory Allocation in C:

Use functions like malloc, calloc, or realloc to allocate memory dynamically. Example: int *arr = (int*)malloc(5 * sizeof(int));

Dynamic Memory Allocation

12. What is the significance of the volatile keyword?

Ans:

Significance of volatile Keyword:

volatile informs the compiler that the variable’s value may change at any time. It prevents certain optimizations, ensuring that the variable is always read from or written to memory.

13. What is the difference between ‘call by value’ and ‘call by reference’?

Ans:

Call by value passes the value to a function, and call by reference passes the address.

14. What is a structure in C?

Ans:

Atomic is the default behavior will ensure the present process is completed by the CPU, before another process accesses the variable is not fast, as it ensures the process is completed entirelyA structure is a user-defined data type that groups related variables under one name.

15. What is the difference between a structure and a union?

Ans:

In a structure, each member has its own memory space. While in a union, all members share the same memory space.

16. Describe the purpose of the enum keyword in C.

Ans:

Purpose of enum Keyword:

enum is used to define named integer constants. It provides a way to create symbolic names for sets of related values, improving code readability.

17. Explain the difference between ‘r’ and ‘w’ modes in file handling.

Ans:

‘r’ is for reading, and ‘w’ is for writing. ‘w’ overwrites an existing file or creates a new one.

18. What is dynamic memory allocation?

Ans:

It involves allocating memory at runtime using functions like malloc and deallocating it using free.

19. Explain the use of static keywords in C.

Ans:

Use of static Keyword:

static is used to declare variables with a lifespan throughout the program’s execution. It limits the scope of the variable to the current source file, making it accessible only within that file.

20. Explain the use of the ‘#ifdef’ and ‘#ifndef’ directives.

Ans:

#ifdef checks if a macro is defined, and #ifndef checks if it’s not defined.

21. What is a typedef in C?

Ans:

It allows defining new data types.

    Subscribe For Free Demo

    [custom_views_post_title]

    22. What is the role of the register keyword in C?

    Ans:

    Role of register Keyword:

    register suggests to the compiler that a variable is frequently used and should be stored in a register. Modern compilers often optimise register allocation, making the keyword less impactful.

    23. What is the significance of the ‘volatile’ keyword in C?

    Ans:

    It tells the compiler that a variable may be changed at any time without any action being taken by the code the compiler finds nearby.

    24. How is an array different from a pointer in C?

    Ans:

    Objective-C allows you to add your own methods to existing classes through categories and class extensions.

    25. What is the purpose of the ‘strlen’ function in C?

    Ans:

    The ‘strlen’ function in C is designed to calculate the length of a null-terminated string. It iterates through the characters of the string until it encounters the null character (‘\0’).

    26. Explain the concept of bitwise operators in C.

    Ans:

    The ‘typedef’ keyword in C is utilised to create aliases for existing data types. This allows programmers to introduce meaningful names for complex types, improving code readability and maintainability.

    27. What is the difference between ‘++i’ and ‘i++’?

    Ans:

    ++i is pre-increment, and i + + is post-increment. In pre-increment, the value is incremented before the current value is used, while in post-increment, the current value is used before incrementing.

    28. How do you swap two variables without using a temporary variable?

    Ans:

    a = a + b; b = a – b; a = a – b;

    29. Explain the concept of a double pointer.

    Ans:

    A double pointer is a pointer that points to another pointer. It is used for indirect access to a value.

    30. Explain the use of #include directive in C.

    Ans:

    Use of #include Directive in C:

    #include is a preprocessor directive that includes the contents of a header file in the program. It allows reuse of code, providing access to functions and declarations defined in other files.

    31. What is the purpose of the ‘strcpy’ function?

    Ans:

    strcpy is used to copy the contents of one string to another.

    Course Curriculum

    Best C Training to Enhance Your Career By TOP-Rated Instructors

    • Instructor-led Sessions
    • Real-life Case Studies
    • Assignments
    Explore Curriculum

    33. What is the difference between ‘fseek’ and ‘rewind’ in file handling?

    Ans:

    fseek is used to set the file position indicator to a specified position, while rewind sets it to the beginning of the file.

    34. Explain the use of ‘fprintf’ and ‘fscanf’ functions.

    Ans:

    fprintf is used to write formatted data to a file, and fscanf is used to read formatted data from a file.

    35. What is a function pointer in C?

    Ans:

    A function pointer is a pointer that points to a function instead of a variable.

    36. Explain the concept of recursion in C with an example.

    Ans:

    • if (condition) {
    • # Code to execute if the condition is true
    • } else {
    • # Code to execute if the condition is false
    • }

    37. Differentiate between continue and break statements.

    Ans:

    Difference between continue and break Statements: Continue skips the rest of the loop body and continues with the next iteration. Break terminates the loop and exits its scope.

    38. What is the purpose of the ‘typedef struct’ in C?

    Ans:

    It allows creating a new type name for a structure

    39. How do you set a particular bit in a variable?

    Ans:

    • variable = variable|(1 << position);

    40. What is the purpose of the ‘bitwise AND’ operator (&)?

    Ans:

    It performs a bitwise AND operation on each pair of corresponding bits.

    • @synchronize(self)
    • {
    • if (counter) counter — ;
    • }.

    41. What is the purpose of the ‘size of’ operator in C?

    Ans:

    It returns the size, in bytes, of the object or data type.

    42. What is the purpose of ‘volatile’ and ‘const’ qualifiers in C?

    Ans:

    volatile indicates that a variable may be changed by external factors, and const indicates that a variable’s value cannot be changed.

    43. What happens if you add your just created object to a mutable array, and you release your object?

    Ans:

    A union is a data structure that allows storing different data types in the same memory location.

    44. What is the significance of the ‘return 0;’ statement in the ‘main’ function?

    Ans:

    • In C, the ‘return 0;’ statement in the ‘main’ function signals successful program execution to the operating system.
    • A return value of zero conventionally indicates that the program terminated without errors, while a non-zero value implies an abnormal termination, often used to convey error codes.

    45. How does recursion work in C, and when is it beneficial?

    Ans:

    Recursion in C involves a function calling itself, breaking a problem into simpler instances. It is beneficial for tasks with repetitive substructures, such as traversing tree-like data structures or computing factorials. Recursion simplifies code by expressing complex problems in a more concise, elegant form.

    46. Discuss the differences between pass-by-value and pass-by-reference.

    Ans:

    Differences between Pass-by-Value and Pass-by-Reference: Pass-by-value sends a copy of the variable to the function. Pass-by-reference sends the actual memory address of the variable, allowing modification of the original.

    47. Explain the concept of a do-while loop in C.

    Ans:

    A do-while loop is similar to a while loop, but it always executes the block of code at least once.

    48. What is the ternary operator in C, and how is it used?

    Ans:

    The ternary operator (? is a shorthand way of writing an if-else statement.

    49. What is a dangling pointer in C?

    Ans:

    A dangling pointer is a pointer that points to a memory location that has been deallocated.

    50. How do you free the memory allocated by ‘malloc’ in C?

    Ans:

    Using the free function

    51. Explain the difference between ‘memset’ and ‘memcpy’.

    Ans:

    memset sets a block of memory to a particular value, and memcpy copies a block of memory from one location to another.

    52. What is the difference between ‘strcmp’ and ‘strncmp’?

    Ans:

    strcmp compares two strings, and strncmp compares a specified number of characters of two strings.

    53. How do you find the length of a string without using the ‘strlen’ function?

    Ans:

    • int stringLength(char str[]) {
    • int length = 0;
    • while (str[length] != ‘\0’) {
    • length++;
    • }
    • return length;
    • }
    Course Curriculum

    Enroll in C Certification Course & Get Hired By TOP MNCs

    Weekday / Weekend BatchesSee Batch Details

    54. What is a function prototype in C, and why is it needed?

    Ans:

    A function prototype declares the function’s name, return type, and parameter types. It is needed to inform the compiler about the function’s signature before its actual definition.

    55. Explain the concept of static functions in C.

    Ans:

    A static function is a function that is only visible within the file it’s declared in.

    56. How do you initialise a structure variable in C?

    Ans:

    • Using the curly braces {} and providing values for each member, e.g.,
    • struct Point {
    • int x;
    • int y;
    • };
    • struct Point p = {1, 2};

    57. What is the purpose of a structure in C, and how is it defined?

    Ans:

    A structure in C is a composite data type that groups variables of different types under a single name. It is defined using the ‘struct’ keyword, allowing the creation of complex data structures.

    58. How do you access structure members in C?

    Ans:

    Structure members in C are accessed using the dot (.) operator. If ‘structVar’ is a structure variable and ‘member’ is a member of that structure, then ‘structVar.member’ is used to access the value of that member.

    59. How do you toggle a particular bit in a variable?

    Ans:

    • variable = variable ^ (1 << position);

    60. What is the purpose of the const keyword in function declarations?

    Ans:

    Purpose of const in Function Declarations:

    Const in function declarations indicates that the function will not modify the values of its parameters. It enhances code clarity and helps catch unintentional modifications.

    61. What is the purpose of the ‘calloc’ function in C?

    Ans:

    calloc allocates memory for an array of elements, initialising them to zero.

    62. Explain the difference between ‘malloc’ and ‘calloc’.

    Ans:

    malloc allocates memory but does not initialise it, while calloc allocates and initialises memory.

    63. What is function overloading, and does C support it?

    Ans:

    Function overloading is the ability to define multiple functions with the same name but different parameter lists. C does not support function overloading.

    64. What is a macro and when would you use it?

    Ans:

    A macro is a fragment of code which has been given a name. Macros are often used for code reusability and to define constants.

    65. What is the purpose of the ‘perror’ function in C?

    Ans:

    perror prints a description for the last error that occurred during a function call.

    66. How do you implement a linked list in C?

    Ans:

    • A basic example of a linked list node and its creation:
    • struct Node {
    • int data;
    • struct Node* next;
    • };

    67. What is the purpose of the ‘volatile’ keyword in C?

    Ans:

    The volatile keyword informs the compiler that a variable may be changed by external factors, and its value should not be optimised.

    68. Explain the difference between ‘fread’ and ‘fwrite’ functions in C.

    Ans:

    In C, ‘fread’ is used for reading data from a file, while ‘fwrite’ is used for writing data to a file. Both functions operate on blocks of data specified by size and count parameters, facilitating efficient file I/O operations.

    69. How do you open a file for writing in C using the ‘fopen’ function?

    Ans:

    To open a file for writing in C, ‘fopen’ is used with the mode “w”. For example, FILE *file = fopen(“filename”, “w”);. This statement opens the file named “filename” for writing, creating a new file if it doesn’t exist or truncating the existing file.

    70. Explain the concept of a two-dimensional array in C.

    Ans:

    A two-dimensional array is an array of arrays, providing a grid-like structure.

    71. How do you reverse a string in C without using any library function?

    Ans:

    • void reverseString(char str[]) {
    • int start = 0;
    • int end = strlen(str) – 1;
    • while (start < end) {
    • char temp = str[start];
    • str[start] = str[end];
    • str[end] = temp;
    • start++;
    • end–;
    • }

    72. What is the difference between ‘void main()’ and ‘int main()’ in C?

    Ans:

    The correct signature for main in C is int main(), which indicates that the program is expected to return an integer value.

    73. What is a pointer in C, and how is it different from a variable?

    Ans:

    A pointer in C is a variable that stores the memory address of another variable. Unlike regular variables that store data directly, pointers hold the location of where data is stored in memory. Pointers facilitate dynamic memory allocation, enabling efficient manipulation of data structures.

    C And C Plus Plus Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

    74. Explain the concept of a recursive function. Provide an example.

    Ans:

    • A recursive function is a function that calls itself. Example:
    • int factorial(int n) {
    • if (n == 0 || n == 1)
    • return 1;
    • else
    • return n * factorial(n – 1);
    • }

    74. What is a pointer in C, and how is it different from a variable?

    Ans:

    A pointer in C is a variable that stores the memory address of another variable. Unlike regular variables that store data directly, pointers hold the location of where data is stored in memory.

    75. Explain the concept of dynamic memory allocation using ‘malloc’ in C.

    Ans:

    Dynamic memory allocation in C is achieved through functions like malloc, allowing the program to request memory from the heap during runtime. It returns a pointer to the allocated memory, enabling the creation of data structures with variable sizes.

    76. How do you read a character from the keyboard without using ‘getchar’?

    Ans:

    char c = getchar();

    77. Explain the concept of recursion and provide an example.

    Ans:

    Concept of Recursion in C: Recursion involves a function calling itself. Example: Factorial calculation – int factorial(int n) { return (n == 0) ? 1 : n * factorial(n-1); }

    78. How do you set a specific bit in a variable to 1?

    Ans:

    variable |= (1 << position);

    79. How is an array different from a pointer in C?

    Ans:

    • While arrays and pointers exhibit similarities in certain contexts, they are distinct data types.
    • An array is a collection of elements of the same type stored in contiguous memory locations.

    80. What is the role of ‘errno’ in C error handling?

    Ans:

    • The ‘strlen’ function in C is designed to calculate the length of a null-terminated string.
    • It iterates through the characters of the string until it encounters the null character (‘\0’).
    • This function is fundamental for determining the size of strings, enabling proper memory allocation and manipulation.

    81. Explain the concept of pointer arithmetic in C and provide an example

    Ans:

    Pointer arithmetic in C involves manipulating pointers using addition or subtraction. For instance, adding an integer to a pointer increments it by the size of that data type.

    82. What is the purpose of the ‘calloc’ function in C, and how is it different from ‘malloc’?

    Ans:

    The ‘calloc’ function in C is used for dynamic memory allocation, similar to ‘malloc.’ However, ‘calloc’ initialises the allocated memory to zero, providing a clear distinction from ‘malloc,’ which does not guarantee initialised memory.

    83. How do you concatenate two strings in C without using library functions?

    Ans:

    Concatenating two strings in C without library functions involves iterating through the characters of the first string and appending each to the end of the second. Ensure proper null termination for the resulting concatenated string.

    84. What is a function prototype in C, and why is it important?

    Ans:

    A function prototype in C declares the function’s signature (return type, name, and parameter types) before its actual implementation. It informs the compiler about the function’s existence, allowing proper type checking and facilitating the creation of modular, well-organised code.

    85. Compare ‘call by value’ and ‘call by reference’ in C function arguments.

    Ans:

    ‘Call by value’ involves passing a copy of the actual parameter’s value to the function, preventing modification of the original. ‘Call by reference’ uses pointers to pass the memory address, allowing modifications to the original parameter within the function.

    86. How do you dynamically allocate memory for a structure in C?

    Ans:

    Dynamically allocating memory for a structure in C is achieved using ‘malloc’ or ‘calloc.’ Example: struct MyStruct *ptr = (struct MyStruct*)malloc(sizeof(struct MyStruct)); allocates memory for a structure instance.

    87. What is the purpose of the ‘union’ keyword in C, and when would you use it?

    Ans:

    • ‘union’ in C allows combining variables of different data types into a single memory space, sharing the same memory location.
    • It is used when memory efficiency is critical, and only one member of the union needs to be accessed at a time.

    88. Explain the use of ‘fseek’ and ‘ftell’ functions in C file handling.

    Ans:

    ‘fseek’ in C is used to set the file position indicator, facilitating navigation within the file. ‘ftell’ returns the current position of the file position indicator, aiding in tracking the file’s read/write location.

    89. How do you handle errors during file operations in C?

    Ans:

    Error handling during file operations in C involves checking the return values of file-related functions. Functions like ‘fopen’ return NULL in case of errors, and ‘errno’ can be checked for more details about the error.

    90. Describe the role of the ‘volatile’ keyword in C.

    Ans:

    ‘volatile’ in C informs the compiler that a variable’s value may change at any time without any action being taken by the code. It prevents certain compiler optimizations, ensuring accurate representation of hardware-related changes.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free