Best Top 10 Lists

Best top 10 list of all time

100 C language interview questions and answers for freshers and experienced

  BestTop      

C programming language interview questions and answers


List of 100 common C programming language interview questions and their answers, divided into categories for easy reference:

C language interview questions and answers for freshers and experienced


1. Basics of C

What is C language?

C is a general-purpose, procedural programming language developed by Dennis Ritchie in the 1970s at Bell Labs. It is widely used for system and application software.

What is the difference between a compiler and an interpreter?

A compiler translates the entire source code into machine code before execution, while an interpreter translates code line-by-line during execution.

What is a header file in C?

A header file contains function declarations and macro definitions to be shared between several source files.

What are the basic data types in C?

The basic data types in C are int, char, float, and double.

What is the difference between int and float?

int stores integers, while float stores floating-point numbers (i.e., numbers with decimal points).

2. Operators and Expressions

What are the different types of operators in C?

Arithmetic, Relational, Logical, Assignment, Bitwise, and Conditional operators.

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

++i is a pre-increment operator (increments first, then returns the value), while i++ is a post-increment operator (returns the value, then increments).

What is a ternary operator?

It’s a shorthand for an if-else statement and is written as: condition ? expr1 : expr2.

What is the modulus operator in C?

The modulus operator (%) returns the remainder of a division.

Explain precedence and associativity in operators.

Precedence defines the order in which operators are evaluated, and associativity defines the direction (left-to-right or right-to-left) in which operators of the same precedence are evaluated.

3. Control Structures

What is an if statement?

It’s a conditional statement that executes a block of code only if a specified condition is true.

What is the syntax of a switch statement in C?

c
switch(expression) {
   case constant1: 
      // code
      break;
   case constant2:
      // code
      break;
   default:
      // code
}

What is the difference between while and do-while loops?

A while loop checks the condition before executing the loop body, whereas a do-while loop checks the condition after executing the loop body.

What does the break statement do?

It exits the nearest enclosing loop or switch statement.

What does the continue statement do?

It skips the rest of the current iteration of the loop and jumps to the next iteration.

4. Functions

What is a function in C?

A function is a block of code that performs a specific task and can be reused throughout the program.

What is function prototype?

A function prototype is a declaration of a function that informs the compiler about the function’s name, return type, and parameters before its actual definition.

What is recursion in C?

Recursion is when a function calls itself directly or indirectly to solve a problem.

What is the difference between call by value and call by reference?

In call by value, a copy of the argument is passed to the function, whereas in call by reference, the actual memory address of the argument is passed.

What is the purpose of the return statement in C?

The return statement is used to exit from a function and optionally return a value to the calling function.

5. Arrays and Strings

What is an array?

An array is a collection of elements of the same type stored in contiguous memory locations.

How do you declare an array in C?

int arr[10];

What is the difference between a one-dimensional and two-dimensional array?

A one-dimensional array stores a linear sequence of elements, while a two-dimensional array stores elements in a grid (rows and columns).

What is a string in C?

A string is a sequence of characters terminated by a null character (\0).

How do you concatenate two strings in C?

You can use the strcat() function from the <string.h> library.

6. Pointers

What is a pointer in C?

A pointer is a variable that stores the memory address of another variable.

How do you declare a pointer?

int *ptr;

What is the NULL pointer?

A NULL pointer is a pointer that points to nothing (address 0).

What is pointer arithmetic?

Pointer arithmetic involves operations like incrementing, decrementing, and subtracting pointers.

What is the difference between * and & in pointers?

* is used to dereference a pointer (access the value at the address), while & is used to get the address of a variable.

7. Dynamic Memory Allocation

What is dynamic memory allocation?

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

What is the difference between malloc() and calloc()?

malloc() allocates memory but doesn’t initialize it, whereas calloc() allocates memory and initializes it to zero.

How do you free dynamically allocated memory?

You can use the free() function to release the memory back to the system.

What is the use of realloc()?

realloc() is used to resize previously allocated memory.

What is memory leakage?

Memory leakage occurs when dynamically allocated memory is not freed, leading to wasted memory resources.

8. Structures and Unions

What is a structure in C?

A structure is a user-defined data type that allows grouping of variables of different types under one name.

How do you declare a structure?

struct Student {
    int id;
    char name[50];
};

What is the difference between a structure and a union?

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

How do you access structure members?

You use the dot operator (.) for direct access and the arrow operator (->) for access through a pointer.

What is a typedef in C?

typedef is used to create an alias for a data type.

9. File Handling

What are the different modes for opening a file in C?

Read (r), Write (w), Append (a), Read + Write (r+, w+, a+).

How do you open a file in C?

Use fopen() function, e.g., FILE *fp = fopen("filename.txt", "r");.

How do you close a file in C?

Use fclose() function.

What is the difference between fscanf() and fgets()?

fscanf() reads formatted data, while fgets() reads a line of text until a newline or end-of-file is encountered.

What is fseek() used for?

fseek() is used to move the file pointer to a specific location in a file.

10. Preprocessors

What is the use of #include in C?

#include is used to include the contents of a file or library in the program.

What is a macro in C?

A macro is a fragment of code which is given a name, defined using #define.

What is the difference between #define and const?

#define is a preprocessor directive for text substitution, while const declares constant variables with a specific type.

What is conditional compilation in C?

Conditional compilation allows parts of the code to be compiled or ignored based on certain conditions using directives like #ifdef, #ifndef, #if.

What is the purpose of #pragma in C?

#pragma is used to issue special commands to the compiler, often specific to the compiler being used.

Let me know if you'd like to continue with more questions or focus on specific topics!

11. Advanced Pointers

What is a pointer to a pointer in C?

A pointer to a pointer is a pointer that stores the address of another pointer. It allows for multi-level pointer operations.

How do you declare a pointer to a pointer?

int **ptr;

What is a function pointer in C?

A function pointer is a pointer that points to the address of a function and allows the function to be called indirectly.

How do you declare and use a function pointer?

void (*funcPtr)(int) = &functionName;
funcPtr(5);

What are void pointers in C?

A void pointer is a generic pointer that can point to any data type, but it needs to be cast to the correct type before dereferencing.

12. Arrays and Pointers

How are arrays and pointers related in C?

In C, the name of an array acts as a pointer to the first element of the array.

How do you pass an array to a function in C?

Arrays are passed by reference (i.e., the base address of the array is passed). Example:

void function(int arr[]) {
    // function code
}

What is pointer to an array?

A pointer to an array is a pointer that points to the entire array rather than to an individual element.

int (*ptr)[5];

What is the difference between char[] and char* in C?

char[] is an array of characters, while char* is a pointer to a character. In most cases, they behave similarly, but arrays have fixed sizes, whereas pointers can be reassigned.

Can you increment or decrement an array variable?

No, an array variable (like arr) is a constant pointer and cannot be incremented or decremented.

13. Strings

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

strcpy() copies the entire string until the null character, while strncpy() allows you to specify the number of characters to copy.

What is the use of strlen() function?

strlen() returns the length of a string (excluding the null character).

What is the difference between strcmp() and strncmp()?

strcmp() compares two strings until the null character, while strncmp() compares a specific number of characters from two strings.

What is the purpose of strchr() in C?

strchr() finds the first occurrence of a character in a string.

How do you reverse a string in C?

You can reverse a string using a loop or recursion, or by using the strrev() function (not part of standard C, but available in some libraries).

14. Memory Management

What is the difference between stack and heap memory?

Stack memory is used for static memory allocation and follows LIFO (Last In, First Out), while heap memory is used for dynamic memory allocation and requires manual management (allocation and deallocation).

What are the common causes of segmentation faults in C?

Dereferencing null or uninitialized pointers, accessing out-of-bounds array elements, or freeing memory twice.

What is the difference between malloc() and free()?

malloc() allocates memory dynamically, while free() releases the memory that was previously allocated.

What happens if you try to access memory that has been freed?

Accessing freed memory can cause undefined behavior, including crashes and segmentation faults.

What is a dangling pointer in C?

A dangling pointer is a pointer that points to memory that has been freed or deallocated, which can cause undefined behavior if dereferenced.

15. Structs and Unions

Can a structure contain a pointer to itself?

Yes, a structure can have a member that is a pointer to the same structure type, enabling the creation of linked lists, trees, etc.

What is the use of bit fields in a structure?

Bit fields allow the allocation of a specific number of bits to structure members, typically used for memory optimization in hardware-related programming.

What is the size of an empty structure in C?

The size of an empty structure is non-zero in most compilers (usually 1 byte) to ensure each structure has a unique memory address.

How do you access union members?

Union members are accessed similarly to structure members, using the dot (.) operator for direct access and the arrow (->) operator for access via pointers.

What is the size of a union?

The size of a union is determined by the size of its largest member because all members share the same memory location.

16. File Handling

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

fgets() reads a specified number of characters from a file or input stream and is safer, while gets() reads a line from the standard input but is unsafe due to potential buffer overflows.

What is the purpose of fflush()?

fflush() clears the output buffer and writes the buffered data to the output stream. It can also be used to clear input buffers on some systems.

What is the difference between fopen() and freopen()?

fopen() opens a file, while freopen() can be used to reopen a file with a different mode or to redirect a stream (such as stdin or stdout).

What is the purpose of ferror()?

ferror() checks for errors in a file stream and returns a non-zero value if an error occurred.

What does feof() do?

feof() checks whether the end of the file has been reached for a given file stream.

17. Macros and Preprocessing

What is the use of #ifndef and #endif in C?

#ifndef is used to check if a macro is not defined. If it’s not defined, the code between #ifndef and #endif will be compiled. This is used to prevent double inclusion of header files.

What is the difference between #include <> and #include ""?

#include <> is used to include system header files, while #include "" is used to include user-defined header files.

What is the #pragma directive?

#pragma gives special instructions to the compiler, often used for machine-specific or compiler-specific optimizations or behaviors.

What is the purpose of the #define directive in C?

#define is used to create symbolic constants or macros.

Can macros have arguments in C?

Yes, macros can take arguments, similar to functions:

#define SQUARE(x) ((x) * (x))

18. Error Handling

How do you handle errors in C?

Error handling can be done using functions like perror(), strerror(), and error codes returned by functions (e.g., fopen() returning NULL on failure).

What is errno in C?

errno is a global variable set by system calls and some library functions to indicate what error occurred.

What is the use of exit() function?

exit() terminates a program and optionally returns a status code to the operating system.

What is setjmp() and longjmp()?

setjmp() and longjmp() are used for non-local jumps, often used for error handling in C.

What is a segmentation fault?

A segmentation fault occurs when a program tries to access a memory location that it is not allowed to access.

19. Miscellaneous

What is the size of int on a 32-bit and 64-bit system?

On a 32-bit system, int is usually 4 bytes. On a 64-bit system, it’s typically still 4 bytes, but this can vary depending on the architecture.

What is the purpose of the volatile keyword in C?

volatile tells the compiler that a variable’s value may change at any time, preventing it from optimizing the code that accesses the variable.

What is const in C?

const is used to define variables whose values cannot be changed after initialization.

What is a memory alignment in C?

Memory alignment refers to arranging data in memory to match the system’s architecture, which can improve access speed and efficiency.

What is the difference between sizeof() and strlen()?

sizeof() returns the size (in bytes) of a data type or object, while strlen() returns the length of a string (number of characters before the null terminator).

20. C Standards and Miscellaneous

What are the different C standards?

Common C standards include C89/C90 (ANSI C), C99, C11, and C18.

What is the purpose of inline keyword?

The inline keyword suggests to the compiler that it should attempt to expand the function inline to reduce function call overhead.

What is the use of restrict keyword in C?

restrict is a pointer qualifier that indicates the object pointed to is accessed only through that pointer, allowing optimization.

What is an opaque pointer?

An opaque pointer is a pointer to a data structure that is defined in one translation unit and used in another without revealing its structure.

What are command-line arguments in C?

Command-line arguments are the arguments passed to the main() function, usually represented as argc (argument count) and argv[] (argument vector), allowing users to provide inputs when running a program.

logoblog

Thanks for reading 100 C language interview questions and answers for freshers and experienced

Previous
« Prev Post

No comments:

Post a Comment