Careers

Soft Skills

C Interview Questions and Answers for Freshers

Top 75+ frequently asked C Programming Interview Questions and Answers for Freshers and 0-2 year experienced C programmers asked in various interviews and competitive exams.

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

We use # include to include a file. The difference between two ways of file inclusion lies in the order in which preprocessor searches for the file specified. When the preprocessor encounters #include<file> statement, it looks for the file specified in the angled brackets in the default location (Path defined in INCLUDE environment variable of the system).

When # include followed by file name in double quotation marks is encountered by the preprocessor, it looks for the file in the current directory. If the file is not found in the current directory, it will look for the file in the default location.

2) Can #include handle other file formats than .h?

Yes. Irrespective of the file type, Preprocessor will do its job and will include any file like test.z.

3) What is a void pointer?

A void pointer is a special pointer type which can point to any data type without letting the compiler know. It helps to pass a pointer to some function of any type which can be decided at run time. In the following example input parameter a and b can be both integer and string.

The value of NULL is 0 or (void*)0. Whenever NULL has to be compared with some variable or assigned to a variable, depending upon the type of that variable, the value of NULL will be decided.

5) Can the size of an array be declared at runtime?

No. The size of an array must be stated at the time of compilation. Alternate way is to use dynamic allocation by calloc or malloc.

6) what is the difference between malloc() and calloc()?

Calloc

Malloc

Allocates a region of memory large enough to hold "n” elements of "size" bytes each.

Allocates "size" bytes of memory.

Calloc initializes the whole memory with 0

Malloc does not change the existing memory. So it returns a memory chunk with garbage value.

7) What is the heap in memory?

The heap is where malloc(), calloc(), and realloc() get memory. The allocation of memory from the heap is much slower than the stack. But, the heap is much more flexible about memory allocation than the stack. Memory can be allocated and deallocated in any time and order. This heap memory isn't deallocated by itself, method free() has to be called in order to do so.

Output will be : 6/4 == 1.000000 or 1. 500000. This is a case of operator promotion. The variable num1 is set to “6/4”. Because, both 3 and 4 are integers. So integer division is performed on them and the result is the integer 0. The variable num2 is set to “6/4.0”. Because 4.0 is a float, the number 6 is converted to a float as well, and the result will be floating value 1.5.