C Interview Questions and Answers

How do declare an array?

We can declare an array by specify its data type, name and the number of elements the array holds between square brackets immediately following the array name.

ex
int array_int[10]; 

Can we access array using pointer in C language?

Yes, by holding the base address of array into pointer, we can access the array using pointer.

What is a string? How do you know its length?

In C, a string is a character array which is terminated by a null character (‘’). The null character tells the string functions to end of the string.
The strlen() function can be used to measure the length of a string. It returns the total number of bytes taken by the string and it is not counted the null character in the string.

What is static memory allocation?

Allocated the memory at compile time called static memory allocation.It is fixed and can't be increased at runtime.

What is dynamic memory allocation?

Allocation of memory at the time of execution is called dynamic memory allocation. In C, It is done via using the standard library functions malloc() and calloc().

What functions are used for dynamic memory allocation in C language?

C provides four dynamic memory allocation functions, those are
malloc(), calloc(), realloc() and free()

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

malloc(): The malloc() function takes only one argument and used toallocates single block of requested memory. It has garbage value initially.
calloc(): The calloc() function takes two arguments and used toallocates multiple block of requested memory. It initially initializes all bytes to zero.

Explain realloc()?

The realloc() function can change the size of a memory space allocated by the malloc() ,calloc() and realloc() itself.

The syntax for the realloc() function is
void *realloc(void *block, size_t size);
Here block is the pointer to the start of a piece of memory space previously allocated.
size specifies the total byte number you want to change to.

What free() function do?

The free() function can be release unused allocated memory by the malloc(), calloc(), or realloc() function.

What are enumerated or enum types?

The enumerated data type can be used to declare named integerconstants.Enumerators are defined with the keyword enum.
By default, the first enumname starts with the value of 0 and the value of each name in the
rest of the list increases by one from left to right.

enum days{SUN,MON,TUE,WED,THU,FRI,SAT};

where SUN represent 0 and MON to SAT respectively 1 to 6

we can also assign any integer values to enum names.

What Is a Structure?

Structure is a user-defined data type and collection of heterogeneous data items,that is collection of different variables with their data type.
The data items in a structure are called members of the structure,whereasvthe structure member can be refer via dot operator (.) with structure variable and arrow operator (->) with structure pointer variable.
for ex
structure student as:

struct student {
introll_number;
charname[30];
inttotal_marks;
} student1;

What Is a Union?

union is similar to a structure, except that the data items saved in the union are overlaid in order to share the same memory location. That is, they all share the same starting address.
The allocated size of a union is at least as large as the size of the largest data item in the list of the union members in memory.

Like a structure, you can reference a union’s members by using dot and arrow operator with it variable.

What are the differences between a union and a structure?

The major differences between structure and union are:

(i) In structure every member has its own memory whereas in union its members share the same member space.
(ii) In structure, it is possible to initialize all the members at the same time which is not possible in case of union. 

What is command line arguments?

The arguments that we pass to main() at command prompt are called command line arguments. These arguments are supplied at the time of invoking the program.
The main()function can take arguments as:main(int argc,char *argv[]) { }
The first argument argc is known as argument counter. It represents the number of arguments in the command line.The second argument argv is known as argument vector. It is an array of char type pointers that points to the command line arguments.Sizeof this array will be equal to the value of argc.

What is a preprocessor?

Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

What is the difference between pre increment and post increment operator?

Post-increment (ex: x++) returns a copy of the original value before increment itself. Pre-increment (ex: ++x) returns a copy of the original value plus 1.
Ex:
int i=0, j, k;
j=i++;
k=++i;
printf("%d %d %d ", i, j, k);

Output :> 2 0 2    

Latest Updates