Skip to content
C interview questions
- Compare and contrast compilers from interpreters.
Compilers and interpreters often deal with how program codes are executed. Interpreters execute program codes one line at a time, while compilers take the program as a whole and convert it into object code, before executing it. The key difference here is that in the case of interpreters, a program may encounter syntax errors in the middle of execution, and will stop from there. On the other hand, compilers check the syntax of the entire program and will only proceed to execution when no syntax errors are found.
- When is the “void” keyword used in a function?
When declaring functions, you will decide whether that function would be returning a value or not. If that function will not return a value, such as when the purpose of a function is to display some outputs on the screen, then “void” is to be placed at the leftmost part of the function header. When a return value is expected after the function execution, the data type of the return value is placed instead of “void”.
- What is the value of NULL?
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.
- 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.
- What is the difference between malloc() and calloc()?
Calloc allocates a region of memory large enough to hold “n” elements of “size” bytes each. Calloc initializes the whole memory with 0.
Malloc allocates “size” bytes of memory. Malloc does not change the existing memory. So it returns a memory chunk with garbage value.
- 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.