Below are six of the most frequently asked C language questions in an entry level / experienced interview in software companies. Question.6 (recursion & factorial program) is frequently asked in fresher’s or entry level interview in C language. Recursion and factorial are interviewers’ favourite to start off in an interview. Question.7(binary to hex) is not purely a C language question. It tests whether the candidate is aware of hexa-decimal and binary representation. Binary and Hexa decimal representation is often used in Embedded System programming in C language. Question.8 is about the differences between structures and unions. Many confuse sturctures and unions. It is a good way to test whether the candidate knows the subtle differences between the both. Question.9 is about the memory advantage of Unions over structures. Question.10, 11 are the most essential basics for a C programmer viz. scope and storage allocation of variables. Any C programmer fairly acquainted with the language must be able to answer these two. See all C interview questions in this blog in one page.
- What is recursion? Write a program using recursion (factorial)?
- To which numbering system, can the binary number 1101100100111100 be easily converted to?
- What are the differences between structures and unions?
- What are the advantages of using unions?
- What is scope & storage allocation of global and extern variables? Explain with an example.
- What is scope & storage allocation of static, local and register variables? Explain with an example.
- What is recursion? Write a program using recursion (factorial)? Recursion: A function is called ‘recursive’ if a statement within the body of a function calls the same function. It is also called ‘circular definition’. Recursion is thus a process of defining something in terms of itself.
- To which numbering system, can the binary number 1101100100111100 be easily converted to?
- What are the differences between structures and unions?
- What are the advantages of using unions?
- What is scope & storage allocation of global and extern variables? Explain with an example
- What is scope & storage allocation of static, local and register variables? Explain with an example.
Program: To calculate the factorial value using recursion.
#include <stdio.h>
int fact(int n);
int main() {
int x, i;
printf("Enter a value for x: \n");
scanf("%d", &x);
i = fact(x);
printf("\nFactorial of %d is %d", x, i);
return 0;
}
int fact(int n) {
/* n=0 indicates a terminating condition */
if (n <= 0) {
return (1);
} else {
/* function calling itself */
return (n * fact(n - 1));
/*n*fact(n-1) is a recursive expression */
}
}
Output:
4
Factorial of 4 is 24
Explanation:
fact(n) = n * fact(n-1)
If n=4
fact(4) = 4 * fact(3) there is a call to fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 * fact(0)
fact(0) = 1
fact(1) = 1 * 1 = 1
fact(2) = 2 * 1 = 2
fact(3) = 3 * 2 = 6
Thus fact(4) = 4 * 6 = 24
Terminating condition(n <= 0 here;) is a must for a recursive program. Otherwise the program enters into an infinite loop.
Back to top
1101100100111100 can be easily converted to hexadecimal numbering system. Hexa-decimal integer constants consist of combination of digits from 0 to 9 and alphabets ‘A’ to ‘F’. The alphabets represent numbers 10 to 15 respectively. Hexa-decimal numbers are preceeded by ’0x’.
1101,1001,0011,1100
1101 = D
1001 = 9
0011 = 3
1100 = C
1101,1001,0011,1100 = 0xD93C
Thus the given binary number 1101100100111100 in hexadecimal form is 0xD93C
Back to top
Structures and Unions are used to store members of different data types.
| STRUCTURE | UNION |
|---|---|
a)Declaration:
struct
{
data type member1;
data type member2;
};
|
a)Declaration:
union
{
data type member1;
data type member2;
};
|
| b)Every structure member is allocated memory when a structure variable is defined. Example: struct emp {
char name[5];
int age;
float sal;
};
struct emp e1;
Memory allocated for structure is 5+4+4=13 bytes(assuming sizeof int is 4, float is 4, char is 1). 5 byte for name, 4 bytes for age and 4 bytes for sal. |
b)The memory equivalent to the largest item is allocated commonly for all members. Example: union emp1 {
char name[5];
int age;
float sal;
};
union emp1 e2;
Memory allocated to a union is equal to size of the largest member. In this case, char name[5] is the largest-sized member. Hence memory allocated to this union is 5 bytes. |
c)All structure variables can be initialized at a time
struct st {
int a;
float b;
};
struct st s = { .a=4, .b=10.5 };
Structure is used when all members are to be independently used in a program. |
c)Only one union member can be initialized at a time
union un {
int a;
float b;
};
union un un1 = { .a=10 };
Union is used when members of it are not required to be accessed at the same time. |
Union is a collection of data items of different data types.
It can hold data of only one member at a time though it has members of different data types.
If a union has two members of different data types, they are allocated the same memory. The memory allocated is equal to maximum size of the members. The data is interpreted in bytes depending on which member is being accessed.
Example:
union pen {
char name;
float point;
};
Here name and point are union members. Out of these two variables, ‘point’ is larger variable which is of float data type and it would need 4 bytes of memory. Therefore 4 bytes space is allocated for both the variables. Both the variables have the same memory location. They are accessed according to their type.
Union is efficient when members of it are not required to be accessed at the same time.
Back to top
Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project. The scope of the extern variables is Global.
Example:
/***************
Index: f1.c
****************/
#include <stdio.h>
extern int x;
int main() {
printf("value of x %d", x);
return 0;
}
/*************** Index: f2.c ****************/ int x = 3;
Here, the program written in file f1.c has the main function and reference to variable x. The file f2.c has the declaration of variable x. The compiler should know the datatype of x and this is done by extern definition.
Global variables: are variables which are declared above the main( ) function. These variables are accessible throughout the program. They can be accessed by all the functions in the program. Their default value is zero.
Example:
#include <stdio.h>
int x = 0;
/* Variable x is a global variable.
It can be accessed throughout the program */
void increment(void) {
x = x + 1;
printf("\n value of x: %d", x);
}
int main(){
printf("\n value of x: %d", x);
increment();
return 0;
}
Register variables: belong to the register storage class and are stored in the CPU registers. The scope of the register variables is local to the block in which the variables are defined. The variables which are used for more number of times in a program are declared as register variables for faster access.
Example: loop counter variables.
register int y=6;
Static variables: Memory is allocated at the beginning of the program execution and it is reallocated only after the program terminates. The scope of the static variables is local to the block in which the variables are defined.
Example:
#include <stdio.h>
void decrement(){
static int a=5;
a--;
printf("Value of a:%d\n", a);
}
int main(){
decrement();
return 0;
}
Here ‘a’ is initialized only once. Every time this function is called, ‘a’ does not get initialized. so output would be 4 3 2 etc.,
Local variables: are variables which are declared within any function or a block. They can be accessed only by function or block in which they are declared. Their default value is a garbage value.
Back to top
| Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Next |