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.


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

  1. 3,3,3
  2. 4,4,4
  3. 4,5,6
  4. static variable cannot be declared inside function

Answer :

Ans : 3 ; static variable can be declared locally and the variable will be deleted only when program ends.

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

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

Answer :

Ans : 1 ; function elements will be evaluated from right to left.

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

  1. 6,7,6
  2. 6,6,7
  3. 7,7,7
  4. Compile time error

Answer :

Ans : 4 ; If static variable is local, we cannot access from another function.

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

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

Answer :

Ans : 2 ; fun() function will be called first time as fun(3,3) and second time fun(3,3).

What is the output of following code?
#include <stdio.h>
void main()
{
int x = 3;
x++;
if (x <= 5)
{
printf(“hi\n”);
main();
}
}

  1. hi, hi
  2. hi, hi, hi
  3. infinite loop
  4. Compile time error

Answer :

Ans : 3 ; When we call main(), everytime local variable x initializes with 3

Find the output of following program
#include<stdio.h>
void xyz(int);
void abc(int);
void main()
{
abc(2);
}
void xyz(int x)
{
printf(“%d”,x);
if(x)
{
xyz(x-1);
}
printf(“%d”,x);
}
void abc(int a)
{
printf(“%d”,a);
xyz(a);
if(a)
{
abc(a-1);
}
printf(“%d”,a);
}

  1. 221001211001000012
  2. 22100121100
  3. 221001211
  4. 22100

Answer :

Ans : 1 ; 

Functions – Objective – Part 1

Share this