C Technical Interview Questions – Functions

These C Technical Interview Questions post providing basic, intermediate and Hard level examples in C Functions. The Practice of these examples makes you good in C programming.


Find the output of following program
#include<stdio.h>
void main(){
int i=3;
i = f1(i);
printf(“%d\n”,i);
}
int f1(int i){
return f2(i++);
}
int f2(int i) {
return ++i ;
}

  1. 5
  2. 4
  3. 3
  4. Compile time error

Answer :

Ans : 2 ; In f1-function, value will be incremented after return.

What is the output of following code?
#include<stdio.h>
void fun(int);
void main()
{
int x=2;
fun(x++);
}
void fun(int x)
{
printf(“x value : %d\n”,x++);
}

  1. 2
  2. 3
  3. 4
  4. Compile time error

Answer :

Ans : 1 ; Function will be called as fun(2) and we are printing x++ value in the called function.

Find the output of following program
void main()
{
abc(3);
}
void abc(int a)
{
printf(“%d”,a);
if(a)
{
abc(a-1);
}
printf(“%d”,a);
}

  1. 3,2,1,0
  2. Infinite loop
  3. 3,2,1,0,0
  4. 3,2,1,0,0,1,2,3

Answer :

Ans : 4 ; Recursive function stops only where it has started.

Find the output of following program
#include<stdio.h>
void fun(int, int);
void main()
{
int i=3;
fun(i , i++);
printf(“%d\n”,i);
}
void fun(int i, int j)
{
printf(“%d\n”,i++);
printf(“%d\n”,j–);
}

  1. 4,3,4
  2. 4,3,3
  3. 4,4,4
  4. 3,4,3

Answer :

Ans : 1 ; fun-function will be called as fun(4, 3). In a function call, elements will be evaluated from right to left

What is the output of following code?
#include <stdio.h>
#include <math.h>
int main()
{
double n=2.3 ;
printf(“%.2lf\n” , ceil(n));
return 0;
}

  1. 2.30
  2. 2.50
  3. 3.000000
  4. 3.00

Answer :

Ans : 4 ; ceil function always increase the value to next number.

What is the output of following code?
#include <stdio.h>
void abc(int a)
{
++a;
printf(“%d”,a);
}
void main()
{
int a=10;
abc(++a);
abc(a++);
printf(“%d”,a);
}

  1. 12,11,12
  2. 12,12,12
  3. 12,11,13
  4. 12,11,12

Answer :

Ans : 2 ; After calling the function a value will be increased in the main function

What is the output of following code?
#include <stdio.h>
#include <math.h>
int main()
{
double n=2.8 ;
printf(“%.3lf\n” , floor(n));
return 0;
}

  1. 2.000
  2. 2.000000
  3. 2.500000
  4. 2.50

Answer :

Ans : 1 ; floor() function decrease the value by 1 and we are printing upto 3 decimal values.

Find the output of following program
#include <stdio.h>
void f1()
{
int a=0;
++a;
printf(“%d”,a);
}
int main()
{
int a=10;
f1();
f1();
f1();
printf(“%d”,a);
return 0;
}

  1. 10,10,10,11
  2. 10,11,12,13
  3. 1,1,1,10
  4. 10,11,12,12

Answer :

Ans : 3 ; f1() function always increase and display local variable a=0

Find the output of following program
#include<stdio.h>
void fun(int);
void main()
{
int i=3;
fun(i);
printf(“%d\n”,i);
}
void fun(int i)
{
++i ;
}

  1. garbage value
  2. 4
  3. 3
  4. Compile time error

Answer :

Ans : 3 ; fun() function increase the value of local variable, hence i value in main function is 3 only.

What is the output of following code?
#include <stdio.h>
void m(void);
void main()
{
m();
void m()
{
printf(“hi”);
}
}

  1. infinite times it prints hi
  2. hi , hi
  3. hi
  4. Compile time error

Answer :

Ans : 4 ; Function cannot be placed inside another function

Functions – Technical – Part 2

Share this