C Interview Questions - Set 6

C Programming Interview Questions

Share

This page consists of C Programming interview questions and answers.

Q1: What is memory leak?

When a programmer allocates a memory space in a heap and forgets to deallocate the memory then it gives rise to memory leak.

Q2: How will you compare two structure variables?

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.

Q3: How many values can we return from a function?

We can return only one value from the function if we use the return keyword.

Q4: Where can you use the 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.

Q5: What is the difference between including header files using < > 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.

Q6: How is negative integer value stored?

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.

Q7: What is the use of register storage specifier?

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;

Q8: Is the following C code snippet valid?

C code:

register int n = 10;
int *ptr = &n;

Answer:

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.

Q9: Is the following C code snippet valid?

C code:

int n = 10;
register int *ptr = &n;

Answer:

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.

Q10: What is the use of 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;
Share