Anda di halaman 1dari 17

Mention the different storage classes in C.

This might be one of the most debated C interview questions; the answer to this question varies
book by book, and site by site on the internet. Here, I would like to make it clear there are only
two storage classes in C, and the rest are storage class specifiers.
As per reference manual of The C Programming Language by: Brian W. Kernighan and
Dennis M. Ritchie, in the Appendix A of the reference manual, the very first line says: There are
two storage classes: automatic and static.
[The C programing Language 2nd Edition,Brian W. Kernighan ,Dennis M. Ritchie ]
Q2. What are the different storage class specifiers in C?
There are 4 storage class specifiers in C, out of which auto and static act as storage classes as
well. So, the storage class specifiers are:
Auto
Register
Static
Extern
Q3. What are library functions in C?
Library functions are the predefined functions in C, stored in .lib files.
Q4. Where are the auto variables stored?
Auto variables are stored in the main memory. The default value of auto variables is garbage
value.
Q5. What is the difference between i++ and ++i?
One of the most asked C interview questions or viva questions i++ and ++i. The expression
i++ returns the old value and then increases i by 1, whereas the expression ++i first increases
the value of i by 1 and then returns the new value.
Q6. What is l-value in C? Mention its types.
Location value, commonly known as the l-value, refers to an expression used on the left side of
an assignment operator. For example: in the expression x = 5, x is the l-value and 5 is the r-
value.
There are two types of l-value in C modifiable l-value and non-modifiable l-value. modifiable
l-value denoted a l-value which can be modified. non-modifiable l-value denote a l-value which
cannot be modified. const variables are non-modifiable l-value.
Q7. Can i++ and ++i be used as l-value in C?
In C both i++ and ++i cannot be used as l-value. Whereas in C++, ++i can be used as l-value, but
i++ cannot be.
Q8. Which of the following shows the correct hierarchy of arithmetic operations in C?
(1) / + * -
(2) * / +
(3) + / *
(4) * / + -
4 is the correct answer.
Q9. Which bit wise operator is suitable for
1. checking whether a particular bit is on or off?
Ans. The bitwise AND operator.
2. turning off a particular bit in a number?
Ans. The bitwise AND operator.
3. putting on a particular bit in a number?
Ans. The bitwise OR operator.
Q10. Can a C program be written without using the main function?
This is one of the most interesting C interview questions. I guess, up until now, you might not
have ever written a C program without using the main function. But, a program can be executed
without the main function. See the example below:
Sample C program without main function - C Interview Questions
C
#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m
#define begin decode(a,n,i,m,a,
int begin()

1
2
3
4
5
6
7
#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
printf( hello );
}
Now, lets see whats happening within the source code. Here, #define acts as the main function
to some extent. We are basically using #define as a preprocessor directive to give an impression
that the source code executes without the main function.
Q11. What is the output of printf(%d); ?
For printf(%d, a); the compiler will print the corresponding value of a. But in this case, there is
nothing after %d, so the compiler will show garbage value in output screen.
Q12. What is the difference between printf() and sprintf() ?
printf() statement writes data to the standard output device, whereas sprintf() writes data to the
character array.
Q13. What is the difference between %d and %*d ?
Here, %d gives the original value of the variable, whereas %*d gives the address of the variable
due to the use of pointer.
Q14. Which function gets() or fgets(), is safe to use, and why?
Neither gets() nor fgets() is completely safe to use. When compared, fgets() is safe to use than
gets() because with fgest() a maximum input length can be specified.
Q15. Write a C program to print Programming is Fun without using semicolon (;) .
Heres a typical source code to print Programming is Fun.
Printing Programming is Fun using semicolon - C Interview Questions
C
#include<stdio.h>
int main()
{
printf("Programming is Fun")

1
2
3
4
5
6
#include<stdio.h>
int main()
{
printf("Programming is Fun");
return 0;
}
Theres not much trick in printing the line without using the semicolon. Simply use the printf
statement inside the if condition as shown below.
Printing Programming is Fun without using semicolon - C Interview Questions
C
#include<stdio.h>
int main()
{
if( printf( "Programming is Fu

1
2
3
4
5
6
#include<stdio.h>
int main()
{
if( printf( "Programming is Fun" ) )
{ }
}
An extension of the above can be write a C program to print ; without using a semicolon.
Printing semicolon sign without using semicolon - C Interview Questions
C
#include<stdio.h>
int main()
{
if(printf("%c",59))

1
2
3
4
5
6
7
#include<stdio.h>
int main()
{
if(printf("%c",59))
{
}
}
Q16. What is the difference between pass by value and pass by reference?
This is another very important C interview questions. I will try to explain this in detail with
source code and output.
Pass by Value:
This is the process of calling a function in which actual value of arguments are passed to call a
function. Here, the values of actual arguments are copied to formal arguments, and as a result,
the values of arguments in the calling function are unchanged even though they are changed in
the called function. So, passing by value to function is restricted to one way transfer of
information. The following example illustrates the mechanism of passing by value to function.
An example illustrating Pass by Value - C Interview Questions
C
#include<stdio.h>
void swap(int a, int b)
{
int temp ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
void swap(int a, int b)
{
int temp ;
temp=a;
a=b;
b=temp;
}
main()
{
int x, y,sum;
prinft(Enter x and y : );
scanf(%d%d,&x,&y);
printf( Before swap, x=%d, y=%d, x,y)
swap(x,y);
printf(After swap, x=%d, y=%d, x,y)
}

//Output:
Enter x and y: 5 10
Before swap, x=5, y=10
After swap, x=5, y=10//
In this example, the values of x and y have been passed into the function and they have been
swapped in the called function without any change in the calling function.
Pass by Reference:
In pass by reference, a function is called by passing the address of arguments instead of passing
the actual value. In order to pass the address of an argument, it must be defined as a pointer. The
following example illustrates the use of pass by reference.
An example illustrating Pass by Reference - C Interview Questions
C
#include<stdio.h>
void swap(int a*, int b*);
main()
{

1 #include<stdio.h>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void swap(int a*, int b*);
main()
{
int x, y,sum;
prinft(Enter x and y : );
scanf(%d%d,&x,&y);
printf( Before swap, x=%d, y=%d, x,y)
swap(&x, &y);
printf(After swap, x=%d, y=%d, x,y)
}
void swap(int *a, int *b)
{
int temp ;
temp=*a;
*a=*b;
*b=temp;
}

//Output:
Enter x and y: 5 10
Before swap, x=5, y=10
After swap, x=10, y=5//
In this example, addresses of x and y have been passed into the function, and their values are
swapped in the called function. As a result of this, the values are swapped in calling function
also.
Q17. Write a C program to swap two variables without using a third variable.
This is one of the very common C interview questions. It can be solved in a total of five steps.
For this, I have considered two variables as a and b, such that a = 5 and b = 10.
Swapping two variables without using third variable - C Interview Questions
C
#include<stdio.h>
int main(){
int a=5,b=10;

1
2
3
4
5
6
7
#include<stdio.h>
int main(){
int a=5,b=10;

a=b+a;
b=a-b;
a=a-b;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
printf("a= %d b= %d",a,b);

a=5;
b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);

a=5;
b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);

a=5;
b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);

a=5,
b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
return 0;
}
Q18. When is a switch statement better than multiple if statements?
When two or more than two conditional expressions are based on a single variable of numeric
type, a switch statement is generally preferred over multiple if statements.
Q19. Write a C program to print numbers from 1 to n without using loop.
Printing numbers from 1 to n without using loop - C Interview Questions
C
void printNos(unsigned int n)
{
if(n > 0)
{

1
2
3
4
5
void printNos(unsigned int n)
{
if(n > 0)
{
printNos(n-1);
6
7
8
printf("%d ", n);
}
}
Q20. What is the difference between declaration and definition of a function?
Declaration of a function in the source code simply indicates that the function is present
somewhere in the program, but memory is not allocated for it. Declaration of a function helps the
program understand the following:
what are the arguments to that function
their data types
the order of arguments in that function
the return type of the function
Definition of a function, on the other hand, acts like a super set of declaration. It not only takes
up the role of declaration, but also allocates memory for that function.
Q21. What are static functions? What is their use in C?
In the C programming language, all functions are global by default. Therefore, to make a
function static, the static keyword is used before the function. Unlike the global functions,
access to static functions in C is restricted to the file where they are declared.
The use of static functions in C are:
to make a function static
to restrict access to functions
to reuse the same function name in other file
Q22. Mention the advantages and disadvantages of arrays in C.
Advantages:
Each element of array can be easily accessed.
Array elements are stored in continuous memory location.
With the use of arrays, too many variables need not be declared.
Arrays have a wide application in data structure.
Disadvantages:
Arrays store only similar type of data.
Arrays occupy and waste memory space.
The size of arrays cannot be changed at the run time.
Q23. What are array pointers in C?
Array whose content is the address of another variable is known as array pointer. Heres an
example illustrating array pointers in C.
Array Pointers in C - C Interview Questions
C
int main()
{
float a=0.0f,b=1.0f,c=2.0f;
float * arr[]= {&a,&b,&c};

1
2
3
4
5
6
7
8
int main()
{
float a=0.0f,b=1.0f,c=2.0f;
float * arr[]= {&a,&b,&c};
b=a+c;
printf("%f",arr[1]);
return 0;
}
Q24. How will you differentiate char const* p and const char* p ?
In char const* p, the pointer p is constant, but not the character referenced by it. Here, you
cannot make p refer to any other location, but you can change the value of the char pointed by
p.
In const char* p, the character pointed by p is constant. Here, unlike the upper case, you cannot
change the value of character pointed by p, but you can make p refer to any other location.
Q25. What is the size of void pointer in C?
Whether its char pointer, function pointer, null pointer, double pointer, void pointer or any
other, the size of any type of pointer in C is of two byte. In C, the size of any pointer type is
independent of data type.
Q26. What is the difference between dangling pointer and wild pointer?
Both these pointers dont point to a particular valid location.
A pointer is called dangling if it was pointing to a valid location earlier, but now the location is
invalid. This happens when a pointer is pointing at the memory address of a variable, but
afterwards some variable has been deleted from that particular memory location, while the
pointer is still pointing at that memory location. Such problems are commonly called dangling
pointer problem and the output is garbage value.
Wild pointer too doesnt point to any particular memory location. A pointer is called wild if it is
not initialized at all. The output in this case is any address.
Q27. What is the difference between wild pointer and null pointer?
Wild pointer is such a pointer which doesnt point to any specific memory location as it is not
initialized at all. Whereas, null pointer is the one that points the base address of segment.
Literally, null pointers point at nothing.
Q28. What is far pointer in C?
Far pointer is the pointer that can
access all the 16 segments, i.e. the whole residence memory of RAM. The size of far pointer is 4
byte. The example below illustrates the size of far pointer in C.
Size of far pointer - C Interview Questions
C
int main()
{
int x=10;
int far *ptr;

1
2
3
4
5
int main()
{
int x=10;
int far *ptr;
ptr=&x;
6
7
8
9
printf("%d",sizeof ptr);
return 0;
}
//Output: 4
Q29. What is FILE?
FILE is simply a predefined data type defined in stdio.h file.
Q30. What are the differences between Structure and Unions?
1. Every member in structure has its own memory, whereas the members in a union share
the same member space.
2. Initialization of all the member at the same time is possible in structures but not in
unions.
3. For the same type of member, a structure requires more space than a union.
4. Different interpretations of the same memory space is possible in union but not in
structures.
Q31. Write a C program to find the size of structure without using sizeof operator?
Finding size of structure without using sizeof operator - C Interview Questions
C
struct ABC
{
int a;
float b;

1
2
3
4
5
6
7
8
9
10
11
12
13
struct ABC
{
int a;
float b;
char c;
};
int main()
{
struct ABC *ptr=(struct ABC *)0;
ptr++;
printf("Size of structure is: %d",*ptr);
return 0;
}
Q32. What is the difference between .com program and .exe program?
Both these programs are executable programs, and all drivers are .com programs.
.com program executes faster than .exe program.
.com file has higher preference than .exe type.
Some Interesting C Interview Questions:
1. Why does n++ execute faster than n+1 ?
The execution depends on the amount machine instruction to carry out the operation in the
expression. n++ requires only a single machine instruction, such as INR, to carry out the
increment operation. On the other hand, n+1 requires more machine instructions to carry out this
operation. Hence, n++ executes faster than n+1 in C.
2. Is int x; a declaration or a definition? What about int x = 3; and x = 3; ?
int x; is a declaration. int x = 3; is a declaration with initialization/definition of variable x with a
value. x = 3; is a definition.
3. What is the parent of the main() function?
The parent of main() function is header files the <stdio.h> header file.
4. Write a C program to print the next prime number for any number entered by the user.
Printing next prime number for any number given as input - C Interview Questions
C
#include< stdio.h>
#include< conio.h>
void main()
{

1
2
3
4
5
6
7
8
9
#include< stdio.h>
#include< conio.h>
void main()
{
int i,j=2,num;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
printf("Next Prime number: ");
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
for(i=num+1; i< 3000; i++)
{
for(j=2; j< i; j++)
{
if(i %j==0)
{
break;
} // if
} // for
if(i==j || i==1)
{
printf("%d\t",i);
break;
}// if
}// outer for getch();
}
5. Write a C program to solve the Tower of Hanoi question without using recursion.
Try this yourself.
6. Create an array of char type whose size is not known until its run, i.e., the size must be
given by the user.
Hints are given in the source code below. If you want to use array finish, you must free it to
return resources to memory.
Solving Tower of Hanoi without recursion - C Interview Questions
C
char *sz = NULL;
int i = 0;
scanf("%d", i );
if(i){ sz = (char *)malloc( sizeof

1
2
3
4
char *sz = NULL;
int i = 0;
scanf("%d", i );
if(i){ sz = (char *)malloc( sizeof(char)*i);
Other Frequently Asked C Interview Questions:
These are some other commonly asked C interview questions or exam/viva questions. Try
answering these on your own, and if you need help with any, mention them in the comments
section at the end.
1. What do you understand by Operator, Operand and Expression in C?
2. What is the difference between declaration and definition of a variable?
3. Can static variables be declared in a header file?
4. Can a variable be both constant and volatile?
5. What are C identifiers?
6. Can include files in C be nested?
7. What are flag values?
8. Write the prototype of printf function.
9. What is the use of void data type in C?
10. What is the use of typedef in C?
11. Differentiate for loop and while loop.
12. Why is the main() function used in C?
13. What are macros? Mention their advantages and disadvantages.
14. Mention the advantages of a macro over a function.
15. What is function recursion in C?
16. What keyword is used to rename a function in C?
17. What is the difference between Calloc() and Malloc() ?
18. What is the difference between strings and character arrays?
19. What are the differences between sizeof operator and strlen function?
20. Differentiate between static memory allocation and dynamic memory allocation.
21. What are the uses of pointers in C?
22. What is a null pointer assignment error?
23. What are enumerations?
24. Mention the advantages of using unions in C.
25. Differentiate a linker and linkage.
So, what do you think of the C interview questions mentioned here? Did you find them new,
difficult, interesting or boring: we would like to hear your feedbacks. Also, you can suggest us
more interview questions that would suit this post. Your suggestions and feedbacks can be
brought up to us from the comments section below, or you can drop a mail at
prameshpudasaini13@yahoo.com.
Share !
tweet



inShare

Tagged with: C interview questions c interview questions and answers commonly asked C
interview questions custom slider interesting C interview questions Most Common C Interview
Questions and Answers
About Pramesh Pudasaini

I am an engineering student at IOE, Pulchowk Campus. An experienced web-content writer, I
like writing articles related to lifestyle, sports, health and fitness, technology and business. I
mostly write programming related contents at my own blog Code with C. Besides writing, I like
watching films, reading novels, traveling, cycling and sleeping.

Previous: Banking Record System Project in C++
Next: Bike Race Game in C++ and SDL
2 comments
1.
Mati Ullah Wazir
July 29, 2014 at 10:09 pm
Q:- Which program is used to convert object code into source code in c/c++? plz help me
Reply
o
Pramesh Pudasaini
July 30, 2014 at 8:11 pm
I sort of get this question as how does a source code become a program? Are
you asking how does a source code get converted into object code?
If yes, the answer is compiler. Source code is turned into object code by a
compiler. Then, once the object code is passed through a linker, an executable
program comes into existence.
Reply
Leave a Reply
Your email address will not be published. Required fields are marked *
Name *
Email *
Website

Advertisement
Forum Login
Username:
Password:
Remember Me
Register Lost Password
Find us on Facebook
Popular
Recent
Comments
Tags

Hospital Management System Project in C
May 23, 2014

Mini Project in C Library Management System
April 3, 2014

Bus Reservation System Project in C++
May 2, 2014

Mini Project in C Snake Game
April 4, 2014

How to include graphics.h in CodeBlocks?
April 17, 2014

Anda mungkin juga menyukai