C Interview Questions
Question-1. What is a pointer on pointer ?
Answer- It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to point to the data held by the designated pointer variable.
Eg: int x = 5, *p=&x, **q=&p;
Therefore ‘x’ can be accessed by **q.
Question-2. Distinguish between malloc() & calloc() memory allocation ?
Answer- Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated memory with 0’s.
Question-3. What is keyword auto for ?
By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables.
void f() {
int i;
auto int j;
}
NOTE − A global variable can’t be an automatic variable.
Question-4. What are the valid places for the keyword break to appear ?
Answer- Break can appear only with in the looping control and switch statement. The purpose of the break is to bring the control out from the said blocks.
Question-5. Explain the syntax for for loop ?
Answer- for(expression-1;expression-2;expression-3) {
//set of statements
}
When control reaches for expression-1 is executed first. Then following expression-2, and if expression-2 evaluates to non-zero ‘set of statements’ and expression-3 is executed, follows expression-2.
Question-6. What is difference between including the header file with-in angular braces < > and double quotes “ “ ?
Answer- If a header file is included with in < > then the compiler searches for the particular header file only with in the built in include path. If a header file is included with in “ “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built in include path.
Question-7. How a negative integer is stored ?
Answer- Get the two’s compliment of the same positive integer. Eg: 1011 (-5)
Step-1 − One’s compliment of 5 : 1010
Step-2 − Add 1 to above, giving 1011, which is -5
Question-8. What is a static variable ?
Answer- A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.
void f() {
static int i;
++i;
printf(“%d “,i);
}
If a global variable is static then its visibility is limited to the same source code.
Question-9. What is a NULL pointer ?
Answer- A pointer pointing to nothing is called so. Eg: char *p=NULL;
Question-10. What is the purpose of extern storage specifier?
Answer- Used to resolve the scope of global symbol.
Eg:
main() {
extern int i;
Printf(“%d”,i);
}
int i = 20;
Question-11. Explain the purpose of the function sprintf() ?
Answer- Prints the formatted output onto the character array.
Question-12. What is the meaning of base address of the array ?
Answer- The starting address of the array is called as the base address of the array.
Question-13. When should we use the register storage specifier ?
Answer- If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable.
S++ or S = S+1, which can be recommended to increment the value by 1 and why?
S++, as it is single machine instruction (INC) internally.
Question-14. What is a dangling pointer ?
Answer- A pointer initially holding valid address, but later the held address is released or freed. Then such a pointer is called as dangling pointer.
Question-15. What is the purpose of the keyword typedef ?
Answer- It is used to alias the existing type. Also used to simplify the complex declaration of the type.
Question-16. What is lvalue and rvalue ?
Answer- The expression appearing on right side of the assignment operator is called as rvalue. Rvalue is assigned to lvalue, which appears on left side of the assignment operator. The lvalue should designate to a variable not a constant.
Question-17. What is the difference between actual and formal parameters ?
Answer- The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters.
Question-18. Can a program be compiled without main() function ?
Answer- Yes, it can be but cannot be executed, as the execution requires main() function definition.
Question-19. What is the advantage of declaring void pointers ?
Answer- When we do not know what type of the memory address the pointer variable is going to hold, then we declare a void pointer for such.
Question-20. Where an automatic variable is stored ?
Answer- Every local variable by default being an auto variable is stored in stack memory.
Question-21. What is a nested structure ?
Answer- A structure containing an element of another structure as its member is referred so.
Question-22. What is the difference between variable declaration and variable definition ?
Answer- Declaration associates type to the variable whereas definition gives the value to the variable.
Question-23. What is a self-referential structure ?
Answer- A structure containing the same structure pointer variable as its element is called as self-referential structure.
Question-24. Does a built-in header file contains built-in function definition ?
Answer- No, the header file only declares function. The definition is in library which is linked by the linker.
Question-25. Explain modular programming ?
Answer- Dividing the program in to sub programs (modules/function) to achieve the given task is modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions.
Question-26. What is a token ?
Answer- A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
Question-27. What is a preprocessor ?
Answer- Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.
Question-28. Explain the use of %i format specifier w.r.t scanf() ?
Answer- Can be used to input integer in all the supported format.
Question-29. How can you print a \ (backslash) using any of the printf() family of functions ?
Answer- Escape it using \ (backslash).
Question-30. Does a break is required by default case in switch statement ?
Answer- Yes, if it is not appearing as the last case and if we do not want the control to flow to the following case after default if any.
Question-31. When to user -> (arrow) operator ?
Answer- If the structure/union variable is a pointer variable, to access structure/union elements the arrow operator is used.
Question-32. What are bit fields ?
Answer- We can create integer structure members of differing size apart from non-standard size using bit fields. Such structure size is automatically adjusted with the multiple of integer size of the machine.
Question-33. What are command line arguments ?
Answer- The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. First argument represents the count of arguments (below in count) and updated automatically by operating system.
main( int count, char *args[]) {
}
Question-34. What are the different ways of passing parameters to the functions? Which to use when ?
Answer- • Call by value − We send only values to the function as parameters.
We choose this if we do not want the actual parameters to be modified with formal parameters but just used.
• Call by reference − We send address of the actual parameters instead of values.
We choose this if we do want the actual parameters to be modified with formal parameters
Question-35. What is the purpose of built-in stricmp() function ?
Answer- It compares two strings by ignoring the case.
Question-36. Describe the file opening mode “w+” ?
Answer- Opens a file both for reading and writing. If a file is not existing it creates one, else if the file is existing it will be over written.
Question-37. Where the address of operator (&) cannot be used ?
Answer- It cannot be used on constants.
It cannot be used on variable which are declared using register storage class.
Question-38. Is FILE a built-in data type ?
Answer- No, it is a structure defined in stdio.h.
Question-39. What is reminder for 5.0 % 2 ?
Answer- Error, It is invalid that either of the operands for the modulus operator (%) is a real number.
Question-40. How many operators are there under the category of ternary operators ?
Answer- There is only one operator and is conditional operator (? : ).
Question-41. Which key word is used to perform unconditional branching ?
Answer- goto.
Question-42. What is a pointer to a function? Give the general syntax for the same ?
Answer- A pointer holding the reference of the function is called pointer to a function. In general it is declared as follows.
T (*fun_ptr) (T1,T2…); Where T is any date type.
Once fun_ptr refers a function the same can be invoked using the pointer as follows.
fun_ptr();
[Or]
(*fun_ptr)();
Question-43. Explain the use of comma operator (,) ?
Answer- Comma operator can be used to separate two or more expressions.
Eg: printf(“hi”) , printf(“Hello”);
Question-44. What is a NULL statement ?
Answer- A null statement is no executable statements such as ; (semicolon).
Eg: int count = 0;
while( ++count<=10 ) ;
Above does nothing 10 times.
Question-45. What is a static function ?
Answer- A function’s definition prefixed with static keyword is called as a static function. You would make a function static if it should be called only within the same source code.
Question-46. Which compiler switch to be used for compiling the programs using math library with gcc compiler ?
Answer- Opiton –lm to be used as > gcc –lm
Question-46. Which operator is used to continue the definition of macro in the next line ?
Answer- Backward slash (\) is used.
E.g. #define MESSAGE "Hi, \
Welcome to C"
Question-47. Which operator is used to receive the variable number of arguments for a function ?
Answer- Ellipses (…) is used for the same. A general function definition looks as follows
void f(int k,…) {
}
Question-48. What is the problem with the following coding snippet ?
Answer-char *s1 = "hello",*s2 = "welcome";
strcat(s1,s2);
s1 points to a string constant and cannot be altered.
Question-49. Which built-in library function can be used to re-size the allocated dynamic memory ?
Answer-realloc().
Question-50. Define an array ?
Answer- Array is collection of similar data items under a common name.
Question-51. What are enumerations ?
Answer- Enumerations are list of integer constants with name. Enumerators are defined with the keyword enum.
Question-52. Which built-in function can be used to move the file pointer internally ?
Answer- fseek()
Question-53. What is a variable ?
Answer- A variable is the name storage.
Question-54. Who designed C programming language ?
Answer- Dennis M Ritchie.
Question-55. C is successor of which programming language ?
Answer- B
Question-56. What is the full form of ANSI ?
Answer- American National Standards Institute.
Question-57. Which operator can be used to determine the size of a data type or variable ?
Answer- sizeof.
Question-58. Can we assign a float variable to a long integer variable ?
Answer- Yes, with loss of fractional part.
Question-59. Is 068 a valid octal number ?
Answer- No, it contains invalid octal digits.
Question-60. What it the return value of a relational operator if it returns any ?
Answer- Return a value 1 if the relation between the expressions is true, else 0.
Question-61. How does bitwise operator XOR works ?
Answer- If both the corresponding bits are same it gives 0 else 1.
Question-62. What is an infinite loop ?
Answer- A loop executing repeatedly as the loop-expression always evaluates to true such as
while(0 == 0) {
}
Quedtion-63. Can variables belonging to different scope have same name ? If so show an example.
Answer- Variables belonging to different scope can have same name as in the following code snippet.
int var;
void f() {
int var;
}
main() {
int var;
}
Question-64. What is the default value of local and global variables ?
Answer- Local variables get garbage value and global variables get a value 0 by default.
Question-65. Can a pointer access the array ?
Answer- Pointer by holding array’s base address can access the array.
Question-66. What are valid operations on pointers ?
Answer- The only two permitted operations on pointers are
• Comparision ii) Addition/Substraction (excluding void pointers)
Question-67. What is a string length ?
Answer- It is the count of character excluding the ‘\0’ character.
Question-68. What is the built-in function to append one string to another ?
Answer- strcat() form the header string.h
Question-69. Which operator can be used to access union elements if union variable is a pointer variable ?
Answer- Arrow (->) operator.
Question-70. Explain about ‘stdin’ ?
Answer- stdin in a pointer variable which is by default opened for standard input device.
Question-71. Name a function which can be used to close the file stream ?
Answer- fclose().
Question-72. What is the purpose of #undef preprocessor ?
Answer- It be used to undefine an existing macro definition.
Question-73. Define a structure ?
Answer- A structure can be defined of collection of heterogeneous data items.
Question-74. Name the predefined macro which be used to determine whether your compiler is ANSI standard or not ?
Answer- __STDC__
Question-75. What is typecasting ?
Answer- Typecasting is a way to convert a variable/constant from one type to another type.
Question-76. What is recursion ?
Answer- Function calling itself is called as recursion.
Question-77. Which function can be used to release the dynamic allocated memory ?
Answer- free().
Question-78. What is the first string in the argument vector w.r.t command line arguments ?
Answer- Program name.
Question-79. How can we determine whether a file is successfully opened or not using fopen() function ?
Answer- On failure fopen() returns NULL, otherwise opened successfully.
Question-80. What is the output file generated by the linker ?
Answer- Linker generates the executable file.
Question-81. What is the maximum length of an identifier ?
Answer- Ideally it is 32 characters and also implementation dependent.
Question-82. What is the default function call method ?
Answer- By default the functions are called by value.
Question-83. Functions must and should be declared. Comment on this ?
Answer- Function declaration is optional if the same is invoked after its definition.
Question-84. When the macros gets expanded ?
Answer- At the time of preprocessing.
Question-85. Can a function return multiple values to the caller using return reserved word ?
Answer- No, only one value can be returned to the caller.
Question-86. What is a constant pointer ?
Answer- A pointer which is not allowed to be altered to hold another address after it is holding one.
Question-87. A pointer which is not allowed to be altered to hold another address after it is holding one ?
Answer- Void.
Question-88. Can the structure variable be initialized as soon as it is declared ?
Answer- Yes, w.r.t the order of structure elements only.
Question-89. Is there a way to compare two structure variables ?
Answer- There is no such. We need to compare element by element of the structure variables.
Question-90. Which built-in library function can be used to match a patter from the string ?
Answer- Strstr().
Question-91. What is difference between far and near pointers ?
Answer- In first place they are non-standard keywords. A near pointer can access only 2^15 memory space and far pointer can access 2^32 memory space. Both the keywords are implementation specific and are non-standard.
Question-92. Can we nest comments in a C code ?
Answer- No, we cannot.
Question-93. Which control loop is recommended if you have to execute set of statements for fixed number of times ?
Answer- for – Loop.
Question-94. What is a constant ?
Answer- A value which cannot be modified is called so. Such variables are qualified with the keyword const.
Question-95. Can we use just the tag name of structures to declare the variables for the same ?
Answer- No, we need to use both the keyword ‘struct’ and the tag name.
Question-96. Can the main() function left empty ?
Answer- Yes, possibly the program doing nothing.
Question-97. Can one function call another ?
Answer- Yes, any user defined function can call any function.
Question-98. Apart from Dennis Ritchie who the other person who contributed in design of C language ?
Answer- Brain Kernighan.
Question-99. What is C language ?
Answer- C is a mid level and procedural programming language.
Question-100. Why C is known as a mother language ?
Answer- C is known as a mother language because most of the compilers, kernals and JVMs are written in C language.
Question-101. Why C is called a mid level programming language ?
Answer- It supports the feature of both low-level and high level languages that is why it is known as a mid level programming language.
Question-102. Who is the founder of C language ?
Answer- Dennis Ritchie.
Question-103. When C langauge was developed ?
Answer- C language was developed in 1972 at bell laboratories of AT&T.
Question-104. What are the features of C language ?
Answer- The main features of C language are given below:
Simple
Portable
Mid Level
Structured
Fast Speed
Memory Management
Extensible
Question-105. What is the use of printf() and scanf() functions ?
Answer- The printf() function is used for output and scanf() function is used for input.
Question-106. What is the difference between local variable and global variable in C ?
Answer- Local variable: A variable which is declared inside function or block is known as local variable.
Global variable: A variable which is declared outside function or block is known as global variable.
int value=50;//global variable
void function1(){
int x=20;//local variable
}
Question-107. What is the use of static variable in C ?
Answer- A variable which is declared as static is known as static variable. The static variable retains its value between multiple function calls.
void function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d\n",x);//will always print 11
printf("%d\n",y);//will always increment value, it will print 11, 12, 13 and so on
}
Question-108. What is the use of function in C ?
Answer- A function in C language provides modularity. It can be called many times. It saves code and we can reuse the same code many times.
Question-109. What is the difference between call by value and call by reference in C ?
Answer- We can pass value to function by one of the two ways: call by value or call by reference. In case of call by value, a copy of value is passed to the function, so original value is not modified. But in case of call by reference, an address of value of passed to the function, so original value is modified.
Question-110. What is recursion in C ?
Answer- Calling the same function, inside function is known as recursion. For example:
void function1(){
function1();//calling same function
}
Question-111. What is array in C ?
Answer- Array is a group of similar types of elements. It has contiguous memory location. It makes the code optimized, easy to traverse and easy to sort.
Question-112. What is pointer in C ?
Answer- A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast.
Question-113. What are the usage of pointer in C ?
Answer- Accessing array elements
Dynamic memory allocation
Call by Reference
Data Structures like tree, graph, linked list etc.
Question-114. What is far pointer in C ?
Answer- A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer.
Question-115. What is static memory allocation ?
Answer- In case of static memory allocation, memory is allocated at compile time and memory can't be increased while executing the program. It is used in array.
Question-116. What is dynamic memory allocation ?
Answer- In case of dynamic memory allocation, memory is allocated at run time and memory can be increased while executing the program. It is used in linked list.
Question-117. What functions are used for dynamic memory allocation in C language ?
Answer- malloc()
calloc()
realloc()
free()
Question-118. What is structure ?
Answer- Structure is a user-defined data type that allows to store multiple types of data in a single unit. It occupies the sum of memory of all members.
Question-119. What is union ?
Answer- Like Structure, union is a user-defined data type that allows to store multiple types of data in a single unit. But it doesn't occupies the sum of memory of all members. It occupies the memory of largest member only.
Question-120. Can we compile a program without main() function ?
Answer- Yes, we can compile but it can't be executed.
But, if we use #define, we can compile and run C program without using main() function. For example:
#include
#define start main
void start() {
printf("Hello");
}
Question-121. What is the acronym for ANSI ?
Answer- American National Standard Institute.
Question-122. What is the difference between getch() and getche() ?
Answer- The getch() function reads a single character from keyboard. It doesn't uses any buffer, so entered data is not displayed on the output screen.
The getche() function reads a single character from keyword but data is displayed on the output screen. Press Alt+f5 to see the entered character.
Question-123. What is new line escape sequence ?
Answer- The new line escape sequence is represented by "\n". It inserts a new line on the output screen.
Question-124. What is the difference between near, far and huge pointers ?
Answer- A virtual address is composed of selector and offset.
A near pointer doesn't have explicit selector whereas far and huge pointers have explicit selector. When you perform pointer arithmetic on far pointer, selector is not modified but in case of huge pointer it can be modified.
These are the non-standard keywords and implementation specific. These are irrelevant in modern platform.
Question-125. What is the maximum length of an identifier ?
Answer- It is 32 characters ideally but implementation specific.
Question-126. What is typecasting ?
Answer- Converting one data type into another is known as typecasting. For example:
float f=3.4;
int a=(int)f;//typecasting
Question-127. What are the functions to open and close file in C language ?
Answer- The fopen() function is used to open file whereas fclose() is used to close file.
Question-128. Can we access array using pointer in C language ?
Answer- Yes, by holding the base address of array into pointer, we can access the array using pointer.
Question-129. What is infinite loop ?
Answer- A loop running continuously for indefinite number of times is called infinite loop.
Infinite For Loop:
for(;;){
//code to be executed
}
Infinite While Loop:
while(1){
//code to be executed
}
Infinite Do-While Loop:
do{
//code to be executed
}while(1);
Question-130. Write a program to print "hello world" without using semicolon ?
Answer- There are various ways to do so. Let's see a program to print "hello world" using if.
#include
void main(){
if(printf("hello world")){}
}
Question-131. Write a program to swap two numbers without using third variable ?
Answer-
#include
#include
main()
{
int a=10, b=20;
clrscr();
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
getch();
}
Question-132. Write a program to print fibonacci series without using recursion ?
Answer-
#include
#include
void main()
{
int n1=0,n2=1,n3,i,number;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i
Question-133. Write a program to print fibonacci series using recursion ?
Answer-
#include
#include
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
void main(){
int n;
clrscr();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
getch();
}
Question-134. Write a program to check prime number in C Programming ?
Answer-
#include
#include
void main()
{
int n,i,m=0,flag=0;
clrscr();
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
getch();
}
Question-135. a program to check palindrome number in C Programming ?
Answer-
#include
#include
main()
{
int n,r,sum=0,temp;
clrscr();
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
getch();
}
Question-136. Write a program to print factorial of given number without using recursion ?
Answer-
#include
#include
void main(){
int i,fact=1,number;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
getch();
}
Question-137. Write a program to print factorial of given number using recursion ?
Answer-
#include
#include
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
clrscr();
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
getch();
}
Question-138. Write a program to check armstrong number in C ?
Answer-
#include
#include
main()
{
int n,r,sum=0,temp;
clrscr();
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
getch();
}
Question-139. Write a program to reverse a given number in C ?
Answer-
#include
#include
main()
{
int n, reverse=0, rem;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
getch();
}