C Programming Interview Questions Set 2
Categories: C Programming language
Ques. What is the use of printf() and scanf() functions?
printf(): The printf() function is used to print the integer, character, float and string values on to the screen.
Following are the format specifier:
a) %d: It is a format specifier used to print an integer value.
b) %s: It is a format specifier used to print a string.
c) %c: It is a format specifier used to display a character value.
d) %f: It is a format specifier used to display a floating point value.
scanf(): The scanf() function is used to take input from the user.
Ques. What is the difference between the local variable and global variable in C?
Ans. Following are the differences between a local variable and global variable:
Basis for comparisonLocal variableGlobal variable
a. DeclarationA variable which is declared inside function or block is known as a local variable.A variable which is declared outside function or block is known as a global variable.
b. ScopeThe scope of a variable is available within a function in which they are declared.The scope of a variable is available throughout the program.
c. AccessVariables can be accessed only by those statements inside a function in which they are declared.Any statement in the entire program can access variables.
d. LifeLife of a variable is created when the function block is entered and destroyed on its exit.Life of a variable exists until the program is executing.
e. StorageVariables are stored in a stack unless specified.The compiler decides the storage location of a variable.
Ques. What is the use of a static variable in C?
Ans. Following are the uses of a static variable:
a. A variable which is declared as static is known as a static variable. The static variable retains its value between multiple function calls.
b. Static variables are used because the scope of the static variable is available in the entire program. So, we can access a static variable anywhere in the program.
c. The static variable is initially initialized to zero. If we update the value of a variable, then the updated value is assigned.
d. The static variable is used as a common value which is shared by all the methods.
e. The static variable is initialized only once in the memory heap to reduce the memory usage.
Ques. What is the use of the function in C?
Ans. Uses of C function are:
a) C functions are used to avoid the rewriting the same code again and again in our program.
b) C functions can be called any number of times from any place of our program.
c) When a program is divided into functions, then any part of our program can easily be tracked.
d) C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes the C program more understandable.
Ques What is the difference between call by value and call by reference in C?
Ans. Following are the differences between a call by value and call by reference are:
Call by valueCall by reference
DescriptionWhen a copy of the value is passed to the function, then the original value is not modified.When a copy of the value is passed to the function, then the original value is modified.
Memory locationActual arguments and formal arguments are created in separate memory locations.Actual arguments and formal arguments are created in the same memory location.
SafetyIn this case, actual arguments remain safe as they cannot be modified.In this case, actual arguments are not reliable, as they are modified.
ArgumentsThe copies of the actual arguments are passed to the formal arguments.The addresses of actual arguments are passed to their respective formal arguments.
Example of call by value:
#include <stdio.h>
void change(int,int);
int main()
{
int a=10,b=20;
change(a,b); //calling a function by passing the values of variables.
printf("Value of a is: %d",a);
printf("\n");
printf("Value of b is: %d",b);
return 0;
}
void change(int x,int y)
{
x=13;
y=17;
}
Output:
Value of a is: 10
Value of b is: 20
Ques. What is recursion in C?
When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.
Recursive function comes in two phases:
a) Winding phase
b) Unwinding phase
a) Winding phase: When the recursive function calls itself, and this phase ends when the condition is reached.
b) Unwinding phase: Unwinding phase starts when the condition is reached, and the control returns to the original call.
Example of recursion
#include <stdio.h>
int calculate_fact(int);
int main()
{
int n=5,f;
f=calculate_fact(n); // calling a function
printf("factorial of a number is %d",f);
return 0;
}
int calculate_fact(int a)
{
if(a==1)
{
return 1;
}
else
return a*calculate_fact(a-1); //calling a function recursively.
}
Output:
factorial of a number is 120