1.
#include “stdio.h”
int main()
{
int i = 5;
printf(“%d %d %d”, i++, i++, i++);
return 0;
}
(a) 7 6 5
(b) 5 6 7
(c) 7 7 7
(d) Compiler Dependent
Answer

(d) When parameters are passed to a function, the value of every parameter is evaluated before being passed to the function.
What is the order of evaluation of parameters – left-to-right or right-to-left?
If evaluation order is left-to-right, then output should be 5 6 7 and if the evaluation order is right-to-left, then output should be 7 6 5. Unfortunately, there is no fixed order defined by C standard. A compiler may choose to evaluate either from left-to-right. So the output is compiler dependent.

2.
#include “stdio.h”
int main()
{
printf(“%d”, main);
return 0;
}
(a) Address of main function
(b) Compiler Error
(c) Runtime Error
(d) Some random value

Answer
(a)Explanation: Name of the function is actually a pointer variable to the function and prints the address of the function.

3.
void f1 (int a, int b)
{
int c;
c=a; a=b; b=c;
}
void f2 (int *a, int *b)
{
int c;
c=*a; *a=*b;*b=c;
}
int main()
{
int a=4, b=5, c=6;
f1(a, b);
f2(&b, &c);
printf (“%d”, c-a-b);
return 0;
}

(a) -5
(b) -4
(c) 5
(d) 3

Answer
(a)
Explanation: The function call to to f1(a, b) won’t have any effect as the values are passed by value.
The function call f2(&b, &c) swaps values of b and c. So b becomes 6 and c becomes 5. Value of c-a-b becomes 5-4-6 which is -5.

4.

#include “stdio.h”
int main()
{
void demo();
void (*fun)();
fun = demo;
(*fun)();
fun();
return 0;
}

void demo()
{
printf(“ScaBhopal”);
}

(a) ScaBhopal
(b) ScaBhopal ScaBhopal
(c) Compiler Error
(d) Blank Screen

Answer
(b) This is a simple program with function pointers. fun is assigned to point to demo. So the two statements “(*fun)();” and “fun();” mean the same thing.

5.
#include “stdio.h”
int a;
int func();
int main()
{
while(a)
{
func();
main();
}
printf(“good\n”);
return 0;
}
int func()
{
printf(“best”);
}
(a) Compilation error
(b) Best
(c) Good
(d) None
Answer

(c) int a The variable a is declared as an integer type.
int func(); This prototype tells the compiler that the function func() does not accept any arguments and it returns an integer value.
while(a) The value of a is not initialized so this while condition is failed. So, it does not execute the while block.
printf(“good\n”); It prints “good”.
Hence the output of the program is “good”.

6.
#include
int reverse(int);
int main()
{
int num=6;
reverse(num);
return 0;
}
int reverse(int num)
{
if(num == 0)
return 0;
else
printf(“%d,”, num);
reverse (num–);
}
(a) 6,5,4,3,2,1
(b) 6,5,4,3,2,1,0
(c) 1,2,3,4,5,6
(d) Infinite loop
Answer

(d) int num=6 The variable no is declared as integer type and initialized to 6.
reverse(num); becomes reverse(6); It calls the function reverse() with ‘6’ as parameter.
The function reverse accept an integer number 6 and it returns ‘0’(zero) if(6 == 0) if the given number is ‘0’(zero) or else printf(“%d,”, num); it prints that number 6 and calls the function reverse(6);.
The function runs infinetely because the there is a post-decrement operator is used. It will not decrease the value of ‘n’ before calling the reverse() function. So, it calls reverse(6) infinitely.
Note: If we use pre-decrement operator like reverse(–num), then the output will be 6,5, 4, 3, 2, 1. Because before calling the function, it decrements the value of ‘num’.

7.
#include
int i;
int fun1(int);
int fun2(int);
int main()
{
extern int j;
int i=3;
fun1(i);
printf(“%d,”, i);
fun2(i);
printf(“%d”, i);
return 0;
}
int fun1(int j)
{
printf(“%d,”, ++j);
return 0;
}
int fun2(int i)
{
printf(“%d,”, ++i);
return 0;
}
int j=1;
(a) 3,4,4,3
(b) 4,3,4,3
(c) 3,3,4,4
(d) 4,3,3,4
Answer

(b) int i The variable i is declared as an global and integer type.
Int fun1(int); This prototype tells the compiler that the fun1() accepts the
one integer parameter and returns the integer value.
int fun2(int); This prototype tells the compiler that the fun2() accepts the
one integer parameter and returns the integer value.
extern int j Inside the main function, the extern variable j is declared and defined in another source file.
int i=3 The local variable i is defines as an integer type and initialized to 3.
fun1(i) The fun1(i) increements the given value of variable i prints it. Here fun1(i) becomes fun1(3) hence it prints ‘4’ then the control is given back to the main function.
printf(“%d,”, i); It prints the value of local variable i. So, it prints ‘3’.
fun2(i); The fun2(i) increements the given value of variable i prints it. Here fun2(i) becomes fun2(3) hence it prints ‘4’ then the control is given back to the main function.
printf(“%d,”, i); It prints the value of local variable i. So, it prints ‘3’.
Hence the output is “4 3 4 3”.

8.
#include “stdio.h”
int func1(int);
int main()
{
int p=35;
p = func1(p=func1(p=func1(p)));
printf(“p=%d\n”, p);
return 0;
}
int func1(int p)
{
p++;
return p;
}
(a) P=38
(b) P=35
(c) P=37
(d) P=36
Answer

(a) int p=35; The variable p is declared as an integer type and initialized to 35.
p= func1(p=func1(p=func1(p))); The func1(p) increment the value of p by 1 and return it. Here the func1(p) is called 3 times. Hence it increments value of p = 35 to 38. The result is stored in the variable p = 38.
printf(“p=%d\n”, p); It prints the value of variable p “38”.

9.
#include “stdio.h”
void fun(char**);
int main()
{
char *argv[] = {“ab”, “cd”, “ef”, “gh”};
fun(argv);
return 0;
}
void fun(char **p)
{
char *t;
t = (p+= sizeof(int))[-1];
printf(“%s\n”, t);
}
(a) Ab
(b) Ef
(c) Gh
(d) Cd
Answer

(d) initally **p contains address of argv[0] element ie “ab”.it is
then incremented by 2 (size of int given as 2 bytes).now it point to “ef”.It is then decremented by -1.so finally points to “cd”.so it print value of t as “cd”

10.
#include “stdio.h”
int main()
{
int func(int);
int x = func(10);
printf(“%d\n”, –x);
return 0;
}
int func(int x)
{
return (x++);
}

(a) 10
(b) 11
(c) 9
(d) 8
Answer

(c) int func(int); Here we declare the prototype of the function
func(). int x = func(10); The variable i is declared as an integer type
and the result of the func(10) will be stored in the variable x.
int func(int x){ return (x++); } Inside the func() we are returning a
value return(x++). It returns 10. because x++ is the post-increment
operator.Then the control back to the main function and the value 10 is
assigned to variable x. printf(“%d\n”, –x); Here –x denoted pre-increment.
Hence it prints the value 9.
functions in C