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

Top Blogs
C Functions ! What is a Function Published at:- Types of Function in C ! Library Function in C ! User Defined Function In C ! Function Definition Published at:- Functions that return multiple values -C Example Published at:- Functions with arguments and return values -C Examples Published at:- Functions with arguments and no return values. Published at:- Example of Function with no return type and no argument Published at:- Loops in C Published at:- Structure in C: Introduction Published at:- C Memory Management ! Dynamic memory allocation Published at:- Learn C Programming language with example Published at:- C Interview Questions And Answers Published at:- What values are printed when we run following? Published at:- C Program example: Input a number and print sum of its digits Published at:- Pointer declaration in C ,Address operator Published at:- C Language Interview Question and Answers Published at:- Benefits of C language over other programming languages Published at:- History of C Language : Introduction to C Programming Language Published at:- How does C Programming Language Work Published at:- Importance of C Programming Language Published at:- Input and Output Functions in C Published at:- Introduction to Implementation of Queue using Linked List Published at:- First C Program Published at:- Inception Of C Language Tutorial for Beginners Published at:- The C Compiler work in C language and its important Published at:- Program Structure with “Hello World” Example Published at:- C Programming Interview Questions Set 1 Published at:- C Programming Interview Questions Set 2 Published at:- C Programming Interview Questions Set 3 Published at:- C Programming Interview Questions Set 4 Published at:- C Programming Interview Questions Set 5 Published at:- C Programming Interview Questions Set 6 Published at:- C Programming Interview Questions Set 7 Published at:- C Programming Interview Questions Set 8 Published at:- C Programming Interview Questions Set 9 Published at:-
R4R.co.in Team
The content on R4R is created by expert teams.