C Language Interview Questions and Answers
C Language Interview Questions: Top 50 C Language Interview Questions. C is a general-purpose, high-level programming language.
What are the best features of C programming language?
- Portability – C is Platform independent language.
- Modularity – C is modular programming language.
- Flexibility – It is flexible.
- Speed – It is very faster.
- Extensibility – New features can be easily added by the developers.
Difference between declaration and definition of a variable/function?
Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated to them.
But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable.
In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about the declaration.
When we define a variable/function, apart from the role of the declaration, it also allocates memory for that variable/function.
Therefore, we can think of definition as a superset of the declaration. (or declaration as a subset of definition).
From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function).
Different storage class specifiers in C?
Ans: auto, register, static, extern
What is scope of a variable? How are variables scoped in C?
Ans: Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped.
When should we use pointers in a C program?
- To get the address of a variable
- For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.
- To pass large structures so that complete copy of the structure can be avoided.
- To implement “linked” data structures like linked lists and binary trees.
What is NULL pointer?
Ans: NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.
What is Dangling pointer?
Ans: Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. Following are examples.
What is memory leak? Why it should be avoided
Ans: Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
What is the use of static variable in C?
Ans: A variable which is declared as static is known as a static variable. The static variable retains its value between multiple function calls.
What is the use of function in C?
Ans: A function in C language provides modularity. It can be called many times. It saves code and we can reuse the same code many times.
What is the difference between call by value and call by reference in C?
We can pass the value to function by one of the two ways: call by value or call by reference.
In case of call by value, a copy of the value is passed to the function, so original value is not modified.
In case of call by reference, an address of value passed to the function, so original value is modified.
What is recursion in C?
Ans: Calling the same function, inside function is known as recursion.
What is array in C?
Ans: Array is a group of similar types of elements. It has contiguous memory location. It makes the code optimized, easy to traverse and easy to sort.
What is pointer in C?
Ans: A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast.
What are the usage of pointer in C?
- Accessing array elements
- Dynamic memory allocation
- Call by Reference
- Data Structures like a tree, graph, linked list etc.
What is NULL pointer in C?
Ans: A pointer that doesn't refer to any address of a value but NULL, is known as NULL pointer.
What is far pointer in C?
Ans: A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer.
What is dangling pointer in C?
If a pointer is pointing at any memory location but meanwhile another pointer deletes the memory occupied by the first pointer while the first pointer still points to that memory location, the first pointer will be known as a dangling pointer. This problem is known as the dangling pointer problem.
What is pointer to pointer in C?
In case of a pointer to pointer concept, one pointer refers to the address of another pointer.
What is static memory allocation?
In the case of static memory allocation, memory is allocated at compile time and memory can't be increased while executing the program. It is used in the array.
What is dynamic memory allocation?
In the case of dynamic memory allocation, memory is allocated at runtime and memory can be increased while executing the program. It is used in the linked list.
What functions are used for dynamic memory allocation in C language?
What is structure?
A structure is a user-defined data type that allows storing multiple types of data in a single unit.
It occupies the sum of the memory of all members.
What is union?
Like Structure, union is a user-defined data type that allows to store multiple types of data in a single unit.
But it doesn't occupy the sum of the memory of all members. It occupies the memory of largest member only.
What is auto keyword in C?
In C, every local variable of a function is known as automatic (auto) variable.
Let's explain with an example:
Here, both 'i' and 'j' variables are automatic variables.
Note: A global variable can't be an automatic variable.
What is the purpose of sprintf() function?
Ans: It is used to print the formatted output into char array.
Can we compile a program without main() function?
Ans: Yes, we can compile but it can't be executed.
But, if we use #define, we can compile and run C program without using main() function.
What is token?
Ans: Token is an identifier. It can be constant, keyword, string literal etc.
What is command line argument?
The argument passed to the main() function while executing the program is known as command line argument.
What is the acronym for ANSI?
Ans: American National Standard Institute.
What is the difference between getch() and getche()?
The getch() function reads a single character from keyboard. It doesn't uses any buffer, so entered data is not displayed on the output screen.
The getche() function reads a single character from keyword but data is displayed on the output screen. Press Alt+f5 to see the entered character.
What is new line escape sequence?
Ans: The new line escape sequence is represented by "\n". It inserts a new line on the output screen.
What is the difference between near, far and huge pointers?
Ans: A virtual address is composed of selector and offset.
A near pointer doesn't have explicit selector whereas far and huge pointers have explicit selector.
When you perform pointer arithmetic on a far pointer, a selector is not modified but in case of a huge pointer, it can be modified.
These are the non-standard keywords and implementation specific. These are irrelevant in a modern platform.
What is the maximum length of an identifier?
Ans: It is 32 characters ideally but implementation specific.
What is typecasting?
Ans: Converting one data type into another is known as typecasting. For example:
What are the functions to open and close file in C language?
Ans: The fopen() function is used to open file whereas fclose() is used to close file.
Can we access array using pointer in C language?
Ans: Yes, by holding the base address of array into a pointer, we can access the array using a pointer.
What is infinite loop?
Ans: A loop running continuously for indefinite number of times is called infinite loop.
- Infinite For Loop:
- Infinite While Loop:
- Infinite Do-While Loop:
Define pointer in C?
Ans: A pointer is an address location of another variable. It is a value that designates the address or memory location of some other value (usually value of a variable). The value of a variable can be accessed by a pointer which points to that variable. To do so, the reference operator (&) is pre-appended to the variable that holds the value. A pointer can hold any data type, including functions.
What is NULL pointer?
Ans: A null pointer does not point to any object.
NULL and 0 are interchangeable in pointer contexts. Usage of NULL should be considered a gentle reminder that a pointer is involved.
It is only in pointer contexts that NULL and 0 are equivalent. NULL should not be used when another kind of 0 is required.
A pointer that points to a no valid location is known as the null pointer. Null pointers are used to indicate special cases.
For example, no next node pointer in case of a linked list. An indication of errors that pointers returned from functions.
What is difference between far and near pointers?
Ans: In first place they are non-standard keywords.
A near pointer can access only 2^15 memory space and a far pointer can access 2^32 memory space.
Both the keywords are implementation specific and are non-standard.
What is a constant pointer?
Ans: A pointer which is not allowed to be altered to hold another address after it is holding one.
Difference between malloc() & calloc() memory allocation?
Ans: Both functions are used to dynamically allocate the memory.
||calloc initializes the allocated memory to 0 or Null
||malloc contains garbage values
||block of memory is allocated by calloc()
||Byte of memory is allocated by malloc()
||calloc takes two parameters, the number of variables to allocate memory and size of bytes of a single variable
||malloc() takes a single argument, the size of memory
||memory is initialized by calloc()
||Memory initialization is not performed by malloc()
||calloc(n,s) returns a pointer with enough contiguous storage each with s bytes.
||malloc(s) returns a pointer with enough storage with s bytes
||calloc takes little longer than mallocbecause of the extra step of initializing the allocated memory by zero. However, in practice the difference in speed is very tiny and not recognizable.
||malloc is faster than calloc.
||syntax of calloc():
void *calloc(size_t n, size_t size);
Allocates a contiguous block of memory large enough to hold n elements of sizebytes each. The allocated region is initialized to zero.
||syntax of malloc():
void *malloc(size_t n);
Allocates n bytes of memory. If the allocation succeeds, a void pointer to the allocated memory is returned. OtherwiseNULL is returned.
||calloc() take two arguments those are:number of blocks and size of each block.
||malloc() takes one argument that is,number of bytes.
You May Like to Buy Now!