C Programming Interview Questions
This page consists of C Programming interview questions and answers.
When a programmer allocates a memory space in a heap and forgets to deallocate the memory then it gives rise to memory leak.
There is no function to directly compare two structure variables. We have to compare each of the elements of the two structure variables to tell if they are equal or not.
We can return only one value from the function if we use the return
keyword.
break
keyword?We can use the break
keyword in loops and in switch statement to jump out of the loop body or the switch.
Click here to learn more about loops and click here to learn about switch statement.
< >
sign and double quotes " "
When we include header file using the angle brackets like #include <stdio.h>
the compiler searching for the header file only looks inside the default include path.
When we include header file using the double quotes like #include "awesome.h"
the compiler searching for the header file first looks inside the current directory. If the header is not found in the current directory then it looks inside the default include path.
Negative integer value is stored in 2's complement.
Example:
Lets say we have a negative integer -3.
So, 3 in binary is 0011
1's complement of 3 = 1100
2's complement of 3 = 1100 + 1 = 1101
So, -3 is saved as 1101.
If a variable is accessed quite frequently then we can save it in the CPU register by using the register
keyword as accessing variable from register is faster then accessing from memory.
Note! The compiler decides whether to put a variable in register or not. So, if we have multiple variables marked with the register keyword then the compiler will decide which one to save in the register and which one to save in memory.
In the following example we have an integer variable marked with the register keyword.
register int x = 1;
C code:
register int n = 10;
int *ptr = &n;
The above C code snippet is NOT VALID.
In the above code the integer variable n
is saved in the CPU register as we are using the register
keyword.
In the next line we are assigning the address of variable n to integer pointer variable ptr
and this is invalid.
Note! If a variable is stored in register then accessing its address is invalid.
C code:
int n = 10;
register int *ptr = &n;
Yes the above C code snippet is VALID.
In the first line we are creating an integer variable n
and assigning integer value 10.
In the next line we are creating an integer pointer variable ptr
and assigning the address of variable n
.
Note! The integer pointer variable ptr
is marked with the register
keyword so, it will be saved in the CPU register.
It is absolutely valid to store address in a register variable.
typedef
keyword?We use the typedef
keyword to assign a new name to a type.
In the following example the data type int
is assigned a new name intNum using the typedef
.
// assigning new name to int type
typedef int intNum;
// creating integer variable using intNum
intNum n = 10;
ADVERTISEMENT