Você está na página 1de 356

93

11
2 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
C Programming Questions and Answers

1. Declarations and Initializations

2. Control Instructions

3. Expressions

4. Floating Point Issues

5. Functions

6. C Preprocessor

7. Pointers

8. Arrays

9. Strings

10. Structures, Unions, Enums

11. Input / Output

12. Command Line Arguments

13. Bitwise Operators

14. Typedef

15. Const

16. Memory Allocation

17. Variable Number of Arguments

18. Complicated Declarations

19. Library Functions

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 3


4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Declarations and Initializations

1. What is the output of the program given below?

#include <stdio.h>

int main()
{
enum status {pass, fail, atkt};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = atkt;
stud3 = fail;
printf("%d, %d, %d\n", stud1, stud2, stud3);
return 0;
}

A.

0, 1, 2

B.

1, 2, 3

C.

0, 2, 1

D.

1, 3, 2

Answer: Option C

Explanation:

enum takes the format like {0, 1, 2 ...) so pass = 0, fail = 1, atkt = 2.

stud1 = pass (value is 0)

stud2 = atkt (value is 2)

stud3 = fail (value is 1)

Hence it prints 0, 2, 1

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 5


2. What will be the output of the program in 16 bit platform (Turbo C
under DOS)?

#include <stdio.h>

int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}

A.

B.

C.

vary from compiler

D.

Linker Error : Undefined symbol 'i'

Answer: Option D

Explanation:

Linker Error : Undefined symbol 'i'.

The statement

extern int i;

specifies to the compiler that the memory for 'i' is allocated in some
other program and that address will be given to the current program at
the time of linking. But linker finds that no other variable of name 'i'
is available in any other program with memory space allocated for it.

Hence a linker error has occurred.

6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What is the output of the program?

#include <stdio.h>

int main()
{
extern int a;
printf("%d\n", a);
return 0;
}

int a = 20;

A.

20

B.

C.

Garbage Value

D.

Error

Answer: Option A

Explanation:

extern int a;

indicates that the variable a is defined elsewhere, usually in a separate


source code module.

printf("%d\n", a);

it prints the value of local variable int a = 20.

Because, whenever there is a conflict between local variable and global


variable, local variable gets the highest priority. So it prints 20.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 7


4. What is the output of the program in Turbo C (in DOS 16-bit OS)?

#include <stdio.h>

int main()
{
char *s1;
char far *s2;
char huge *s3;
printf("%d, %d, %d\n", sizeof(s1), sizeof(s2), sizeof(s3));
return 0;
}

A.

2, 4, 6

B.

4, 4, 2

C.

2, 4, 4

D.

2, 2, 2

Answer: Option C

Explanation:

Any pointer size is 2 bytes (only 16-bit offset).

So, char *s1 = 2 bytes.

So, char far *s2; = 4 bytes.

So, char huge *s3; = 4 bytes.

A far, huge pointer has two parts: a 16-bit segment value and a 16-bit
offset value.

Since C is a compiler dependent language, it may give different output in


other platforms. The above program works fine in Windows (TurboC), but
error in Linux (GCC Compiler).

8 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What is the output of the program?

#include <stdio.h>

int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf("%d, %f\n", e.age, e.sal);
return 0;
}

A.

0, 0.000000

B.

Garbage values

C.

Error

D.

None of above

Answer: Option A

Explanation:

When an automatic structure is partially initialized remaining elements


are initialized to 0 (zero).

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 9


6. What will be the output of the program?

#include <stdio.h>

int X = 40;
int main()
{
int X = 20;
printf("%d\n", X);
return 0;
}

A.

20

B.

40

C.

Error

D.

No Output

Answer: Option A

Explanation:

Whenever there is conflict between a local variable and global variable,


the local variable gets priority.

10 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What is the output of the program

#include <stdio.h>

int main()
{
int x = 10, y = 20, z = 5, i;
i = x < y < z;
printf("%d\n", i);
return 0;
}

A.

B.

C.

Error

D.

None of these

Answer: Option B

Explanation:

Since x < y turns to be TRUE it is replaced by 1. Then 1 < z is compared


and to be TRUE. The 1 is assigned to i.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 11


8. What is the output of the program?

#include <stdio.h>

int main()
{
extern int fun(float);
int a;
a = fun(3.14);
printf("%d\n", a);
return 0;
}

int fun(int aa)


{
return (int)++aa;
}

A.

B.

3.14

C.

D.

E.

Compile Error

Answer: Option E

Explanation:

2 Errors

1. Type mismatch in redeclaration of fun.

2. Type mismatch in parameter aa.

12 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What is the output of the program?

#include <stdio.h>

int main()
{
int a[5] = {2, 3};
printf("%d, %d, %d\n", a[2], a[3], a[4]);
return 0;
}

A.

Garbage Values

B.

2, 3, 3

C.

3, 2, 2

D.

0, 0, 0

Answer: Option D

Explanation:

When an automatic array is partially initialized, the remaining elements


are initialized to 0.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 13


10. What is the output of the program?

#include <stdio.h>

int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}

A.

3, 2, 515

B.

515, 2, 3

C.

3, 2, 5 D.

None of these

Answer: Option A

Explanation:

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);

It prints the value ofu.ch[0] = 3, u.ch[1] = 2 and it prints the value of


u.i means the value of entire union size.

So the output is 3, 2, 515.

14 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


11. In the following program how long will the for loop get executed?

#include <stdio.h>

int main()
{
int i = 5;
for(; scanf("%s", &i); printf("%d\n", i));
return 0;
}

A.

The for loop would not get executed at all

B.

The for loop would get executed only once

C.

The for loop would get executed 5 times

D.

The for loop would get executed infinite times

Answer: Option D

Explanation:

During the for loop execution scanf() ask input and then printf() prints
that given input. This process will be continued repeatedly because,
scanf() returns the number of input given, the condition is always
true(user gives a input means it reurns '1').

Hence this for loop would get executed infinite times.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 15


12. What will be the output of the program?

#include <stdio.h>

int main()
{
int X = 40;
{
int X = 20;
printf("%d ", X);
}
printf("%d\n", X);
return 0;
}

A.

40 40

B.

20 40

C.

20

D.

Error

Answer: Option B

Explanation:

In case of a conflict between a local variable and global variable, the


local variable gets priority.

16 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Control Instructions

1. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 0;
for(; i <= 5; i++);
printf("%d", i);
return 0;
}

A.

0, 1, 2, 3, 4, 5

B.

C.

1, 2, 3, 4

D.

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 17


Explanation:

Step 1:

int i = 0;

here variable i is an integer type and initialized to '0'.

Step 2:

for(; i <= 5; i++);

variable i = 0 is already assigned in previous step. The semicolon at the


end of this for loop tells, "there is no more statement is inside the
loop".

Loop 1: here i = 0, the condition in for(; 0 <= 5; i++) loop satisfies


and then i is incremented by '1'(one)

Loop 2: here i = 1, the condition in for(; 1 <= 5; i++) loop satisfies


and then i is incremented by '1'(one)

Loop 3: here i = 2, the condition in for(; 2 <= 5; i++) loop satisfies


and then i is incremented by '1'(one)

Loop 4: here i = 3, the condition in for(; 3 <= 5; i++) loop satisfies


and then i is increemented by '1'(one)

Loop 5: here i = 4, the condition in for(; 4 <= 5; i++) loop satisfies


and then i is incremented by '1'(one)

Loop 6: here i = 5, the condition in for(; 5 <= 5; i++) loop satisfies


and then i is incremented by '1'(one)

Loop 7: here i = 6, the condition in for(; 6 <= 5; i++) loop fails and
then i is not incremented.

Step 3:

printf("%d", i);

here the value of i is 6.

Hence the output is '6'.

18 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


2. What will be the output of the program?

#include <stdio.h>

int main()
{
char str[] = "C-program";
int a = 5;
printf(a > 10 ? "Ps\n" : "%s\n", str);
return 0;
}

A.

C-program

B.

Ps

C.

Error

D.

None of above

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 19


Explanation:

Step 1: char str[]="C-program"; here variable str contains "C-program".

Step 2: int a = 5; here variable a contains "5".

Step 3: printf(a > 10 ? "Ps\n" : "%s\n", str); this statement can be


written as

if(a > 10)


{
printf("Ps\n");
}
else
{
printf("%s\n", str);
}

Here we are checking a > 10 means 5 > 10. Hence this condition will be
failed. So it prints variable str.

Hence the output is "C-program".

20 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>

int main()
{
int a = 500, b = 100, c;
if(!a >= 400)
b = 300;
c = 200;
printf("b = %d c = %d\n", b, c);
return 0;
}

A.

b = 300 c = 200

B.
b = 100 c = garbage

C.

b = 300 c = garbage

D.

b = 100 c = 200

Answer: Option D

Explanation:

Initially variables a = 500, b = 100 and c is not assigned.

Step 1: if(!a >= 400)

Step 2: if(!500 >= 400)

Step 3: if(0 >= 400)

Step 4: if(FALSE) Hence the if condition is failed.

Step 5: So, variable c is assigned to a value '200'.

Step 6:

printf("b = %d c = %d\n", b, c);

It prints value of b and c. Hence the output is "b = 100 c = 200"

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 21


4. What will be the output of the program?

#include <stdio.h>

int main()
{
unsigned int i = 65535; /* Assume 2 byte integer*/
while(i++ != 0)
printf("%d", ++i);
printf("\n");
return 0;
}

A.

Infinite loop

B.

0 1 2 . . . 65535

C.

0 1 2 . . . 32767 - 32766 -32765 -1 0

D.

No output

Answer: Option A

22 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.

Step 1:

unsigned int i = 65535;

Step 2:

Loop 1: while(i++ != 0) this statement becomes while(65535 != 0). Hence


the while(TRUE) condition is satisfied. Then the

printf("%d", ++i);

prints '1' (variable'i' is already increemented by '1' in while statement


and now increemented by '1' in printf statement)

Loop 2: while(i++ != 0) this statement becomes while(1 != 0). Hence the


while(TRUE) condition is satisfied. Then the

printf("%d", ++i);

prints '3'(variable 'i' is already increemented by '1' in while statement


and now increemented by '1' in printf statement)
. . .
. . .
. . .
The while loop will never stops executing, because variable i will never
become '0'(zero).

Hence it is an 'Infinite loop'.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 23


5. What will be the output of the program?

#include <stdio.h>

int main()
{
int x = 3;
float y = 3.0;
if(x == y)
printf("x and y are equal");
else
printf("x and y are not equal");
return 0;
}

A.

x and y are equal

B.

x and y are not equal

C.

Unpredictable

D.

No output

Answer: Option A

Explanation:

Step 1: int x = 3; here variable x is an integer type and initialized to


'3'.

Step 2: float y = 3.0; here variable y is an float type and initialized


to '3.0'

Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this


condition is satisfied.

Hence it prints "x and y are equal".

24 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


6. What will be the output of the program, if a short int is 2 bytes
wide?

#include <stdio.h>

int main()
{
short int i = 0;
for(i <= 5 && i >= -1; ++i; i > 0)
printf("%u,", i);
return 0;
}

A.

1 . . . 65535

B.

Expression syntax error

C.

No output

D.

0, 1, 2, 3, 4, 5

Answer: Option A

Explanation:

for(i <= 5 && i >= -1; ++i; i > 0)

so expression i <= 5 && i >= -1 initializes for loop. expression ++i is


the loop condition. expression i > 0 is the increment expression.

In

for(i <= 5 && i >= -1; ++i; i > 0)

expression i <= 5 && i >= -1 evaluates to one.

Loop condition always get evaluated to true. Also at this point it


increases i by one.

An increment expression i > 0 has no effect on value of i. So for loop


get executed till the limit of integer (ie. 65535)

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 25


7. What will be the output of the program?

#include <stdio.h>

int main()
{
char ch;
if(ch = printf(""))
printf("It matters\n");
else
printf("It doesn't matters\n");
return 0;
}

A.

It matters

B.

It doesn't matters

C.

matters

D.

No output

Answer: Option B

Explanation:

printf() returns the number of charecters printed on the console.

Step 1: if(ch = printf("")) here printf() does not print anything, so it


returns '0'(zero).

Step 2: if(ch = 0) here variable ch has the value '0'(zero).

Step 3: if(0) Hence the if condition is not satisfied. So it prints the


else statements.

Hence the output is "It doesn't matters".

Note: Compiler shows a warning "possibly incorrect assignment".

26 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


8. What will be the output of the program?

#include <stdio.h>

int main()
{
unsigned int i = 65536; /* Assume 2 byte integer*/
while(i != 0)
printf("%d", ++i);
printf("\n");
return 0;
}

A.

Infinite loop

B.

0 1 2 . . . 65535

C.

0 1 2 . . . 32767 - 32766 -32765 -1 0

D.

No output

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 27


Explanation:

Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.

Step 1:

unsigned int i = 65536;

here variable i becomes '0'(zero). because unsigned int varies from 0 to


65535.

Step 2:

while(i != 0)

this statement becomes while(0 != 0). Hence the while(FALSE) condition is


not satisfied. So, the inside the statements of while loop will not get
executed.

Hence there is no output.

Note: Don't forget that the size of int should be 2 bytes. If you run the
above program in GCC it may run infinite loop, because in Linux platform
the size of the integer is 4 bytes.

28 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What will be the output of the program?

#include <stdio.h>

int main()
{
float a = 0.7;
if(0.7 > a)
printf("Hi\n");
else
printf("Hello\n");
return 0;
}

A.

Hi

B.

Hello

C.

Hi Hello

D.

None of above

Answer: Option A

Explanation:

if(0.7 > a) here a is a float variable and 0.7 is a double constant. The
double constant 0.7 is greater than the float variable a. Hence the if
condition is satisfied and it prints 'Hi'

Example:

#include <stdio.h>
int main()
{
float a = 0.7;
printf("%.10f %.10f\n", 0.7, a);
return 0;
}

Output:

0.7000000000 0.6999999881

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 29


10. What will be the output of the program?

#include <stdio.h>

int main()
{
int a = 0, b = 1, c = 3;
*((a) ? &b : &a) = a ? b : c;
printf("%d, %d, %d\n", a, b, c);
return 0;
}

A.

0, 1, 3

B.

1, 2, 3

C.

3, 1, 3

D.

1, 3, 1

Answer: Option C

30 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int a = 0, b = 1, c = 3;

here variable a, b, and c are declared as integer type and initialized to


0, 1, 3 respectively.

Step 2:

*((a) ? &b : &a) = a ? b : c;

The right side of the expression (a ? b : c) becomes (0 ? 1 : 3). Hence


it return the value '3'.

The left side of the expression *((a) ? &b : &a) becomes *((0) ? &b :
&a). Hence this contains the address of the variable a *(&a).

Step 3:

*((a) ? &b : &a) = a ? b : c;

Finally this statement becomes *(&a) = 3. Hence the variable a has the
value '3'.

Step 4:

printf("%d, %d, %d\n", a, b, c);

It prints "3, 1, 3".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 31


11. What will be the output of the program?

#include <stdio.h>

int main()
{
int k, num = 30;
k = (num < 10) ? 100 : 200;
printf("%d\n", num);
return 0;
}

A.

200

B.

30

C.

100

D.

500

Answer: Option B

32 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


12. What will be the output of the program?

#include <stdio.h>

int main()
{
int a = 300, b, c;
if(a >= 400)
b = 300;
c = 200;
printf("%d, %d, %d\n", a, b, c);
return 0;
}

A.

300, 300, 200

B.

Garbage, 300, 200

C.

300, Garbage, 200

D.

300, 300, Garbage

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 33


Explanation:

Step 1:

int a = 300, b, c;

here variable a is initialized to '300', variable b and c are declared,


but not initialized.

Step 2: if(a >= 400) means if(300 >= 400). Hence this condition will be
failed.

Step 3:

c = 200;

here variable c is initialized to '200'.

Step 4:

printf("%d, %d, %d\n", a, b, c);

It prints "300, garbage value, 200", because variable b is not


initialized.

34 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


13. What will be the output of the program?

#include <stdio.h>

int main()
{
int x = 1, y = 1;
for(; y; printf("%d %d\n", x, y))
{
y = x++ <= 5;
}
printf("\n");
return 0;
}

A.

2 1
3 1
4 1
5 1
6 1
7 0

B.

2 1
3 1
4 1
5 1
6 1

C.

2 1
3 1
4 1
5 1

D.

2 2
3 3
4 4
5 5

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 35


14. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 5;
while(i-- >= 0)
printf("%d, ", i);
i = 5;
printf("\n");
while(i-- >= 0)
printf("%i, ", i);
while(i-- >= 0)
printf("%d, ", i);
return 0;
}

A.

4, 3, 2, 1, 0, -1,
4, 3, 2, 1, 0, -1,

B.

5, 4, 3, 2, 1, 0,
5, 4, 3, 2, 1, 0,

C.

Error

D.

5, 4, 3, 2, 1, 0,
5, 4, 3, 2, 1, 0,
5, 4, 3, 2, 1, 0,

Answer: Option A

36 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1: Initially the value of variable i is '5'.

Loop 1: while(i-- >= 0) here i = 5, this statement becomes while(5-- >=


0)Hence the while condition is satisfied and it prints '4'. (variable 'i'
is decremented by'1'(one) in previous while condition)

Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >=


0)Hence the while condition is satisfied and it prints '3'. (variable 'i'
is decremented by'1'(one) in previous while condition)
Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >=
0)Hence the while condition is satisfied and it prints '2'. (variable 'i'
is decremented by'1'(one) in previous while condition)

Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >=


0)Hence the while condition is satisfied and it prints '1'. (variable 'i'
is decremented by'1'(one) in previous while condition)

Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >=


0)Hence the while condition is satisfied and it prints '0'. (variable 'i'
is decremented by'1'(one) in previous while condition)

Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >=


0)Hence the while condition is satisfied and it prints '-1'. (variable
'i' is decremented by'1'(one) in previous while condition)

Loop 7: while(i-- >= 0) here i = -1, this statement becomes while(-1-- >=
0)Hence the while condition is not satisfied and loop exits.
The output of first while loop is 4,3,2,1,0,-1

Step 2: Then the value of variable i is initialized to '5' Then it prints


a new line character(\n).

See the above Loop 1 to Loop 7.

The output of second while loop is 4, 3, 2, 1, 0, -1

Step 3: The third while loop, while(i-- >= 0) here i = -1(because the
variable 'i' is decremented to '-1' by previous while loop and it never
initialized.). This statement becomes while(-1-- >= 0) Hence the while
condition is not satisfied and loop exits.

Hence the output of the program is:

4, 3, 2, 1, 0, -1,
4, 3, 2, 1, 0, -1,

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 37


15. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 3;
switch(i)
{
case 1:
printf("Hello\n");
case 2:
printf("Hi\n");
case 3:
continue;
default:
printf("Bye\n");
}
return 0;
}

A.

Error: Misplaced continue

B.

Bye

C.

No output

D.

Hello Hi

Answer: Option A

Explanation:

The keyword continue cannot be used in switch case. It must be used in


for or while or do while loop. If there is any looping statement in
switch case then we can use continue.

38 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


16. What will be the output of the program?

#include <stdio.h>

int main()
{
int x = 10, y = 20;
if(!(!x) && x)
printf("x = %d\n", x);
else
printf("y = %d\n", y);
return 0;
}

A.

y =20

B.

x = 0

C.

x = 10

D.

x = 1

Answer: Option C

Explanation:

The logical not operator takes expression and evaluates to true if the
expression is false and evaluates to false if the expression is true. In
other words it reverses the value of the expression.

Step 1: if(!(!x) && x)

Step 2: if(!(!10) && 10)

Step 3: if(!(0) && 10)

Step 3: if(1 && 10)

Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x =


10.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 39


17. What will be the output of the program?

#include <stdio.h>
int main()
{
int i = 4;
switch(i)
{
default:
printf("This is default\n");
case 1:
printf("This is case 1\n");
break;
case 2:
printf("This is case 2\n");
break;
case 3:
printf("This is case 3\n");
}
return 0;
}

A.

This is default
This is case 1

B.

This is case 3
This is default

C.

This is case 1
This is case 3

D.

This is default

Answer: Option A

40 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

In the very beginning of switch-case statement default statement is


encountered. So, it prints "This is default".

In default statement there is no break; statement is included. So it


prints the case 1 statements. "This is case 1".

Then the break; statement is encountered. Hence the program exits from
the switch-case block.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 41


18. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 1;
switch(i)
{
printf("Hello\n");
case 1:
printf("Hi\n");
break;
case 2:
printf("\nBye\n");
break;
}
return 0;
}

A.

Hello
Hi

B.

Hello
Bye

C.

Hi

D.

Bye

Answer: Option C

Explanation:

switch(i) has the variable i it has the value '1'(one).

Then case 1: statements got executed. so, it prints "Hi". The break;
statement make the program to be exited from switch-case statement.

switch-case do not execute any statements outside these blocks case and
default.

Hence the output is "Hi".

42 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


19. What will be the output of the program?

#include <stdio.h>
int main()
{
char j = 1;
while(j < 5)
{
printf("%d, ", j);
j = j + 1;
}
printf("\n");
return 0;
}

A.

1, 2, 3, ... 127

B.

1, 2, 3, ... 255

C.

1, 2, 3, ... 127, 128, 0, 1, 2, 3, ... infinite times

D.

1, 2, 3, 4,

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 43


20. What will be the output of the program?

#include <stdio.h>
int main()
{
int x, y, z;
x = y = z = 1;
z = ++x || ++y && ++z;
printf("x = %d, y = %d, z = %d\n", x, y, z);
return 0;
}

A.

x = 2, y = 1, z = 1

B.

x = 2, y = 2, z = 1

C.

x = 2, y = 2, z = 2

D.

x = 1, y = 2, z = 1

Answer: Option A

44 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

x = y = z = 1;

here the variables x ,y, z are initialized to value '1'.

Step 2:

z = ++x || ++y && ++z;

becomes z = ((++x) || (++y && ++z)). Here ++x becomes 2. So there is no


need to check the other side because || (Logical OR) condition is
satisfied.(z = (2 || ++y && ++z)). There is no need to process ++y &&
++z. Hence it returns '1'. So the value of variable z is '1'

Step 3:

printf("x = %d, y = %d, z = %d\n", x, y, z);

It prints "x = 2, y = 1, z = 1". here x is incremented in previous step.


y and z are not incremented.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 45


46 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Expressions

1. What will be the output of the program?

#include <stdio.h>
int main()
{
int i =-3, j = 2, k = 0, m;
m = ++i && ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}

A.

-2, 3, 1, 1

B.

2, 3, 1, 2

C.

1, 2, 3, 1

D.

3, 3, 1, 2

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 47


Explanation:

Step 1:

int i = -3, j = 2, k = 0, m;

here variable i, j, k, m are declared as an integer type and variable i,


j, k are initialized to -3, 2, 0 respectively.

Step 2:

m = ++i && ++j && ++k;

becomes m = -2 && 3 && 1; becomes m = TRUE && TRUE; Hence this statement
becomes TRUE. So it returns '1'(one). Hence m = 1.

Step 3:

printf("%d, %d, %d, %d\n", i, j, k, m);

In the previous step the value of i, j, k are increemented by '1'(one).

Hence the output is "-2, 3, 1, 1".

48 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


2. Assunming, integer is 2 bytes, what will be the output of the
program?

#include <stdio.h>

int main()
{
printf("%x\n", -2<<2);
return 0;
}

A.

ffff

B.

C.

fff8

D.

Error

Answer: Option C

Explanation:

The integer value 2 is represented as 00000000 00000010 in binary system.

Negative numbers are represented in 2's complement method.

1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s


to 1 and 1s to 0).

2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's


complement to obtain the 2's complement value).

Therefore, in binary we represent -2 as: 11111111 11111110.

After left shifting it by 2 bits we obtain: 11111111 11111000, and it is


equal to "fff8" in hexadecimal system.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 49


3. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = -3, j = 2, k = 0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}

A.

2, 2, 0, 1

B.

1, 2, 1, 0

C.

-2, 2, 0, 0

D.

-2, 2, 0, 1

Answer: Option D

50 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1: int i = -3, j = 2, k = 0, m; here variable i, j, k, m are


declared as an integer type and variable i, j, k are initialized to -3,
2, 0 respectively.

Step 2:

m = ++i || ++j && ++k;

here (++j && ++k;) this code will not get executed because ++i has non-
zero value, becomes m = -2 || ++j && ++k; becomes m = TRUE || ++j && ++k;
Hence this statement becomes TRUE. So it returns '1'(one). Hence m = 1.

Step 3:

printf("%d, %d, %d, %d\n", i, j, k, m);

In the previous step the value of variable 'i' only increemented by


'1'(one). The variable j, k are not increemented.

Hence the output is "-2, 2, 0, 1".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 51


4. What will be the output of the program?

#include <stdio.h>
int main()
{
int x = 12, y = 7, z;
z = x != 4 || y == 2;
printf("z = %d\n", z);
return 0;
}

A.

z = 0

B.

z = 1

C.

z = 4

D.

z = 2

Answer: Option B

52 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int x = 12, y = 7, z;

here variable x, y and z are declared as an integer and variable x and y


are initialized to 12, 7 respectively.

Step 2:

z = x != 4 || y == 2;

becomes z = 12 != 4 || 7 == 2; then z = (condition true) || (condition


false); Hence it returns 1. So the value of z=1.

Step 3:

printf("z = %d\n", z);

Hence the output of the program is "z = 1".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 53


5. What will be the output of the program?

#include <stdio.h>

int main()
{
static int a[20];
int i = 0;
a[i] = i ;
printf("%d, %d, %d\n", a[0], a[1], i);
return 0;
}

A.

1, 0, 1

B.

1, 1, 1

C.

0, 0, 0

D.

0, 1, 0

Answer: Option C

54 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

static int a[20];

here variable a is declared as an integer type and static. If a variable


is declared as static and it will be automatically initialized to value
'0'(zero).

Step 2:

int i = 0;

here vaiable i is declared as an integer type and initialized to '0'


(zero).

Step 3:

a[i] = i;

becomes a[0] = 0;

Step 4:

printf("%d, %d, %d\n", a[0], a[1], i);

Here a[0] = 0, a[1] = 0 (because all staic variables are initialized to


'0') and i = 0.

Step 4: Hence the output is "0, 0, 0".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 55


6. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 4, j = -1, k = 0, w, x, y, z;
w = i || j || k;
x = i && j && k;
y = i || j &&k;
z = i && j || k;
printf("%d, %d, %d, %d\n", w, x, y, z);
return 0;
}

A.

1, 1, 1, 1

B.

1, 1, 0, 1

C.

1, 0, 0, 1

D.

1, 0, 1, 1

Answer: Option D

56 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int i = 4, j = -1, k = 0, w, x, y, z;

here variable i, j, k, w, x, y, z are declared as an integer type and the


variable i, j, k are initialized to 4, -1, 0 respectively.

Step 2:

w = i || j || k;

becomes w = 4 || -1 || 0;. Hence it returns TRUE. So, w = 1.

Step 3:

x = i && j && k;

becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x = 0.

Step 4:

y = i || j && k;

becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y = 1.

Step 5:

z = i && j || k;

becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z = 1.

Step 6:

printf("%d, %d, %d, %d\n", w, x, y, z);

Hence the output is "1, 0, 1, 1".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 57


7. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = -3, j = 2, k = 0, m;
m = ++i && ++j || ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}

A.

1, 2, 0, 1

B.

-3, 2, 0, 1

C.

-2, 3, 0, 1

D.

2, 3, 1, 1

Answer: Option C

58 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int i = -3, j = 2, k = 0, m;

here variable i, j, k, m are declared as an integer type and variable i,


j, k are initialized to -3, 2, 0 respectively.

Step 2:

m = ++i && ++j || ++k;

becomes m = (-2 && 3) || ++k; becomes m = TRUE || ++k;.

(++k) is not executed because (-2 && 3) alone return TRUE.

Hence this statement becomes TRUE. So it returns '1'(one). Hence m = 1.

Step 3:

printf("%d, %d, %d, %d\n", i, j, k, m);

In the previous step the value of i,j are increemented by '1' (one).

Hence the output is "-2, 3, 0, 1".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 59


8. What will be the output of the program?

#include <stdio.h>

int main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d, %d, %d\n", x, y, z);
return 0;
}

A.

4, 3, 3

B.

4, 3, 2

C.

3, 3, 2

D.

2, 3, 3

Answer: Option D

60 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int x = 4, y, z;

here variable x, y, z are declared as an integer type and variable x is


initialized to 4.

Step 2:

y = --x;

becomes y = 3; because (--x) is pre-decrement operator.

Step 3:

z = x--;

becomes z = 3;. In the next step variable x becomes 2, because (x--) is


post-decrement operator.

Step 4:

printf("%d, %d, %d\n", x, y, z);

Hence it prints "2, 3, 3".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 61


9. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 3;
i = i++;
printf("%d\n", i);
return 0;
}

A.

B.

C.

D.

Answer: Option B

62 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


10. What will be the output of the program?

#include <stdio.h>

int main()
{
int a = 100, b = 200, c;
c = (a == 100 || b > 200);
printf("c = %d\n", c);
return 0;
}

A.

c = 100

B.

c = 200

C.

c = 1

D.

c = 300

Answer: Option C

Explanation:

Step 1: int a = 100, b = 200, c;

Step 2: c = (a == 100 || b > 200);

becomes c = (100 == 100 || 200 > 200); becomes c = (TRUE || FALSE);


becomes c = (TRUE); (ie. c = 1)

Step 3:

printf("c = %d\n", c);

It prints the value of variable c = 1.

Hence the output of the program is '1' (one).

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 63


11. What will be the output of the program?

#include <stdio.h>

int main()
{
int x = 55;
printf("%d, %d, %d\n", x <= 55, x = 40, x >= 10);
return 0;
}

A.

1, 40, 1

B.

1, 55, 1

C.

1, 55, 0

D.

1, 1, 1

Answer: Option A

Explanation:

Step 1:

int x = 55;

here variable x is declared as an integer type and initialized to '55'.

Step 2:

printf("%d, %d, %d\n", x <= 55, x = 40, x >= 10);

In printf the execution of expressions is from Right to Left. Here x >=


10 returns TRUE hence it prints '1'.

x = 40 here x is assigned to 40 Hence it prints '40'.

x <= 55 returns TRUE. hence it prints '1'.

Step 3: Hence the output is "1, 40, 1".

64 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


12. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 2;
printf("%d, %d\n", ++i, ++i);
return 0;
}

A.

3, 4

B.

4, 3

C.

4, 4

D.

Output may vary from compiler to compiler

Answer: Option D

Explanation:

The order of evaluation of arguments passed to a function call is


unspecified.

Anyhow, we consider ++i, ++i are Right-to-Left associativity. The output


of the program is 4, 3.

In TurboC, the output will be 4, 3.

In GCC, the output will be 4, 4.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 65


13. What will be the output of the program?

#include <stdio.h>

int main()
{
int k, num = 30;
k = (num>5 ? (num <=10 ? 100 : 200): 500);
printf("%d\n", num);
return 0;
}

A.

200

B.

30

C.

100

D.

500

Answer: Option B

66 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int k, num = 30;

here variable k and num are declared as an integer type and variable num
is initialized to '30'.

Step 2:

k = (num > 5 ? (num <= 10 ? 100 : 200): 500);

This statement does not affect the output of the program. Because we are
going to print the variable num in the next statement. So, we skip this
statement.

Step 3:

printf("%d\n", num);

It prints the value of variable num '30'

Step 3: Hence the output of the program is '30'

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 67


14. What will be the output of the program?

#include <stdio.h>

int main()
{
char ch;
ch = 'A';
printf("The letter is ");
printf("%c\n", ch >= 'A' && ch <= 'Z' ? ch + 'a' - 'A':ch);
printf("Now the letter is ");
printf("%c\n", ch >= 'A' && ch <= 'Z' ? ch : ch + 'a' - 'A');
return 0;
}

A.

The letter is a
Now the letter is A

B.

The letter is A
Now the letter is a

C.

Error

D.

None of above

Answer: Option A

68 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

char ch;
ch = 'A';

here variable ch is declared as an character type an initialized to 'A'.

Step 2:

printf("The letter is ");

It prints "The letter is".

Step 3:

printf("%c\n", ch >= 'A' && ch <= 'Z' ? ch + 'a' - 'A':ch);

The ASCII value of 'A' is 65 and 'a' is 97.

Here

=> ('A' >= 'A' && 'A' <= 'Z') ? (A + 'a' - 'A') : ('A')
=> (TRUE && TRUE) ? (65 + 97 - 65) : ('A')
=> (TRUE) ? (97): ('A')

In printf the format specifier is '%c'. Hence prints 97 as 'a'.

Step 4:

printf("Now the letter is ");

It prints "Now the letter is".

Step 5:

printf("%c\n", ch >= 'A' && ch <= 'Z' ? ch : ch + 'a' - 'A');

Here => ('A' >= 'A' && 'A' <= 'Z') ? ('A') : (A + 'a' - 'A')
=> (TRUE && TRUE) ? ('A') :(65 + 97 - 65)
=> (TRUE) ? ('A') : (97)

It prints 'A'

Hence the output is:

The letter is a
Now the letter is A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 69


15. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 2;
int j = i + (1, 2, 3, 4, 5);
printf("%d\n", j);
return 0;
}

A.
4

B.

C.

D.

Answer: Option B

Explanation:

Because, comma operator used in the expression i (1, 2, 3, 4, 5). The


comma operator has left-right associativity. The left operand is always
evaluated first, and the result of evaluation is discarded before the
right operand is evaluated. In this expression 5 is the right most
operand, hence after evaluating expression (1, 2, 3, 4, 5) the result is
5, which on adding to i results into 7.

70 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Floating Point Issues

1. What will be the output of the program?

#include <stdio.h>
int main()
{
float a = 0.7;
if(a < 0.7)
printf("C\n");
else
printf("C++\n");
return 0;
}

A.
C

B.
C++

C.
Compiler error

D.
None of above

Answer: Option A

Explanation:

if(a < 0.7) here a is a float variable and 0.7 is a double constant. The
float variablea is less than double constant 0.7. Hence the if condition
is satisfied and it prints 'C'

Example:

#include <stdio.h>
int main()
{
float a = 0.7;
printf("%.10f %.10f\n",0.7, a);
return 0;
}

Output:

0.7000000000 0.6999999881

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 71


2. What will be the output of the program?

#include <stdio.h>

int main()
{
float *p;
printf("%d\n", sizeof(p));
return 0;
}

A.

2 in 16bit compiler, 4 in 32bit compiler

B.

4 in 16bit compiler, 2 in 32bit compiler

C.

4 in 16bit compiler, 4 in 32bit compiler

D.

2 in 16bit compiler, 2 in 32bit compiler

Answer: Option A

Explanation:

sizeof(x) returns the size of x in bytes.

float *p is a pointer to a float.

In 16 bit compiler, the pointer size is always 2 bytes.

In 32 bit compiler, the pointer size is always 4 bytes.

72 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>

int main()
{
float fval = 7.29;
printf("%d\n", (int)fval);
return 0;
}

A.

B.

0.0

C.

7.0

D.

Answer: Option D

Explanation:

printf("%d\n", (int) fval);

It prints '7'. because, we typecast the (int)fval in to integer. It


converts the float value to the nearest integer value.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 73


4. What will be the output of the program?

#include <stdio.h>
#include<math.h>

int main()
{
printf("%f\n", sqrt(36.0));
return 0;
}

A.

6.0

B.

C.

6.000000

D.

Error: Prototype sqrt() not found.

Answer: Option C

Explanation:

printf("%f\n", sqrt(36.0));

It prints the square root of 36 in the float format(i.e 6.000000).

Declaration Syntax: double sqrt(double x) calculates and return the


positive square root of the given number.

74 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program?

#include <stdio.h>
#include<math.h>

int main()
{
printf("%d, %d, %d\n", sizeof(3.14f), sizeof(3.14), sizeof(3.14l));
return 0;
}

A.

4, 4, 4

B.

4, 8, 8

C.

4, 8, 10

D.

4, 8, 12

Answer: Option C

Explanation:

sizeof(3.14f) here '3.14f' specifies the float data type. Hence size of
float is 4 bytes.

sizeof(3.14) here '3.14' specifies the double data type. Hence size of
float is 8 bytes.

sizeof(3.14l) here '3.14l' specifies the long double data type. Hence
size of float is 10 bytes.

Note: If you run the above program in Linux platform (GCC Compiler) it
will give 4, 8, 12 as output. If you run in Windows platform (TurboC
Compiler) it will give 4, 8, 10 as output. Because, C is a machine
dependent language.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 75


6. What will be the output of the program?

#include <stdio.h>

int main()
{
float f = 43.20;
printf("%e, ", f);
printf("%f, ", f);
printf("%g", f);
return 0;
}

A.

4.320000e+01, 43.200001, 43.2

B.

4.3, 43.22, 43.21

C.

4.3e, 43.20f, 43.00

D.

Error

Answer: Option A

Explanation:

printf("%e, ", f); Here '%e' specifies the "Scientific Notation" format.
So, it prints the 43.20 as 4.320000e+01.

printf("%f, ", f); Here '%f' specifies the "Decimal Floating Point"
format. So, it prints the 43.20 as 43.200001.

printf("%g, ", f); Here '%g' "Use the shorter of %e or %f". So, it prints
the 43.20 as 43.2.

76 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What will be the output of the program?

#include <stdio.h>
int main()
{
float a=0.7;
if(a < 0.7f)
printf("C\n");
else
printf("C++\n");
return 0;
}

A.

B.

C++

C.

Compiler error

D.

None of above

Answer: Option B

Explanation:

if(a < 0.7f) here a is a float variable and 0.7f is a float constant. The
float variable a is not less than 0.7f float constant. But both are
equal. Hence the if condition is failed and it goes to else it prints
'C++'

Example:

#include <stdio.h>
int main()
{
float a=0.7;
printf("%.10f %.10f\n",0.7f, a);
return 0;
}

Output:

0.6999999881 0.6999999881

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 77


8. What will be the output of the program?

#include <stdio.h>
#include<math.h>

int main()
{
float n = 1.54;
printf("%f, %f\n", ceil(n), floor(n));
return 0;
}

A.

2.000000, 1.000000

B.

1.500000, 1.500000

C.

1.550000, 2.000000

D.

1.000000, 2.000000

Answer: Option A

Explanation:

ceil(x) round up the given value. It finds the smallest integer not < x.
floor(x) round down the given value. It finds the smallest integer not >
x.

printf("%f, %f\n", ceil(n), floor(n));

In this line ceil(1.54) round up the 1.54 to 2 and floor(1.54) round down
the 1.54 to 1.

In the

printf("%f, %f\n", ceil(n), floor(n));

statement, the format specifier "%f %f" tells output to be float value.

Hence it prints 2.000000 and 1.000000.

78 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What will be the output of the program?

#include <stdio.h>
int main()
{
float d = 2.25;
printf("%e,", d);
printf("%f,", d);
printf("%g,", d);
printf("%lf", d);
return 0;
}

A.
2.2, 2.50, 2.50, 2.5

B.
2.2e, 2.25f, 2.00, 2.25

C.
2.250000e+000, 2.250000, 2.25, 2.250000

D.
Error

Answer & Explanation

Answer: Option C

Explanation:

printf("%e,", d);

Here '%e' specifies the "Scientific Notation" format. So, it prints the
2.25 as 2.250000e+000.

printf("%f,", d);

Here '%f' specifies the "Decimal Floating Point" format. So, it prints
the 2.25 as 2.250000.

printf("%g,", d);

Here '%g' "Use the shorter of %e or %f". So, it prints the 2.25 as 2.25.

printf("%lf,", d);

Here '%lf' specifies the "Long Double" format. So, it prints the 2.25 as
2.250000.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 79


80 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Functions

1. What will be the output of the program in 16 bit platform (Turbo C


under DOS)?

#include <stdio.h>

int main()
{
int fun();
int i;
i = fun();
printf("%d\n", i);
return 0;
}

int fun()
{
_AX = 1990;
}

A.

Garbage value

B.

0 (Zero)

C.

1990

D.

No output

Answer: Option C

Explanation:

Turbo C (Windows): The return value of the function is taken from the
Accumulator_AX=1990.

But it may not work as expected in GCC compiler (Linux).

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 81


2. What will be the output of the program?

#include <stdio.h>

void fun(int*, int*);

int main()
{
int i = 5, j = 2;
fun(&i, &j);
printf("%d, %d", i, j);
return 0;
}

void fun(int *i, int *j)


{
*i = *i * *i;
*j = *j * *j;
}

A.

5, 2

B.

10, 4

C.

2, 5

D.

25, 4

Answer: Option D

82 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int i = 5, j = 2;

Here variable i and j are declared as an integer type and initialized to


5 and 2 respectively.

Step 2:

fun(&i, &j);

Here the function fun() is called with two parameters &i and &j (The &
denotes call by reference. So the address of the variable i and j are
passed).

Step 3:

void fun(int *i, int *j);

This function is called by reference, so we have to use * before the


parameters.

Step 4: *i = *i**i; Here *i denotes the value of the variable i. We are


multiplying5*5 and storing the result 25 in same variable i.

Step 5:

*j = *j**j;

Here *j denotes the value of the variable j. We are multiplying 2 * 2 and


storing the result 4 in same variable j.

Step 6: Then the function void fun(int *i, int *j) return back the
control back to main() function.

Step 7:

printf("%d, %d", i, j);

It prints the value of variable i and j.

Hence the output is 25, 4.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 83


3. What will be the output of the program?

#include <stdio.h>
int i;
int fun();

int main()
{
while(i)
{
fun();
main();
}
printf("Hello\n");
return 0;
}

int fun()
{
printf("Hi");
}

A.

Hello

B.

Hi Hello

C.

No output

D.

Infinite loop

Answer: Option A

84 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int i;

The variable i is declared as an integer type.

Step 2:

int fun();

This prototype tells the compiler that the function fun() does not accept
any arguments and it returns an integer value.

Step 3:

while(i)

The value of i is not initialized so this while condition is failed. So,


it does not execute the while block.

Step 4:

printf("Hello\n");

It prints "Hello".

Hence the output of the program is "Hello".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 85


4. What will be the output of the program?

#include <stdio.h>

int reverse(int);

int main()
{
int no = 5;
reverse(no);
return 0;
}

int reverse(int no)


{
if(no == 0)
return 0;
else
printf("%d,", no);
reverse (no--);
}

A.

Print 5, 4, 3, 2, 1

B.

Print 1, 2, 3, 4, 5

C.

Print 5, 4, 3, 2, 1, 0

D.

Infinite loop

Answer: Option D

86 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int no = 5;

The variable no is declared as integer type and initialized to 5.

Step 2:

reverse(no);

becomes reverse(5); It calls the function reverse() with '5' as


parameter.

The function reverse accept an integer number 5 and it returns '0'(zero)


if(5 == 0) if the given number is '0'(zero) or else printf("%d,", no); it
prints that number 5 and calls the function reverse(5);.

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(5) infinitely.

Note: If we use pre-decrement operator like reverse(--n), then the output


will be 5, 4, 3, 2, 1. Because before calling the function, it decrements
the value of 'n'.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 87


5. What will be the output of the program?

#include <stdio.h>

void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
int a=3;
fun(a);
return 0;
}

void fun(int n)
{
if(n > 0)
{
fun(--n);
printf("%d,", n);
fun(--n);
}
}

A.

0, 2, 1, 0,

B.

1, 1, 2, 0,

C.

0, 1, 0, 2,

D.

0, 1, 2, 0,

Answer: Option D

88 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


6. What will be the output of the program?

#include <stdio.h>

int sumdig(int);

int main()
{
int a, b;
a = sumdig(123);
b = sumdig(123);
printf("%d, %d\n", a, b);
return 0;
}

int sumdig(int n)
{
int s, d;
if(n!=0)
{
d = n%10;
n = n/10;
s = d+sumdig(n);
}
else
return 0;
return s;
}

A.

4, 4

B.

3, 3

C.

6, 6

D.

12, 12

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 89


7. What will be the output of the program?

#include <stdio.h>

int main()
{
void fun(char*);
char a[100];
a[0] = 'A';
a[1] = 'B';
a[2] = 'C';
a[3] = 'D';
fun(&a[0]);
return 0;
}

void fun(char *a)


{
a++;
printf("%c", *a);
a++;
printf("%c", *a);
}

A.

AB

B.

BC

C.

CD

D.

No output

Answer: Option B

90 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


8. What will be the output of the program?

#include <stdio.h>

int main()
{
int fun(int);
int i = fun(10);
printf("%d\n", --i);
return 0;
}

int fun(int i)
{
return (i++);
}

A.

B.

10

C.

11

D.

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 91


Explanation:

Step 1:

int fun(int);

Here we declare the prototype of the function fun().

Step 2:

int i = fun(10);

The variable i is declared as an integer type and the result of the


fun(10) will be stored in the variable i.

Step 3:

int fun(int i)
{
return (i++);
}

Inside the fun() we are returning a value return(i++). It returns 10.


because i++ is the post-increement operator.

Step 4: Then the control back to the main function and the value 10 is
assigned to variable i.

Step 5:

printf("%d\n", --i);

Here --i denoted pre-increement.

Hence it prints the value 9.

92 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What will be the output of the program?

#include <stdio.h>

int check (int, int);

int main()
{
int c;
c = check(10, 20);
printf("c = %d\n", c);
return 0;
}

int check(int i, int j)


{
int *p, *q;
p = &i;
q = &j;
i >= 45 ? return(*p) : return(*q);
}

A.

Print 10

B.

Print 20

C.

Print 1

D.

Compile error

Answer: Option D

Explanation:

There is an error in this line

i >= 45 ? return(*p) : return(*q);.

We cannot use return keyword in the ternary operators.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 93


10. What will be the output of the program?

#include <stdio.h>

int fun(int, int);


typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
printf("%d\n", proc(fun, 6, 6));
return 0;
}

int fun(int a, int b)


{
return (a == b);
}

int proc(pf p, int a, int b)


{
return ((*p)(a, b));
}

A.

B.

C.

D.

-1

Answer: Option B

94 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


11. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 1;
if(!i)
printf("IndiaREC,");
else
{
i = 0;
printf("C-Program");
main();
}
return 0;
}

A.

prints "IndiaREC, C-Program" infinitely

B.

prints "C-Program" infinetly

C.

prints "C-Program, IndiaREC" infinitely

D.

Error: main() should not inside else statement

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 95


Explanation:

Step 1:
int i = 1; The variable i is declared as an integer type and initialized
to 1(one).

Step 2:

if(!i)

Here the !(NOT) operator reverts the i value 1 to 0. Hence the if(0)
condition fails. So it goes to else part.

Step 3: else { i=0; In the else part variable i is assigned to value


0(zero).

Step 4:

printf("C-Program");

It prints the "C-program".

Step 5:

main();

Here we are calling the main() function.

After calling the function, the program repeats from step 1 to step 5
infinitely.

Hence it prints "C-Program" infinitely.

96 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


12. What will be the output of the program?

#include <stdio.h>

int addmult(int ii, int jj)


{
int kk, ll;
kk = ii + jj;
ll = ii * jj;
return (kk, ll);
}

int main()
{
int i = 3, j = 4, k, l;
k = addmult(i, j);
l = addmult(i, j);
printf("%d %d\n", k, l);
return 0;
}

A.

12 12

B.

No error, No output

C.

Error: Compile error

D.

None of above

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 97


13. What will be the output of the program?

#include <stdio.h>
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.

3, 4, 3, 4

Answer: Option B

98 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int i;

The variable i is declared as an global and integer type.

Step 2:

int fun1(int);

This prototype tells the compiler that the fun1() accepts the one integer
parameter and returns the integer value.

Step 3:

int fun2(int);

This prototype tells the compiler that the fun2() accepts the one integer
parameter and returns the integer value.

Step 4:

extern int j;

Inside the main function, the extern variable j is declared and defined
in another source file.

Step 5:

int i = 3;

The local variable i is defines as an integer type and initialized to 3.

Step 6:

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.

Step 7:

printf("%d,", i);

It prints the value of local variable i. So, it prints '3'.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 99


Step 8:

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.

Step 9:

printf("%d,", i);

It prints the value of local variable i. So, it prints '3'.

Hence the output is "4 3 4 3".

100 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


14. What will be the output of the program?

#include <stdio.h>

int func1(int);

int main()
{
int k = 35;
k = func1(k = func1(k = func1(k)));
printf("k = %d\n", k);
return 0;
}
int func1(int k)
{
k++;
return k;
}

A.

k = 35

B.

k = 36

C.

k = 37

D.

k = 38

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 101


Explanation:

Step 1:

int k = 35;

The variable k is declared as an integer type and initialized to 35.

Step 2:

k = func1(k = func1(k = func1(k)));

The func1(k) increment the value of k by 1 and return it. Here the
func1(k) is called 3 times. Hence it increments value of k = 35 to 38.
The result is stored in the variable k = 38.

Step 3:

printf("k = %d\n", k);

It prints the value of variable k "38".

102 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


15. What will be the output of the program?

#include <stdio.h>

int addmult(int ii, int jj)


{
int kk, ll;
kk = ii + jj;
ll = ii * jj;
return (kk, ll);
}

int main()
{
int i = 3, j = 4, k, l;
k = addmult(i, j);
l = addmult(i, j);
printf("%d, %d\n", k, l);
return 0;
}

A.

12, 12

B.

7, 7

C.

7, 12

D.

12, 7

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 103


Explanation:

Step 1:

int i = 3, j = 4, k, l;

The variables i, j, k, l are declared as an integer type and variable i,


j are initialized to 3, 4 respectively.

The function

addmult(i, j);

accept 2 integer parameters.

Step 2:

k = addmult(i, j);

becomes k = addmult(3, 4)

In the function addmult(). The variable kk, ll are declared as an integer


type int kk, ll;

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll);

It returns the value of variable ll only.

The value 12 is stored in variable 'k'.

Step 3: l = addmult(i, j); becomes l = addmult(3, 4)

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll); It returns the value of variable ll only.

The value 12 is stored in variable 'l'.

Step 4:

printf("%d, %d\n", k, l);

It prints the value of k and l

Hence the output is "12, 12".

104 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


16. What will be the output of the program?

#include <stdio.h>

int check(int);

int main()
{
int i = 45, c;
c = check(i);
printf("%d\n", c);
return 0;
}

int check(int ch)


{
if(ch >= 45)
return 100;
else
return 10;
}

A.

100

B.

10

C.

D.

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 105


Explanation:

Step 1:

int check(int);

This prototype tells the compiler that the functioncheck() accepts one
integer parameter and returns an integer value.

Step 2:

int i = 45, c;

The variable i and c are declared as an integer type and i is initialized


to 45.

The function check(i) return 100 if the given value of variable i is >=
(greater than or equal to) 45, else it will return 10.

Step 3:

c = check(i);

becomes c = check(45); The function check() return 100 and it get stored
in the variable c.(c = 100)

Step 4:

printf("%d\n", c);

It prints the value of variable c.

Hence the output of the program is '100'.

106 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


17. If int is 2 bytes wide. What will be the output of the program?

#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.

cd

C.

Ef

D.

gh

Answer: Option B

Explanation:

Since C is a machine dependent language sizeof(int) may return different


values.

The output for the above program will be cd in Windows (Turbo C) and gh
in Linux (GCC).

To understand it better, compile and execute the above program in Windows


(with Turbo C compiler) and in Linux (GCC compiler).

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 107


18. What will be the output of the program?

#include <stdio.h>
int fun(int(*)());

int main()
{
fun(main);
printf("Hi\n");
return 0;
}

int fun(int (*p)())


{
printf("Hello ");
return 0;
}

A.

Infinite loop

B.

Hi

C.

Hello Hi

D.

Error

Answer: Option C

108 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


19. What will be the output of the program?

#include <stdio.h>

int fun(int i)
{
i++;
return i;
}

int main()
{
int fun(int);
int i = 3;
fun(i = fun(fun(i)));
printf("%d\n", i);
return 0;
}

A.
5

B.
4

C.
Error

D.
Garbage value

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 109


Explanation:

Step 1:

int fun(int);

This is prototype of function fun(). It tells the compiler that the


function fun() accept one integer parameter and returns an integer value.

Step 2:

int i = 3;

The variable i is declared as an integer type and initialized to value 3.

Step 3:

fun(i=fun(fun(i)));

The function fun(i) increements the value of i by 1(one) and return it.

Lets go step by step,

=> fun(i) becomes fun(3) is called and it returns 4.

=> i = fun(fun(i)) becomes i = fun(4) is called and it returns 5 and


stored in variable i.(i=5)

=> fun(i=fun(fun(i))); becomes fun(5); is called and it return 6 and


nowhere the return value is stored.

Step 4:

printf("%d\n", i);

It prints the value of variable i (5).

Hence the output is '5'.

110 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


20. What will be the output of the program?

#include <stdio.h>
int fun(int);

int main()
{
float k = 3;
fun(k = fun(fun(k)));
printf("%f\n", k);
return 0;
}

int fun(int i)
{
i++;
return i;
}

A.

5.000000

B.

3.000000

C.

Garbage value

D.

4.000000

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 111


21. What will be the output of the program?

#include <stdio.h>
#include<stdlib.h>

int main()
{
int i = 0;
i++;
if(i <= 5)
{
printf("IndiaREC");
exit(1);
main();
}
return 0;
}

A.

Prints "IndiaREC" 5 times

B.

Function main() doesn't calls itself

C.

Infinite loop

D.

Prints "IndiaREC"

Answer: Option D

112 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int i = 0;

The variable i is declared as in integer type and initialized to


'0'(zero).

Step 2:

i++;

Here variable i is increemented by 1. Hence i becomes '1'(one).

Step 3:

if(i <= 5)

becomes if(1 <= 5). Hence the if condition is satisfied and it enter into
if block statements.

Step 4:

printf("IndiaREC");

It prints "IndiaREC".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 113


114 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
C Preprocessor

1. What will be the output of the program?

#include <stdio.h>
#define MAN(x, y) ((x) > (y)) ? (x) : (y);
int main()
{
int i = 10, j = 5, k = 0;
k = MAN(++i, j++);
printf("%d, %d, %d\n", i, j, k);
return 0;
}

A.

12, 6, 12

B.

11, 5, 11

C.

11, 5, Garbage

D.

12, 6, Garbage

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 115


Explanation:

The macro

MAN(x, y) ((x)>(y)) ? (x):(y);

returns the biggest number of given two numbers.

Step 1:

int i = 10, j = 5, k = 0;

The variable i, j, k are declared as an integer type and initialized to


value 10, 5, 0 respectively.

Step 2:

k = MAN(++i, j++);

becomes,

=> k = ((++i) > (j++)) ? (++i) : (j++);


=> k = ((11) > (5)) ? (12) : (6);
=> k = 12

Step 3:

printf("%d, %d, %d\n", i, j, k);

It prints the variable i, j, k.

In the above macro step 2 the variable i value is increemented by 2 and


variable jvalue is increemented by 1.

Hence the output of the program is 12, 6, 12

116 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


2. What will be the output of the program?

#include <stdio.h>
#define SQUARE(x) x * x

int main()
{
float s = 10, u = 30, t = 2, a;
a = 2 * (s - u * t) / SQUARE(t);
printf("Result = %f", a);
return 0;
}

A.

Result = -100.000000

B.

Result = -25.000000

C.

Result = 0.000000

D.

Result = 100.000000

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 117


Explanation:

The macro function

SQUARE(x) x * x

calculate the square of the given number 'x'.

Step 1:

float s = 10, u = 30, t = 2, a;

Here the variable s, u, t, a are declared as an floating point type and


the variable s, u, t are initialized to 10, 30, 2.

Step 2:

a = 2 * (s – u * t) / SQUARE(t);

becomes,

=> a = 2 * (10 - 30 * 2) / t * t;

Here SQUARE(t) is replaced by macro t * t.

=> a = 2 * (10 - 30 * 2) / 2 * 2;
=> a = 2 * (10 - 60) / 2 * 2;
=> a = 2 * (-50) / 2 * 2;
=> a = 2 * (-25) * 2;
=> a = (-50) * 2;
=> a = -100;

Step 3:

printf("Result = %f", a);

It prints the value of variable 'a'.

Hence the output of the program is -100.000000.

118 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>
#define SQR(x) (x * x)

int main()
{
int a, b = 3;
a = SQR(b + 2);
printf("%d\n", a);
return 0;
}

A.
25

B.

11

C.

Error

D.

Garbage value

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 119


Explanation:

The macro function

SQR(x) (x * x)

calculate the square of the given number 'x'.

Step 1:

int a, b = 3;

Here the variable a, b are declared as an integer type and the variable b
is initialized to 3.

Step 2:

a = SQR(b + 2);

becomes,

=> a = b + 2 * b + 2;

Here SQR(x) is replaced by macro to x * x.

=> a = 3 + 2 * 3 + 2;
=> a = 3 + 6 + 2;
=> a = 11;

Step 3:

printf("%d\n", a);

It prints the value of variable 'a'.

Hence the output of the program is 11

120 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


4. What will be the output of the program?

#include <stdio.h>
#define JOIN(s1, s2) printf("%s = %s % s =%s\n", #s1, s1, #s2, s2);
int main()
{
char *str1 = "India";
char *str2 = "REC";
JOIN(str1, str2);
return 0;
}

A.

str1 = IndiaREC str2 = REC

B.

str1 = India str2 = REC

C.

str1 = India str2 = IndiaREC

D.

Error: in macro substitution

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 121


5. What will be the output of the program?

#include <stdio.h>
#define CUBE(x) (x * x * x)

int main()
{
int a, b = 3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}

A.

9, 4

B.

27, 4

C.

27, 6

D.

Error

Answer: Option C

122 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The macro function

CUBE(x) (x * x * x)

calculates the cubic value of given number.

Step 1:

int a, b = 3;

The variable a and b are declared as an integer type and varaible b id


initialized to 3.

Step 2:

a = CUBE(b++);

becomes

=> a = b++ * b++ * b++;


=> a = 3 * 3 * 3;

Here we are using post-increement operator, so the 3 is not incremented


in this statement.

=> a = 27;

Here, 27 is store in the variable a. By the way, the value of variable


bis incremented by 3. (ie: b = 6)

Step 3:

printf("%d, %d\n", a, b);

It prints the value of variable a and b.

Hence the output of the program is 27, 6.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 123


6. What will be the output of the program?

#include <stdio.h>
#define PRINT(int) printf("int = %d, ", int);

int main()
{
int x = 2, y = 3, z = 4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}

A.

int = 2, int = 3, int = 4

B.

int = 2, int = 2, int = 2

C.

int = 3, int = 3, int = 3

D.

int = 4, int = 4, int = 4

Answer: Option A

124 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The macro

PRINT(int) print("%d,", int);

prints the given variable value in an integer format.

Step 1:

int x = 2, y = 3, z = 4;

The variable x, y, z are declared as an integer type and initialized to


2, 3, 4 respectively.

Step 2:

PRINT(x);

becomes printf("int = %d,",x). Hence it prints 'int = 2'.

Step 3:

PRINT(y);

becomes printf("int = %d,",y). Hence it prints 'int = 3'.

Step 4:

PRINT(z);

becomes printf("int = %d,",z). Hence it prints 'int = 4'.

Hence the output of the program is int = 2, int = 3, int = 4.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 125


7. What will be the output of the program?

#include <stdio.h>
#define SWAP(a, b) int t; t = a, a = b, b = t;
int main()
{
int a = 10, b = 12;
SWAP(a, b);
printf("a = %d, b = %d\n", a, b);
return 0;
}

A.

a = 10, b = 12

B.

a = 12, b = 10

C.

Error: Declaration not allowed in macro

D.

Error: Undefined symbol 't'

Answer: Option B

126 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The macro

SWAP(a, b) int t; t = a, a = b, b = t;

swaps the value of the given two variables.

Step 1:

int a = 10, b = 12;

The variable a and b are declared as an integer type and initialized to


10, 12 respectively.

Step 2:

SWAP(a, b);

Here the macro is substituted and it swaps the value to variable a and b.

Hence the output of the program is 12, 10.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 127


8. What will be the output of the program?

#include <stdio.h>
#define FUN(i, j) i ## j

int main()
{
int va1 = 10;
int va12 = 20;
printf("%d\n", FUN(va1, 2));
return 0;
}

A.

10

B.

20

C.

1020

D.

12

Answer: Option B

128 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The following program will make you understand about ## (macro


concatenation) operator clearly.

#include <stdio.h>
#define FUN(i, j) i ## j

int main()
{
int First = 10;
int Second = 20;
char FirstSecond[] = "IndiaREC";
printf("%s\n", FUN(First, Second) );
return 0;
}

Output:

IndiaREC

The preprocessor will replace FUN(First, Second) as FirstSecond.

Therefore, the

printf("%s\n", FUN(First, Second));

statement will become as printf("%s\n", FirstSecond);

Hence it prints IndiaREC as output.

Like the same, the line

printf("%d\n", FUN(va1, 2));

given in the above question will become as printf("%d\n", va12);.

Therefore, it prints 20 as output.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 129


9. What will be the output of the program?

#include <stdio.h>
#define FUN(arg) do\
{\
if(arg)\
printf("REC...", "\n");\
} while(--i)
int main()
{
int i = 2;
FUN(i < 3);
return 0;
}

A.

REC...
REC...
REC

B.

REC... REC...

C.

Error: cannot use control instructions in macro

D.

No output

Answer: Option B

130 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The macro

FUN(arg)

prints the statement "IndiaREC..." until the while condition is


satisfied.

Step 1:

int i = 2;

The variable i is declared as an integer type and initialized to 2.

Step 2:

FUN(i < 3);

becomes,
do
{
if(2 < 3)
printf("REC...", "\n");
} while(--2)

After the 2 while loops the value of i becomes '0'(zero). Hence the while
loop breaks.

Hence the output of the program is "REC... REC..."

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 131


10. What will be the output of the program?

#include <stdio.h>
#define MAX(a, b) (a > b ? a : b)

int main()
{
int x;
x = MAX(3 + 2, 2 + 7);
printf("%d\n", x);
return 0;
}

A.

B.

C.

D.

Answer: Option B

132 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The macro

MAX(a, b) (a > b ? a : b)

returns the biggest value of the given two numbers.

Step 1:

int x;

The variable x is declared as an integer type.

Step 2:

x = MAX(3 + 2, 2 + 7);

becomes,

=> x = (3 + 2 > 2 + 7 ? 3 + 2 : 2 + 7)
=> x = (5 > 9 ? 5 : 9)
=> x = 9

Step 3:

printf("%d\n", x);

It prints the value of variable x.

Hence the output of the program is 9.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 133


11. What will be the output of the program?

#include <stdio.h>
#define MIN(x, y) (x < y) ? x : y;

int main()
{
int x = 3, y = 4, z;
z = MIN(x + y / 2, y - 1);
if(z > 0)
printf("%d\n", z);
return 0;
}

A.

B.

C.

D.

No output

Answer: Option A

134 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The macro

MIN(x, y) (x < y) ? x : y;

returns the smallest value from the given two numbers.

Step 1:

int x = 3, y = 4, z;

The variable x, y, z are declared as an integer type and the variable x,


y are initialized to value 3, 4 respectively.

Step 2:

z = MIN(x + y / 2, y - 1);

becomes,

=> z = (x + y / 2 < y - 1) ? x + y / 2 : y - 1;
=> z = (3 + 4 / 2 < 4 - 1) ? 3 + 4 / 2 : 4 - 1;
=> z = (3 + 2 < 4 - 1)? 3 + 2 : 4 - 1;
=> z = (5 < 3) ? 5 : 3;

The macro return the number 3 and it is stored in the variable z.

Step 3:

if(z > 0)

becomes if(3 > 0) here the if condition is satisfied. It executes the if


block statements.

Step 4:

printf("%d\n", z);

It prints the value of variable z.

Hence the output of the program is 3

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 135


12. What will be the output of the program?

#include <stdio.h>
#define str(x) #x
#define Xstr(x) str(x)
#define oper multiply

int main()
{
char *opername = Xstr(oper);
printf("%s\n", opername);
return 0;
}

A.

Error: in macro substitution

B.

Error: invalid reference 'x' in macro

C.

print 'multiply'

D.

No output

Answer: Option C

136 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The macro #define str(x) #x replaces the symbol 'str(x)' with 'x'.

The macro #define Xstr(x) str(x) replaces the symbol 'Xstr(x)' with
'str(x)'.

The macro #define oper multiply replaces the symbol 'oper' with
'multiply'.

Step 1:

char *opername = Xstr(oper);

The varible *opername is declared as an pointer to a character type.

=> Xstr(oper);

becomes,

=> Xstr(multiply);
=> str(multiply)
=> char *opername = multiply

Step 2:

printf("%s\n", opername);

It prints the value of variable opername.

Hence the output of the program is "multiply".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 137


13. What will be the output of the program?

#include <stdio.h>
#define MESS junk

int main()
{
printf("MESS\n");
return 0;
}

A.

junk

B.

MESS

C.

Error

D.

Nothing will print

Answer: Option B

Explanation:

printf("MESS\n");

It prints the text "MESS". There is no macro calling inside the printf
statement occured.

138 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


14. What will be the output of the program?

#include <stdio.h>
#define PRINT(i) printf("%d,", i)

int main()
{
int x = 2, y = 3, z = 4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}

A.

2, 3, 4,

B.

2, 2, 2,

C.

3, 3, 3,

D.

4, 4, 4,

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 139


Explanation:

The macro

PRINT(i) print("%d,", i);

prints the given variable value in an integer format.

Step 1:

int x = 2, y = 3, z = 4;

The variable x, y, z are declared as an integer type and initialized to


2, 3, 4 respectively.

Step 2:

PRINT(x);

becomes printf("%d,", x). Hence it prints '2'.

Step 3:

PRINT(y);

becomes printf("%d,", y). Hence it prints '3'.

Step 4:

PRINT(z);

becomes printf("%d,", z). Hence it prints '4'.

Hence the output of the program is 2, 3, 4.

140 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


15. What will be the output of the program?

#include <stdio.h>
#define MAX(a, b, c) (a > b ? a > c ? a : c: b > c ? b : c)

int main()
{
int x;
x = MAX(3 + 2, 2 + 7, 3 + 7);
printf("%d\n", x);
return 0;
}

A.

B.

C.

10

D.

3+7

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 141


Explanation:

The macro

MAX(a, b, c) (a > b ? a > c ? a : c: b > c ? b : c)

returns the biggest of given three numbers.

Step 1:

int x;

The variable x is declared as an integer type.

Step 2:

x = MAX(3 + 2, 2 + 7, 3 + 7);

becomes,

=> x = (3+2 > 2+7 ? 3+2 > 3+7 ? 3+2 : 3+7 : 2+7 > 3+7 ? 2+7 : 3+7)
=> x = (5 > 9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )
=> x = (5 > 9 ? (10): (10))
=> x = 10

Step 3:

printf("%d\n", x);

It prints the value of 'x'.

Hence the output of the program is "10".

142 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Pointers

1. What will be the output of the program?

#include <stdio.h>

int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s + 3, s + 2, s + 1, s}, ***p;
p = ptr;
++p;
printf("%s", **p + 1);
return 0;
}

A.

ink

B.

ack

C.

Ite

D.

let

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 143


2. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 3, *j, k;
j = &i;
printf("%d\n", i * *j * i + *j);
return 0;
}

A.

30

B.

27

C.

D.

Answer: Option A

144 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>

int main()
{
int x = 30, *y, *z;
y = &x; /* Assume address of x is 500 and integer is 4 byte size */
z = y;
*y++ = *z++;
x++;
printf("x = %d, y = %d, z = %d\n", x, y, z);
return 0;
}

A.

x = 31, y = 502, z = 502

B.

x = 31, y = 500, z = 500

C.

x = 31, y = 498, z = 498

D.

x = 31, y = 504, z = 504

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 145


4. What will be the output of the program?

#include <stdio.h>

int main()
{
char str[20] = "Hello";
char *const p = str;
*p = 'M';
printf("%s\n", str);
return 0;
}

A.

Mello

B.

Hello

C.

HMello

D.

MHello

Answer: Option A

Explanation:

near = 2, far = 4 and huge = 4 pointers exist only under DOS. Under
windows and Linux every pointers is 4 bytes long.

146 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program If the integer is 4bytes
long?

#include <stdio.h>

int main()
{
int ***r, **q, *p, i = 8;
p = &i;
q = &p;
r = &q;
printf("%d, %d, %d\n", *p, **q, ***r);
return 0;
}

A.

8, 8, 8

B.

4000, 4002, 4004

C.

4000, 4004, 4008

D.

4000, 4008, 4016

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 147


6. What will be the output of the program?

#include <stdio.h>

void fun(void *p);


int i;

int main()
{
void *vptr;
vptr = &i;
fun(vptr);
return 0;
}

void fun(void *p)


{
int **q;
q = (int**)&p;
printf("%d\n", **q);
}

A.

Error: cannot convert from void** to int**

B.

Garbage value

C.

D.

No output

Answer: Option C

148 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What will be the output of the program?

#include <stdio.h>

int main()
{
char *str;
str = "%s";
printf(str, "K\n");
return 0;
}

A.

Error

B.

No output

C.

D.

%s

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 149


8. What will be the output of the program?

#include <stdio.h>

int *check(static int, static int);

int main()
{
int *c;
c = check(10, 20);
printf("%d\n", c);
return 0;
}

int *check(static int i, static int j)


{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}

A.

10

B.

20

C.

Error: Non portable pointer conversion

D.

Error: cannot use static for function parameters

Answer: Option D

150 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What will be the output of the program if the size of pointer is 4-
bytes?

#include <stdio.h>

int main()
{
printf("%d, %d\n", sizeof(NULL), sizeof(""));
return 0;
}

A.

2, 1

B.

2, 2

C.

4, 1

D.

4, 2

Answer: Option C

Explanation:

In TurboC, the output will be 2, 1 because the size of the pointer is 2


bytes in 16-bit platform.

But in Linux, the output will be 4, 1 because the size of the pointer is
4 bytes.

This difference is due to the platform dependency of C compiler.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 151


10. What will be the output of the program?

#include <stdio.h>

int main()
{
void *vp;
char ch = 74, *cp = "JACK";
int j = 65;
vp = &ch;
printf("%c", *(char*)vp);
vp = &j;
printf("%c", *(int*)vp);
vp = cp;
printf("%s", (char*)vp + 2);
return 0;
}

A.

JCK

B.

J65K

C.

JAK

D.

JACK

Answer: Option D

152 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


11. What will be the output of the program?

#include <stdio.h>

int main()
{
int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
int *p, *q;
p = &arr[1][1][1];
q = (int*) arr;
printf("%d, %d\n", *p, *q);
return 0;
}

A.

8, 10

B.

10, 2

C.

8, 1

D.

Garbage values

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 153


12. What will be the output of the program assuming that the array
begins at the location 1002 and size of an integer is 4 bytes?

#include <stdio.h>

int main()
{
int a[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
printf("%u, %u, %u\n", a[0] + 1, *(a[0] + 1), *(*(a + 0) + 1));
return 0;
}

A.

448, 4, 4

B.

520, 2, 2

C.

1006, 2, 2

D.

Error

Answer: Option C

154 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


13. What will be the output of the program?

#include <stdio.h>

int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p + 1);
printf("%d", *p);
return 0;
}

A.

2, 3

B.

2, 0

C.

2, Garbage value

D.

0, 0

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 155


14. What will be the output of the program?

#include <stdio.h>

int main()
{
char *str;
str = "%d\n";
str++;
str++;
printf(str - 2, 300);
return 0;
}

A.

No output

B.

30

C.

D.

300

Answer: Option D

156 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


15. What will be the output of the program?

#include <stdio.h>

int main()
{
printf("%c\n", 7["IndiaREC"]);
return 0;
}

A.

Error: in printf

B.

Nothing will print

C.

print "C" of IndiaREC

D.

print "7"

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 157


16. What will be the output of the program?

#include <stdio.h>

int main()
{
char str[] = "peace";
char *s = str;
printf("%s\n", s++ +3);
return 0;
}

A.

peace

B.

eace

C.

ace

D.

ce

Answer: Option D

158 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


17. What will be the output of the program?

#include <stdio.h>

int main()
{
char *p;
p = "hello";
printf("%s\n", *&*&p);
return 0;
}

A.

llo

B.

hello

C.

ello

D.

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 159


18. What will be the output of the program assuming that the array
begins at location 1002?

#include <stdio.h>

int main()
{
int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2},
{2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
return 0;
}

A.

1002, 2004, 4008, 2

B.

2004, 4008, 8016, 1

C.

1002, 1002, 1002, 1

D.

Error

Answer: Option C

160 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


19. What will be the output of the program?

#include <stdio.h>

power(int**);

int main()
{
int a = 5, *aa; /* Address of 'a' is 1000 */
aa = &a;
a = power(&aa);
printf("%d\n", a);
return 0;
}

power(int **ptr)
{
int b;
b = **ptr * **ptr;
return (b);
}

A.

B.

25

C.

125

D.

Garbage value

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 161


20. What will be the output of the program?

#include <stdio.h>

int main()
{
char str1[] = "India";
char str2[] = "REC";
char *s1 = str1, *s2 = str2;
while(*s1++ = *s2++)
printf("%s", str1);
printf("\n");
return 0;
}

A.

IndiaREC

B.

RndiaREdiaRICia

C.

REC

D.

(null)

Answer: Option B

162 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


21. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
int i, n;
char *x = "Alice";
n = strlen(x);
*x = x[n];
for(i = 0; i <= n; i++)
{
printf("%s ", x);
x++;
}
printf("\n", x);
return 0;
}

A.

Alice

B.

ecilA

C.

Alice lice ice ce e

D.

lice ice ce e

Answer: Option D

Explanation:

If you compile and execute this program in windows platform with Turbo C,
it will give "lice ice ce e".

It may give different output in other platforms (depends upon compiler


and machine). The online C compiler given in this site will give the
Option C as output (it runs on Linux platform).

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 163


22. What will be the output of the program?

#include <stdio.h>

int main()
{
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
for(i = 0; i <= 4; i++)
printf("%d, ", a[i]);
return 0;
}

void change(int *b, int n)


{
int i;
for(i = 0; i < n; i++)
*(b + 1) = *(b + i) + 5;
}

A.

7, 9, 11, 13, 15

B.

2, 15, 6, 8, 10

C.

2 4 6 8 10

D.

3, 1, -1, -3, -5

Answer: Option B

164 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


23. If the size of integer is 4bytes, what will be the output of the
program?

#include <stdio.h>

int main()
{
int arr[] = {12, 13, 14, 15, 16};
printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
return 0;
}

A.

10, 2, 4

B.

20, 4, 4

C.

16, 2, 2

D.

20, 2, 2

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 165


166 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Arrays

1. What will be the output of the program?

#include <stdio.h>

int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}

A.

2, 1, 15

B.

1, 2, 5

C.

3, 2, 15

D.

2, 3, 20

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 167


Explanation:

Step 1:

int a[5] = {5, 1, 15, 20, 25};

The variable arr is declared as an integer array with a size of 5 and it


is initialized to

a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25

Step 2:

int i, j, m;

The variable i, j, m are declared as an integer type.

Step 3:

i = ++a[1];

becomes i = ++1; Hence i = 2 and a[1] = 2.

Step 4:

j = a[1]++;

becomes j = 2++; Hence j = 2 and a[1] = 3.

Step 5:

m = a[i++];

becomes m = a[2]; Hence m = 15 and i is incremented by 1 (i++ means 2++


so i=3).

Step 6:

printf("%d, %d, %d", i, j, m);

It prints the value of the variables i, j, m.

Hence the output of the program is 3, 2, 15.

168 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


2. What will be the output of the program?

#include <stdio.h>

int main()
{
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
printf("%d, %d, %d, %d\n", *(*(p + i) + j), *(*(j + p) + i),
*(*(i + p) + j), *(*(p + j) + i));
}
}
return 0;
}

A.

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

B.

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

C.

1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3

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

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 169


3. What will be the output of the program?

#include <stdio.h>

int main()
{
void fun(int, int[]);
int arr[] = {1, 2, 3, 4};
int i;
fun(4, arr);
for(i = 0; i < 4; i++)
printf("%d,", arr[i]);
return 0;
}

void fun(int n, int arr[])


{
int *p = 0;
int i = 0;
while(i++ < n)
p = &arr[i];
*p = 0;
}

A.

2, 3, 4, 5

B.

1, 2, 3, 4,

C.

0, 1, 2, 3

D.

3, 2, 1 0

Answer: Option B

170 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

void fun(int, int[]);

This prototype tells the compiler that the functionfun() accepts one
integer value and one array as an arguments and does not return anything.

Step 2:

int arr[] = {1, 2, 3, 4};

The variable a is declared as an integer array and it is initialized to

a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4.

Step 3:

int i;

The variable i is declared as an integer type.

Step 4:

fun(4, arr);

This function does not affect the output of the program. Let's skip this
function.

Step 5:

for(i = 0; i < 4; i++)


{
printf("%d,", arr[i]);
}

The for loop runs untill the variable i is less than '4' and it prints
the each value of array a.

Hence the output of the program is 1,2,3,4.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 171


4. What will be the output of the program?

#include <stdio.h>
void fun(int **p);
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
int *ptr;
ptr = &a[0][0];
fun(&ptr);
return 0;
}

void fun(int **p)


{
printf("%d\n", **p);
}

A.

B.

C.

D.

Answer: Option A

172 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};

The variable a is declared as an multidimensional integer array with size


of 3 rows 4 columns.

Step 2:

int *ptr;

The *ptr is a integer pointer variable.

Step 3:

ptr = &a[0][0];

Here we are assigning the base address of the array a to the pointer
variable *ptr.

Step 4:

fun(&ptr);

Now, the &ptr contains the base address of array a.

Step 5: Inside the function fun(&ptr); The

printf("%d\n", **p);

prints the value '1'.

because the *p contains the base address or the first element memory
address of the array a (ie. a[0]).

**p contains the value of *p memory location (ie. a[0]=1).

Hence the output of the program is '1'

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 173


5. What will be the output of the program?

#include <stdio.h>

int main()
{
static int arr[] = {0, 1, 2, 3, 4};
int *p[] = {arr, arr + 1, arr + 2, arr + 3, arr + 4};
int **ptr = p;
ptr++;
printf("%d, %d, %d\n", ptr - p, *ptr - arr, **ptr);
*ptr++;
printf("%d, %d, %d\n", ptr - p, *ptr - arr, **ptr);
*++ptr;
printf("%d, %d, %d\n", ptr - p, *ptr - arr, **ptr);
++*ptr;
printf("%d, %d, %d\n", ptr - p, *ptr - arr, **ptr);
return 0;
}

A.

0, 0, 0
1, 1, 1
2, 2, 2
3, 3, 3

B.

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

C.

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

D.

0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5

Answer: Option C

174 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


6. What will be the output of the program if the array begins at 65472
and each integer occupies 2 bytes?

#include <stdio.h>

int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u\n", a + 1, &a + 1);
return 0;
}

A.

65474, 65476

B.

65480, 65496

C.

65480, 65488

D.

65474, 65488

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 175


Explanation:

Step 1:

int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};

The array a[3][4] is declared as an integer array having the 3 rows and 4
colums dimensions.

Step 2:

printf("%u, %u\n", a+1, &a+1);

The base address (also the address of the first element) of array is
65472.

For a two-dimensional array like a reference to array has type "pointer


to array of 4 ints". Therefore, a+1 is pointing to the memory location of
first element of the second row in array a. Hence 65472 + (4 ints * 2
bytes) = 65480

Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12


ints. Therefore, &a + 1 denotes "12 ints * 2 bytes * 1 = 24 bytes".

Hence, begining address 65472 + 24 = 65496. So, &a + 1 = 65496.

Hence the output of the program is 65480, 65496.

176 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What will be the output of the program in Turb C (under DOS)?

#include <stdio.h>

int main()
{
int arr[5], i = 0;
while(i<5)
arr[i] = ++i;

for(i = 0; i < 5; i++)


printf("%d, ", arr[i]);

return 0;
}

A.

1, 2, 3, 4, 5,

B.

Garbage value, 1, 2, 3, 4,

C.

0, 1, 2, 3, 4,

D.

2, 3, 4, 5, 6,

Answer: Option B

Explanation:

Since C is a compiler dependent language, it may give different outputs


at different platforms. We have given the TurboC Compiler (Windows)
output.

Please try the above programs in Windows (Turbo-C Compiler) and Linux
(GCC Compiler), you will understand the difference better.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 177


8. What will be the output of the program?

#include <stdio.h>

int main()
{
int arr[1] = {10};
printf("%d\n", 0[arr]);
return 0;
}

A.

B.

10

C.

D.

Answer: Option B

Explanation:

Step 1:

int arr[1] = {10};

The variable arr[1] is declared as an integer array with size '2' and
it's first element is initialized to value '10' (means arr[0] = 10).

Step 2:

printf("%d\n", 0[arr]);

It prints the first element value of the variable arr.

Hence the output of the program is 10.

178 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What will be the output of the program if the array begins at
address 65486?

#include <stdio.h>

int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%u, %u\n", arr, &arr);
return 0;
}

A.

65486, 65488

B.

65486, 65486

C.

65486, 65490

D.

65486, 65487

Answer: Option B

Explanation:

Step 1:

int arr[] = {12, 14, 15, 23, 45};

The variable arr is declared as an integer array and initialized.

Step 2:

printf("%u, %u\n", arr, &arr);

Here,

The base address of the array is 65486.

=> arr, &arr is pointing to the base address of the array arr.

Hence the output of the program is 65486, 65486.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 179


10. What will be the output of the program?

#include <stdio.h>

int main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7};
printf("%d\n", sizeof(arr) / sizeof(arr[0]));
return 0;
}

A.

B.

C.

D.

Answer: Option B

180 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The sizeof function return the given variable. Example: float a=10;
sizeof(a) is 4 bytes.

Step 1:

float arr[] = {12.4, 2.3, 4.5, 6.7};

The variable arr is declared as an floating point array and it is


initialized with the values.

Step 2:

printf("%d\n", sizeof(arr)/sizeof(arr[0]));

The variable arr has 4 elements. The size of the float variable is 4
bytes.

Hence 4 elements x 4 bytes = 16 bytes

sizeof(arr[0]) is 4 bytes

Hence 16/4 is 4 bytes.

Hence the output of the program is '4'.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 181


11. What will be the output of the program if the array begins 1200 in
memory?

#include <stdio.h>
int main()
{
int arr[] = {2, 3, 4, 1, 6};
printf("%u, %u, %u\n", arr, &arr[0], &arr);
return 0;
}

A.

1200, 1202, 1204

B.

1200, 1200, 1200

C.

1200, 1204, 1208

D.

1200, 1202, 1200

Answer: Option B

Explanation:

Step 1:

int arr[]={2, 3, 4, 1, 6};

The variable arr is declared as an integer array and initialized.

Step 2:

printf("%u, %u, %u\n", arr, &arr[0], &arr);

Here,

The base address of the array is 1200.

=> arr, &arr is pointing to the base address of the array arr.

=> &arr[0] is pointing to the address of the first element array arr (ie.
base address).

Hence the output of the program is 1200, 1200, 1200.

182 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Strings

1. What will be the output of the program?

#include <stdio.h>
#include<string.h>
int main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%s\n", strcpy(str2, strcat(str1, str2)));
return 0;
}

A.

Hello

B.

World

C.

Hello World

D.

WorldHello

Answer: Option C

Explanation:

Step 1:

char str1[20] = "Hello", str2[20] = " World";

The variable str1and str2 is declared as an array of characters and


initialized with value "Hello" and " World" respectively.

Step 2:

printf("%s\n", strcpy(str2, strcat(str1, str2)));

=> strcat(str1, str2)) it append the string str2 to str1. The result will
be stored in str1. Therefore str1 contains "Hello World".

=> strcpy(str2, "Hello World") it copies the "Hello World" to the


variable str2.

Hence it prints "Hello World".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 183


2. What will be the output of the program?

#include <stdio.h>
int main()
{
char p[] = "%d\n";
p[1] = 'c';
printf(p, 65);
return 0;
}

A.

B.

C.

D.

65

Answer: Option A

Explanation:

Step 1:

char p[] = "%d\n";

The variable p is declared as an array of characters and initialized with


string "%d".

Step 2:

p[1] = 'c';

Here, we overwrite the second element of array p by 'c'. So array p


becomes "%c".

Step 3:

printf(p, 65);

becomes printf("%c", 65); Therefore, it prints the ASCII value of 65. The
output is 'A'.

184 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
printf("%d\n", strlen("123456"));
return 0;
}

A.

B.

12

C.

D.

Answer: Option A

Explanation:

The function strlen returns the number of characters in the given string.

Therefore, strlen("123456") returns 6.

Hence the output of the program is "6".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 185


4. What will be the output of the program?

#include <stdio.h>

int main()
{
printf(5 + "Good Morning\n");
return 0;
}

A.

Good Morning

B.

Good

C.

D.

Morning

Answer: Option D

Explanation:

printf(5 + "Good Morning\n");

It skips the 5 characters and prints the given string.

Hence the output is "Morning".

186 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
char str[] = "India\0\REC\0";
printf("%s\n", str);
return 0;
}

A.

REC

B.

India

C.

India REC

D.

India\0REC

Answer: Option B

Explanation:

A string is a collection of characters terminated by '\0'.

Step 1:

char str[] = "India\0\REC\0";

The variable str is declared as an array of characters and initialized


with value "India".

Step 2:

printf("%s\n", str);

It prints the value of the str.

The output of the program is "India".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 187


6. What will be the output of the program If characters 'a', 'b' and
'c' enter are supplied as input?

#include <stdio.h>

int main()
{
void fun();
fun();
printf("\n");
return 0;
}

void fun()
{
char c;
if((c = getchar())!= '\n')
fun();
printf("%c", c);
}

A.

abc abc

B.

bca

C.

Infinite loop

D.

cba

Answer: Option D

188 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

void fun();

This is the prototype for the function fun().

Step 2:

fun();

The function fun() is called here.

The function fun() gets a character input and the input is terminated by
an enter key(New line character). It prints the given character in the
reverse order.

The given input characters are "abc".

Output: cba

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 189


7. What will be the output of the program?

#include <stdio.h>

int main()
{
printf("India", "REC\n");
return 0;
}

A.

Error

B.

India REC

C.

India D.

REC

Answer: Option C

Explanation:

printf("India", "REC\n");

It prints "India".

Because ,(comma) operator has Left to Right associativity. After printing


"India", the statement got terminated.

190 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


8. What will be the output of the program?

#include <stdio.h>

int main()
{
char str[7] = "IndiaREC";
printf("%s\n", str);
return 0;
}

A.

Error

B.

IndiaREC

C.

Cannot predict

D.

None of above

Answer: Option C

Explanation:

Here str[] has declared as 7 character array and into a 8 character is


stored. This will result in overwriting of the byte beyond 7 byte
reserved for '\0'.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 191


9. What will be the output of the program?

#include <stdio.h>

int main()
{
char *names[] = {"Suresh", "Siva", "Sona", "Baiju", "Ritu"};
int i;
char *t;
t = names[3];
names[3] = names[4];
names[4] = t;
for(i = 0; i <= 4; i++)
printf("%s,", names[i]);
return 0;
}

A.

Suresh, Siva, Sona, Baiju, Ritu

B.

Suresh, Siva, Sona, Ritu, Baiju

C.

Suresh, Siva, Baiju, Sona, Ritu

D.

Suresh, Siva, Ritu, Sona, Baiju

Answer: Option B

192 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

char *names[] = {"Suresh", "Siva", "Sona", "Baiju", "Ritu"};

The variable names is declared as an pointer to a array of strings.

Step 2:

int i;

The variable i is declared as an integer type.

Step 3:

char *t;

The variable t is declared as pointer to a string.

Step 4:

t = names[3];
names[3] = names[4];
names[4] = t;

These statements the swaps the 4 and 5 element of the array names.

Step 5:

for(i=0; i<=4; i++)


printf("%s,", names[i]);

These statement prints the all the value of the array names.

Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 193


10. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
char str[] = "India\0\REC\0";
printf("%d\n", strlen(str));
return 0;
}

A.

10

B.

C.

D.

11

Answer: Option C

Explanation:

The function strlen returns the number of characters int the given
string.

Therefore, strlen(str) becomes strlen("India") contains 5 characters. A


string is a collection of characters terminated by '\0'.

The output of the program is "5".

194 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


11. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
static char str1[] = "dills";
static char str2[20];
static char str3[] = "Daffo";
int i;
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
printf("%d\n", i);
return 0;
}

A.

B.

C.

D.

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 195


12. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
static char s[] = "Hello!";
printf("%d\n", *(s + strlen(s)));
return 0;
}

A.

B.

C.

16

D.

Error

Answer: Option B

196 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


13. What will be the output of the program?

#include <stdio.h>

int main()
{
static char s[25] = "The cocaine man";
int i=0;
char ch;
ch = s[++i];
printf("%c", ch);
ch = s[i++];
printf("%c", ch);
ch = i++[s];
printf("%c", ch);
ch = ++i[s];
printf("%c", ch);
return 0;
}

A.

hhe!

B.

he c

C.

The c D.

Hhec

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 197


14. What will be the output of the program in 16-bit platform (Turbo C
under DOS)?

#include <stdio.h>

int main()
{
printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));
return 0;
}

A.

8, 1, 4

B.

4, 2, 8

C.

4, 2, 4

D.

10, 3, 4

Answer: Option B

Explanation:

Step 1:

printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));

The sizeof function returns the size of the given expression.

sizeof(3.0f) is a floating point constant. The size of float is 4 bytes.

sizeof('3') It converts '3' in to ASCII value.. The size of int is 2


bytes.

sizeof(3.0) is a double constant. The size of double is 8 bytes.

Hence the output of the program is 4,2,8

Note: The above program may produce different output in other platform
due to the platform dependency of C compiler.

In Turbo C, 4 2 8. But in GCC, the output will be 4 4 8.

198 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


15. What will be the output of the program?

#include <stdio.h>
int main()
{
int i;
char a[] = "\0";
if(printf("%s", a))
printf("The string is empty\n");
else
printf("The string is not empty\n");
return 0;
}

A.

The string is empty

B.

The string is not empty

C.

No output

D.

Answer: Option B

Explanation:

The function printf() returns the number of charecters printed on the


console.

Step 1:

char a[] = "\0";

The variable a is declared as an array of characters and it initialized


with "\0". It denotes that the string is empty.

Step 2:

if(printf("%s", a))

The printf() statement does not print anything, so it returns '0'(zero).


Hence the if condition is failed. In the else part it prints "The string
is not empty".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 199


16. If char=1, int=4, and float=4 bytes size, What will be the output
of the program ?

#include <stdio.h>

int main()
{
char ch = 'A';
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
return 0;
}

A.

1, 2, 4

B.

1, 4, 4

C.

2, 2, 4

D.

2, 4, 8

Answer: Option B

Explanation:

Step 1:

char ch = 'A'; The variable ch is declared as an character type and


initialized with value 'A'.

Step 2:

printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14));

The sizeof function returns the size of the given expression.

sizeof(ch) becomes sizeof(char). The size of char is 1 byte.

sizeof('A') becomes sizeof(65). The size of int is 4 bytes (as mentioned


in the question).

sizeof(3.14f). The size of float is 4 bytes.

Hence the output of the program is 1, 4, 4

200 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


17. If the size of pointer is 32 bits What will be the output of the
program?

#include <stdio.h>

int main()
{
char a[] = "Visual C++";
char *b = "Visual C++";
printf("%d, %d\n", sizeof(a), sizeof(b));
printf("%d, %d", sizeof(*a), sizeof(*b));
return 0;
}

A.

10, 2
2, 2

B.

10, 4
1, 2

C.

11, 4
1, 1

D.

12, 2
2, 2

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 201


18. What will be the output of the program?

#include <stdio.h>

int main()
{
static char mess[6][30] = {"Don't walk in front of me...",
"I may not follow;",
"Don't walk behind me...",
"Just walk beside me...",
"And be my friend."};
printf("%c, %c\n", *(mess[2] + 9), *(*(mess + 2) + 9));
return 0;
}

A.

t, t

B.

k, k

C.

n, k

D.

m, f

Answer: Option B

202 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


19. What will be the output of the program?

#include <stdio.h>

int main()
{
char str1[] = "Hello";
char str2[10];
char *t, *s;
s = str1;
t = str2;
while(*t = *s)
*t++ = *s++;
printf("%s\n", str2);
return 0;
}

A.

Hello

B.

HelloHello

C.

No output

D.

ello

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 203


20. What will be the output of the program?

#include <stdio.h>

int main()
{
char str[] = "India\0REC\0";
printf("%d\n", sizeof(str));
return 0;
}

A.
10

B.

C.

D.

11

Answer: Option D

Explanation:

The following examples may help you understand this problem:

1. sizeof("") returns 1 (1*).

2. sizeof("India") returns 6 (5 + 1*).

3. sizeof("REC") returns 4 (3 + 1*).

4. sizeof("India\0REC") returns 10 (5 + 1 + 3 + 1*). Here '\0' is


considered as 1 char by sizeof() function.

5. sizeof("India\0REC\0") returns 11 (5 + 1 + 3 + 1 + 1*). Here '\0' is


considered as 1 char by sizeof() function.

204 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


21. What will be the output of the program?

#include <stdio.h>

int main()
{
char str[25] = "IndiaREC";
printf("%s\n", &str + 2);
return 0;
}

A.

Garbage value

B.

Error

C.

No output

D.

diaREC

Answer: Option A

Explanation:

Step 1:

char str[25] = "IndiaREC";

The variable str is declared as an array of characteres and initialized


with a string "IndiaREC".

Step 2:

printf("%s\n", &str+2);

=> In the printf statement %s is string format specifier tells the


compiler to print the string in the memory of &str+2.

=> &str is a location of string "IndiaREC". Therefore &str+2 is another


memory location.

Hence it prints the Garbage value.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 205


22. What will be the output of the program?

#include <stdio.h>

int main()
{
char str = "IndiaREC";
printf("%s\n", str);
return 0;
}

A.

Error

B.

IndiaREC

C.

Base address of str

D.

No output

Answer: Option A

Explanation:

The line

char str = "IndiaREC";

generates "Non portable pointer conversion" error.

To eliminate the error, we have to change the above line to

char *str = "IndiaREC"; (or) char str[] = "IndiaREC";

Then it prints "IndiaREC".

206 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


23. What will be the output of the program?

#include <stdio.h>

int main()
{
char str[] = "Nagpur";
str[0] = 'K';
printf("%s, ", str);
str = "Kanpur";
printf("%s", str + 1);
return 0;
}

A.

Kagpur, Kanpur

B.

Nagpur, Kanpur

C.

Kagpur, anpur

D.

Error

Answer: Option D

Explanation:

The statement

str = "Kanpur";

generates the LVALUE required error. We have to use strcpy function to


copy a string.

To remove error we have to change this statement

str = "Kanpur"; tostrcpy(str, "Kanpur");

The program prints the string "anpur"

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 207


24. What will be the output of the program?

#include <stdio.h>

int main()
{
printf(5 + "IndiaREC\n");
return 0;
}

A.

Error

B.

IndiaREC

C.

REC

D.

None of above

Answer: Option C

Explanation:

printf(5+"IndiaREC\n");

In the printf statement, it skips the first 5 characters and it prints


"REC".

208 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


25. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
char sentence[80];
int i;
printf("Enter a line of text\n");
gets(sentence);
for(i = strlen(sentence) - 1; i >=0; i--)
putchar(sentence[i]);
return 0;
}

A.

The sentence will get printed in same order as it entered

B.

The sentence will get printed in reverse order

C.

Half of the sentence will get printed

D.

None of above

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 209


26. What will be the output of the program?

#include <stdio.h>
void swap(char *, char *);

int main()
{
char *pstr[2] = {"Hello", "IndiaREC"};
swap(pstr[0], pstr[1]);
printf("%s\n%s", pstr[0], pstr[1]);
return 0;
}

void swap(char *t1, char *t2)


{
char *t;
t = t1;
t1 = t2;
t2 = t;
}

A.

IndiaREC
Hello

B.

Address of "Hello" and "IndiaREC"

C.

Hello
IndiaREC

D.

Iello
HndiaREC

Answer: Option C

210 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

void swap(char *, char *);

This prototype tells the compiler that the function swap accept two
strings as arguments and it does not return anything.

Step 2:

char *pstr[2] = {"Hello", "IndiaREC"};

The variable pstr is declared as an pointer to the array of strings. It


is initialized to pstr[0] = "Hello", pstr[1] = "IndiaREC".

Step 3:

swap(pstr[0], pstr[1]);

The swap function is called by "call by value". Hence it does not affect
the output of the program.

If the swap function is "called by reference" it will affect the variable


pstr.

Step 4:

printf("%s\n%s", pstr[0], pstr[1]);

It prints the value of pstr[0]and pstr[1].

Hence the output of the program is:

Hello
IndiaREC

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 211


27. What will be the output of the program (Turbo C in 16 bit platform
DOS)?

#include <stdio.h>
#include<string.h>

int main()
{
char *str1 = "India";
char *str2 = "REC";
char *str3;
str3 = strcat(str1, str2);
printf("%s %s\n", str3, str1);
return 0;
}

A.

IndiaREC India

B.

IndiaREC IndiaREC

C.

India India

D.

Error

Answer: Option B

Explanation:

It prints 'IndiaREC IndiaREC' in TurboC (in 16 bit platform).

It may cause a 'segmentation fault error' in GCC (32 bit platform).

212 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


28. If the size of pointer is 4 bytes then What will be the output of
the program?

#include <stdio.h>

int main()
{
char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};
printf("%d, %d", sizeof(str), strlen(str[0]));
return 0;
}

A.

22, 4

B.

25, 5

C.

24, 5

D.

20, 2

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 213


Explanation:

Step 1:

char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};

The variable str is declared as an pointer to the array of 6 strings.

Step 2:

printf("%d, %d", sizeof(str), strlen(str[0]));

sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'.

strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5'.

Hence the output of the program is 24, 5.

Hint: If you run the above code in 16 bit platform (Turbo C under DOS)
the output will be 12, 5. Because the pointer occupies only 2 bytes. If
you run the above code in Linux (32 bit platform), the output will be 24,
5 (because the size of pointer is 4 bytes).

214 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


29. What will be the output of the program?

#include <stdio.h>

int main()
{
int i;
char a[] = "\0";
if(printf("%s", a))
printf("The string is not empty\n");
else
printf("The string is empty\n");
return 0;
}

A.

The string is not empty

B.

The string is empty

C.

No output

D.

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 215


Explanation:

The function printf() returns the number of charecters printed on the


console.

Step 1:

char a[] = '\0';

The variable a is declared as an array of characters and it initialized


with "\0". It denotes that the string is empty.

Step 2:

if(printf("%s", a))

The printf() statement does not print anything, so it returns '0'(zero).


Hence the if condition is failed.

In the else part it prints "The string is empty".

216 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


30. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
char str1[5], str2[5];
int i;
gets(str1);
gets(str2);
i = strcmp(str1, str2);
printf("%d\n", i);
return 0;
}

A.

Unpredictable integer value

B.

C.

-1

D.

Error

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 217


Explanation:

gets() gets collects a string of characters terminated by a new line from


the standard input stream stdin.

The gets(str1) read the input string from user and store in variable
str1.

The gets(str2) read the input string from user and store in variable
str2.

The code

i = strcmp(str1, str2);

The strcmp not only returns -1, 0 and +1, but also other negative or
positive values. So the value of i is "unpredictable integer value".

printf("%d\n", i);

It prints the value of variable i.

218 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


31. What will be the output of the program in Turbo C?

#include <stdio.h>

int main()
{
char str[10] = "India";
str[6] = "REC";
printf("%s\n", str);
return 0;
}

A.

India REC

B.

REC

C.

India D.

Error

Answer: Option D

Explanation:

str[6] = "REC"; - Nonportable pointer conversion.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 219


32. What will be the output of the program?

#include <stdio.h>

int main()
{
char str1[] = "Hello";
char str2[] = "Hello";
if(str1 == str2)
printf("Equal\n");
else
printf("Unequal\n");
return 0;
}

A.

Equal

B.

Unequal

C.

Error

D.

None of above

Answer: Option B

220 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

char str1[] = "Hello";

The variable str1 is declared as an array of characters and initialized


with a string "Hello".

Step 2:

char str2[] = "Hello";

The variable str2 is declared as an array of characters and initialized


with a string "Hello".

We have use strcmp(s1,s2) function to compare strings.

Step 3:

if(str1 == str2)

here the address of str1 and str2 are compared. The address of both
variable is not same. Hence the if condition is failed.

Step 4: At the else part it prints "Unequal".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 221


33. What will be the output of the program?

#include <stdio.h>

int main()
{
char t;
char *p1 = "India", *p2;
p2 = p1;
p1 = "REC";
printf("%s %s\n", p1, p2);
return 0;
}

A.

India REC

B.

REC India

C.

India India

D.

REC REC

Answer: Option B

222 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

char *p1 = "India", *p2;

The variable p1 and p2 is declared as an pointer to a character value and


p1 is assigned with a value "India".

Step 2:

p2 = p1;

The value of p1 is assigned to variable p2. So p2 contains "India".

Step 3:

p1 = "REC";

The p1 is assigned with a string "REC".

Step 4:

printf("%s %s\n", p1, p2);

It prints the value of p1 and p2.

Hence the output of the program is "REC India".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 223


34. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
printf("%c\n", "abcdefgh"[4]);
return 0;
}

A.

Error

B.

C.

D.

abcdefgh

Answer: Option C

Explanation:

printf("%c\n", "abcdefgh"[4]);

It prints the 5 character of the string "abcdefgh".

Hence the output is 'e'.

224 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


35. What will be the output of the following program in 16 bit platform
assuming that 1022 is memory address of the string "Hello1" (in Turbo C
under DOS)?

#include <stdio.h>

int main()
{
printf("%u %s\n", &"Hello1", &"Hello2");
return 0;
}

A.

1022 Hello2

B.

Hello1 1022

C.

Hello1 Hello2

D.

1022 1022

E.

Error

Answer: Option A

Explanation:

In printf("%u %s\n", &"Hello", &"Hello");.

The %u format specifier tells the compiler to print the memory address of
the "Hello1".

The %s format specifier tells the compiler to print the string "Hello2".

Hence the output of the program is "1022 Hello2".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 225


226 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Structures, Unions, Enums

1. What will be the output of the program?

#include <stdio.h>

int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}

A.

3, 2, 515

B.

515, 2, 3

C.

3, 2, 5

D.

515, 515, 4

Answer: Option A

Explanation:

The system will allocate 2 bytes for the union.

The statements u.ch[0] = 3; u.ch[1] = 2; store data in memory as given


below.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 227


2. What will be the output of the program?

#include <stdio.h>

int main()
{
union var
{
int a, b;
};
union var v;
v.a = 10;
v.b = 20;
printf("%d\n", v.a);
return 0;
}

A.

10

B.

20

C.

30

D.

Answer: Option B

228 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>

int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
} bit = {1, 2, 13};
printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
return 0;
}

A.

1, 2, 13

B.

1, 4, 4

C.

-1, 2, -3

D.

-1, -2, -13

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 229


Explanation:

Note the below statement inside the struct:

int bit1:1;

--> 'int' indicates that it is a SIGNED integer.

For signed integers the leftmost bit will be taken for +/- sign.

If you store 1 in 1-bit field:

The left most bit is 1, so the system will treat the value as negative
number.

The 2's complement method is used by the system to handle the negative
values.

Therefore, the data stored is 1. The 2's complement of 1 is also 1


(negative).

Therefore -1 is printed.

If you store 2 in 4-bits field:

Binary 2: 0010 (left most bit is 0, so system will treat it as positive


value)

0010 is 2

Therefore 2 is printed.

If you store 13 in 4-bits field:

Binary 13: 1101 (left most bit is 1, so system will treat it as negative
value)

Find 2's complement of 1101:

1's complement of 1101 : 0010

2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)

0011 is 3 (but negative value)

Therefore -3 is printed.

230 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


4. What will be the output of the program in 16 bit platform (Turbo C
under DOS)?

#include <stdio.h>

int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
} bit;
printf("%d\n", sizeof(bit));
return 0;
}

A.

B.

C.

D.

Answer: Option B

Explanation:

Since C is a compiler dependent language, in Turbo C (DOS) the output


will be 2, but in GCC (Linux) the output will be 4.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 231


5. What will be the output of the program?

#include <stdio.h>

int main()
{
enum days {MON = -1, TUE, WED = 6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);
return 0;
}

A.

-1, 0, 1, 2, 3, 4

B.

-1, 2, 6, 3, 4, 5

C.

-1, 0, 6, 2, 3, 4

D.

-1, 0, 6, 7, 8, 9

Answer: Option D

232 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


6. What will be the output of the program?

#include <stdio.h>

int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d\n", stud1, stud2, stud3);
return 0;
}

A.

0, 1, 2

B.

1, 2, 3

C.

0, 2, 1

D.

1, 3, 2

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 233


7. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 4, j = 8;
printf("%d, %d, %d\n", i | j & j | i, i | j & j | i, i ^ j);
return 0;
}

A.

12, 12, 12

B.

112, 1, 12

C.

32, 1, 12

D.

-64, 1, 12

Answer: Option A

234 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


8. What will be the output of the program in Turbo C (under DOS)?

#include <stdio.h>

int main()
{
struct emp
{
char *n;
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
strupr(e2.n);
printf("%s\n", e1.n);
return 0;
}

A.

Error: Invalid structure assignment

B.

DRAVID

C.

Dravid

D.

No output

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 235


9. What will be the output of the program in 16-bit platform (under
DOS)?

#include <stdio.h>

int main()
{
struct node
{
int data;
struct node *link;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
printf("%d, %d\n", sizeof(p), sizeof(q));
return 0;
}

A.

2, 2

B.

8, 8

C.

5, 5

D.

4, 4

Answer: Option A

236 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


10. What will be the output of the program?

#include <stdio.h>

int main()
{
struct byte
{
int one : 1;
};
struct byte var = {1};
printf("%d\n", var.one);
return 0;
}

A.

B.

-1

C.

D.

Error

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 237


11. What will be the output of the program?

#include <stdio.h>

int main()
{
enum days {MON = -1, TUE, WED = 6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", ++MON, TUE, WED, THU, FRI, SAT);
return 0;
}

A.

-1, 0, 1, 2, 3, 4

B.

Error

C.

0, 1, 6, 3, 4, 5

D.

0, 0, 6, 7, 8, 9

Answer: Option B

Explanation:

Because ++ or -- cannot be done on enum value.

238 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


12. What will be the output of the program?

#include <stdio.h>

struct course
{
int courseno;
char coursename[25];
};

int main()
{
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"} };
printf("%d ", c[1].courseno);
printf("%s\n", (*(c + 2)).coursename);
return 0;
}

A.

103 DotNet

B.

102 Java

C.

103 PHP

D.

104 DotNet

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 239


13. What will be the output of the program given below in 16-bit
platform?

#include <stdio.h>

int main()
{
enum value {VAL1 = 0, VAL2, VAL3, VAL4, VAL5} var;
printf("%d\n", sizeof(var));
return 0;
}

A.

B.

C.

D.

10

Answer: Option B

240 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Input / Output

1. What will be the content of 'file.c' after executing the following


program?

#include <stdio.h>

int main()
{
FILE *fp1, *fp2;
fp1 = fopen("file.c", "w");
fp2 = fopen("file.c", "w");
fputc('A', fp1);
fputc('B', fp2);
fclose(fp1);
fclose(fp2);
return 0;
}

A.

B.

A
B

C.

B
B

D.

Error in opening file 'file1.c'

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 241


Explanation:

Here

fputc('A', fp1);

stores 'A' in the file1.c then

fputc('B', fp2);

overwrites the contents of the file1.c with value 'B'. Because the fp1
and fp2 opens the file1.c in write mode.

Hence the file1.c contents is 'B'.

242 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


2. What will be the output of the program?

#include <stdio.h>

int main()
{
int k = 1;
printf("%d == 1 is " "%s\n", k, k == 1 ? "TRUE" : "FALSE");
return 0;
}

A.

k == 1 is TRUE

B.

1 == 1 is TRUE

C.

1 == 1 is FALSE

D.

K == 1 is FALSE

Answer: Option B

Explanation:

Step 1:

int k = 1;

The variable k is declared as an integer type and initialized to '1'.

Step 2:

printf("%d == 1 is" "%s\n", k, k == 1 ? "TRUE" : "FALSE");

becomes

=> k == 1 ? "TRUE" : "FALSE"

=> 1 == 1 ? "TRUE" : "FALSE"

=> "TRUE"

Therefore, the output of the program is 1 == 1 is TRUE

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 243


3. What will be the output of the program?

#include <stdio.h>

char *str = "char *str = %c%s%c; main() { printf(str, 34, str, 34); }";

int main()
{
printf(str, 34, str, 34);
return 0;
}

A.

char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34); }";
main(){ printf(str, 34, str, 34);}

B.

char *str = %c%s%c; main(){ printf(str, 34, str, 34);}


C.

No output

D.

Error in program

Answer: Option A

244 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


4. If the file 'source.txt' contains a line "Be my friend" which of
the following will be the output of below program?

#include <stdio.h>

int main()
{
FILE *fs, *ft;
char c[10];
fs = fopen("source.txt", "r");
c[0] = getc(fs);
fseek(fs, 0, SEEK_END);
fseek(fs, -3L, SEEK_CUR);
fgets(c, 5, fs);
puts(c);
return 0;
}

A.

friend

B.

frien

C.

end

D.

Error in fseek();

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 245


Explanation:

The file source.txt contains "Be my friend".

fseek(fs, 0, SEEK_END);

moves the file pointer to the end of the file.

fseek(fs, -3L, SEEK_CUR);

moves the file pointer backward by 3 characters.

fgets(c, 5, fs);

read the file from the current position of the file pointer.

Hence, it contains the last 3 characters of "Be my friend".

Therefore, it prints "end".

246 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program?

#include <stdio.h>

int main()
{
float a = 3.15529;
printf("%2.1f\n", a);
return 0;
}

A.

3.00

B.

3.15

C.

3.2

D.

Answer: Option C

Explanation:

float a = 3.15529;

The variable a is declared as an float data type and initialized to value


3.15529;

printf("%2.1f\n", a);

The precision specifier tells .1f tells the printf function to place only
one number after the .(dot).

Hence the output is 3.2

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 247


6. What will be the output of the program?

#include <stdio.h>

int main()
{
printf("%c\n", ~('C' * -1));
return 0;
}

A.

B.

C.

D.

Answer: Option B

248 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What will be the output of the program?

#include <stdio.h>

int main()
{
FILE *fp;
unsigned char ch;
/* file 'abc.c' contains "This is IndiaREC " */
fp = fopen("abc.c", "r");
if(fp == NULL)
{
printf("Unable to open file");
exit(1);
}
while((ch=getc(fp)) != EOF)
printf("%c", ch);

fclose(fp);
printf("\n", ch);
return 0;
}

A.

This is IndiaREC

B.

This is

C.

Infinite loop

D.

Error

Answer: Option C

Explanation:

The macro EOF means -1.

while((ch=getc(fp)) != EOF)

Here getc function read the character and convert it to an integer value
and store it in the variable ch, but it is declared as an unsigned char.

So the while loop runs infinitely.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 249


8. What will be the output of the program?

#include <stdio.h>

int main()
{
char *p;
p = "%d\n";
p++;
p++;
printf(p - 2, 23);
return 0;
}

A.

21

B.

23

C.

Error

D.

No output

Answer: Option B

250 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What will be the output of the program?

#include <stdio.h>

int main()
{
FILE *ptr;
char i;
ptr = fopen("myfile.c", "r");
while((i = fgetc(ptr)) != NULL)
printf("%c", i);
return 0;
}

A.

Print the contents of file "myfile.c"

B.

Print the contents of file "myfile.c" upto NULL character

C.

Infinite loop

D.

Error in program

Answer: Option C

Explanation:

The program will generate infinite loop. When an EOF is encountered


fgetc() returns EOF. Instead of checking the condition for EOF we have
checked it for NULL. so the program will generate infinite loop.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 251


10. What will be the output of the program?

#include <stdio.h>

int main()
{
printf("%%%%\n");
return 0;
}

A.

%%%%%

B.

%%

C.

No output

D.

Error

Answer: Option B

252 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


11. What will be the output of the program?

#include <stdio.h>

int main()
{
int a = 250;
printf("%1d\n", a);
return 0;
}

A.

1250

B.

C.

50

D.

250

Answer: Option D

Explanation:

int a = 250;

The variable a is declared as an integer type and initialized to value


250.

printf("%1d\n", a);

It prints the value of variable a.

Hence the output of the program is 250.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 253


12. What will be the output of the program?

#include <stdio.h>

int main()
{
FILE *fp;
char ch, str[7];
fp=fopen("try.c", "r"); /* file 'try.c' contains "This is Nagpur" */
fseek(fp, 9L, SEEK_CUR);
fgets(str, 5, fp);
puts(str);
return 0;
}

A.

agpur

B.

gpur

C.

Nagp

D.

agpu

Answer: Option D

254 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


13. What will be the output of the program if value 25 given to
scanf()?

#include <stdio.h>

int main()
{
int i;
printf("%d\n", scanf("%d", &i));
return 0;
}

A.

25

B.

C.

D.

Answer: Option C

Explanation:

The scanf function returns the number of input is given.

printf("%d\n", scanf("%d", &i));

The scanf function returns the value 1(one).

Therefore, the output of the program is '1'.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 255


256 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Command Line Arguments

1. What will be the output of the program (myprog.c) given below if it


is executed from the command line?

C:\BHUVAN>myprog one two three

/* myprog.c */

#include <stdio.h>

int main(int argc, char **argv)


{
printf("%c\n", **++argv);
return 0;
}

A.

myprog one two three

B.

myprog one

C.

D.

two

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 257


2. What will be the output of the program (myprog.c) given below if it
is executed from the command line?

C:\BHUVAN>myprog one two three

/* myprog.c */

#include <stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)


{
printf("%s\n", *++argv);
return 0;
}

A.

myprog

B.

one

C.

two

D.

three

Answer: Option B

258 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program (sample.c) given below if it
is executed from the command line (Turbo C in DOS)?

C:\BHUVAN>sample 1 2 3

/* sample.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
int j;
j = argv[1] + argv[2] + argv[3];
printf("%d", j);
return 0;
}

A.

B.

sample 6

C.

Error

D.

Garbage value

Answer: Option C

Explanation:

Here argv[1], argv[2] and argv[3] are string type. We have to convert the
string to integer type before perform arithmetic operation.

Example: j = atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]);

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 259


4. What will be the output of the program (sample.c) given below if it
is executed from the command line (turbo c under DOS)?

C:\BHUVAN>sample Good Morning

/* sample.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
printf("%d %s", argc, argv[1]);
return 0;
}

A.

3 Good

B.

2 Good

C.

Good Morning

D.

3 Morning

Answer: Option A

260 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program?

#include <stdio.h>

void fun(int);

int main(int argc)


{
printf("%d ", argc);
fun(argc);
return 0;
}

void fun(int i)
{
if(i != 4)
main(++i);
}

A.

1 2 3

B.

1 2 3 4

C.

2 3 4

D.

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 261


6. What will be the output of the program (sample.c) given below if it
is executed from the command line?

C:\BHUVAN>sample "*.c"

/* sample.c */

#include <stdio.h>

int main(int argc, int *argv)


{
int i;
for(i = 1; i < argc; i++)
printf("%s\n", argv[i]);
return 0;
}

A.

*.c

B.

"*.c"

C.

sample *.c

D.

List of all files and folders in the current directory

Answer: Option A

262 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What will be the output of the program if it is executed like
below?

C:\BHUVAN>sample

/* sample.c */

#include <stdio.h>

int main(int argc, char **argv)


{
printf("%s\n", argv[argc - 1]);
return 0;
}

A.

B.

sample

C.

samp

D.

No output

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 263


8. What will be the output of the program (sample.c) given below if it
is executed from the command line?

C:\BHUVAN>sample friday tuesday sunday

/* sample.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
printf("%c", **++argv);
return 0;
}

A.

B.

C.

sample

D.

friday

Answer: Option B

264 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What will be the output of the program (myprog.c) given below if it
is executed from the command line?

C:\BHUVAN>myprog friday tuesday sunday

/* myprog.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
printf("%c", *++argv[1]);
return 0;
}

A.

B.

C.

D.

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 265


10. What will be the output of the program (sample.c) given below if it
is executed from the command line?

C:\BHUVAN>sample one two three

/* sample.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
int i = 0;
i += strlen(argv[1]);
while(i > 0)
{
printf("%c", argv[1][--i]);
}
return 0;
}

A.

three two one

B.

owt

C.

eno

D.

eerht

Answer: Option C

266 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


11. What will be the output of the program in Turbo C?

#include <stdio.h>

int main(int argc, char *argv, char *env[])


{
int i;
for(i = 1; i < argc; i++)
printf("%s\n", env[i]);
return 0;
}

A.

List of all environment variables

B.

List of all command-line arguments

C.

count of command-line arguments

D.

Error: cannot have more than two arguments in main()

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 267


12. What will be the output of the program (sample.c) given below if it
is executed from the command line?

C:\BHUVAN>sample Jan Feb Mar

/* sample.c */

#include <stdio.h>
#include<dos.h>

int main(int arc, char *arv[])


{
int i;
for(i = 1; i < argc; i++)
printf("%s ", argv[i]);
return 0;
}

A.

No output

B.

sample Jan Feb Mar

C.

Jan Feb Mar

D.

Error

Answer: Option C

268 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


13. What will be the output of the program (sample.c) given below if it
is executed from the command line?

C:\BHUVAN>sample monday tuesday wednesday thursday

/* sample.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
while(--argc > 0)
printf("%s", *++argv);
return 0;
}

A.

sample monday tuesday wednesday thursday

B.

monday tuesday wednesday thursday

C.

monday tuesday thursday

D.

tuesday

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 269


14. If the following program (myproc.c) is present in the directory
"C:\TC" then what will be output of the program if run it from DOS shell?

/* myproc.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
printf("%s", argv[0]);
return 0;
}

A.

SAMPLE.C

B.

C:\TC\MYPROC.EXE

C.

C:\TC

D.

Error

Answer: Option B

Explanation:

In order to execute it from DOS shell, we have to run the created EXE
file by entering the exe file name as C:\TC>myproc <enter>.

270 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


15. What will be the output of the program (myprog.c) given below if it
is executed from the command line?

C:\BHUVAN>myprog one two three

/* myprog.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
int i;
for(i = 1; i < argc; i++)
printf("%c", argv[i][0]);
return 0;
}

A.

Oot

B.

ott

C.

nwh

D.

eoe

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 271


16. What will be the output of the program (sample.c) given below if it
is executed from the command line?

C:\BHUVAN>sample 1 2 3

C:\BHUVAN>sample 2 2 3

C:\BHUVAN>sample 3 2 3

/* sample.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
printf("%s\n", argv[0]);
return 0;
}

A.

sample 3 2 3

B.

sample 1 2 3

C.

sample

D.

Error

Answer: Option C

272 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


17. What will be the output of the program (myprog.c) given below if it
is executed from the command line?

C:\BHUVAN>myprog 1 2 3

/* myprog.c */

#include <stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)


{
int i, j = 0;
for(i = 0; i < argc; i++)
j = j + atoi(argv[i]);
printf("%d\n", j);
return 0;
}

A.

123

B.

C.

Error

D.

"123"

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 273


18. What will be the output of the program (sample.c) given below if it
is executed from the command line?

C:\BHUVAN>sample friday tuesday sunday

/* sample.c */

#include <stdio.h>

int main(int sizeofargv, char *argv[])


{
while(sizeofargv)
printf("%s", argv[--sizeofargv]);
return 0;
}

A.

sample friday tuesday sunday

B.

sample friday tuesday

C.

sunday tuesday friday sample

D.

sunday tuesday friday

Answer: Option C

274 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


19. What will be the output of the program (sample.c) given below if it
is executed from the command line?

C:\BHUVAN>sample friday tuesday sunday

/* sample.c */

#include <stdio.h>

int main(int argc, char *argv[])


{
printf("%c", *++argv[2] );
return 0;
}

A.

B.

C.

D.

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 275


20. What will be the output of the program (myprog.c) given below if it
is executed from the command line?

C:\BHUVAN>myprog 10 20 30

/* myprog.c */

#include <stdio.h>

int main(int argc, char **argv)


{
int i;
for(i = 0; i < argc; i++)
printf("%s\n", argv[i]);
return 0;
}

A.

10 20 30

B.

myprog 10 20

C.

myprog 10 20 30

D.

10 20

Answer: Option C

276 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


21. What will be the output of the program (myprog.c) given below if it
is executed from the command line?

C:\BHUVAN>myprog one two three

/* myprog.c */

#include <stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)


{
int i;
for(i = 1; i <= 3; i++)
printf("%u\n", &argv[i]);
return 0;
}

If the first value printed by the above program is 65517, what will be
the rest of output?

A.

65525 65531

B.

65519 65521

C.

65517 65517

D.

65521 65525

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 277


278 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Bitwise Operators

1. Assunming, integer is 2 byte, What will be the output of the


program?

#include <stdio.h>

int main()
{
printf("%x\n", -1 >> 1);
return 0;
}

A.

ffff

B.

0fff

C.

0000

D.

fff0

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 279


Explanation:

Negative numbers are treated with 2's complement method.

1's complement: Inverting the bits (all 1s to 0s and all 0s to 1s).

2's complement: Adding 1 to the result of 1's complement.

Binary of 1 (2 byte) : 0000 0000 0000 0001

Representing -1:

1s complement of 1 (2byte) : 1111 1111 1111 1110

Adding 1 to 1's comp. result : 1111 1111 1111 1111

Right shift 1 bit(-1 >> 1) : 1111 1111 1111 1111 (carry out 1)

Hexadecimal : f f f f

(Filled with 1s in the left side in the above step)

Note:

1. Fill with 1s in the left side for right shift for negative numbers.

2. Fill with 0s in the right side for left shift for negative numbers.

3. Fill with 0s in the left side for right shift for positive numbers.

4. Fill with 0s in the right side for left shift for positive numbers.

280 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


2. If an unsigned int is 2 bytes wide then, what will be the output of
the program?

#include <stdio.h>

int main()
{
unsigned int m = 32;
printf("%x\n", ~m);
return 0;
}

A.

ffff

B.

0000

C.

ffdf

D.

ddfd

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 281


3. Assuming a integer 2-bytes, What will be the output of the program?

#include <stdio.h>

int main()
{
printf("%x\n", -1 << 3);
return 0;
}

A.

ffff

B.

fff8

C.

D.

-1

Answer: Option B

282 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

The system will treat negative numbers in 2's complement method.

Example:

Assume the size of int is 2-bytes (16 bits). The integer value 1 is
represented as given below:

Binary of 1: 00000000 00000001 (this is for positive value of 1)

1's complement of binary 1: 11111111 11111110

2's complement of binary 1: 11111111 11111111

Thy system will store '11111111 11111111' in memory to represent '-1'.

If we do left shift (3 bits) on 11111111 11111111 it will become as given


below:

11111111 11111111 - (left shift 3 times) -> 11111111 11111000.

So, 11111111 11111000 - (binary to hex) -> FF F8. (Required Answer)

Note:

How is the negative number obtained from 2's complement value?

As stated above, -1 is represented as '11111111 11111111' in memory.

So, the system will take 2's complement of '11111111 11111111' to the get
the original negative value back.

Example:

Bit Representation of -1: 11111111 11111111

Since the left most bit is 1, it is a negative number. Then the value is

1's complement: 00000000 00000000

2's complement: 00000000 00000001 (Add 1 to the above result)

Therefore, '00000000 00000001' = 1 and the sign is negative.

Hence the value is -1.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 283


4. If an unsigned int is 2 bytes wide then, what will be the output of
the program?

#include <stdio.h>

int main()
{
unsigned int a = 0xffff;
~a;
printf("%x\n", a);
return 0;
}

A.

ffff

B.

0000

C.

00ff

D.

ddfd

Answer: Option A

284 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program?

#include <stdio.h>

int main()
{
unsigned char i = 0x80;
printf("%d\n", i << 1);
return 0;
}

A.

B.

256

C.

100

D.

80

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 285


6. What will be the output of the program?

#include <stdio.h>

int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
return 0;
}

A.

4 1 8 1

B.

4 >> 1 8 >> 1

C.

2 >> 4 Garbage value >> Garbage value

D.

2 4

Answer: Option C

286 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What will be the output of the program?

#include <stdio.h>

int main()
{
char c = 48;
int i, mask = 01;
for(i = 1; i <= 5; i++)
{
printf("%c", c | mask);
mask = mask << 1;
}
return 0;
}

A.

12400

B.

12480

C.

12500

D.

12556

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 287


8. What will be the output of the program?

#define P printf("%d\n", -1^~0);


#define M(P) int main()\
{\
P\
return 0;\
}
M(P)

A.

B.

C.

-1

D.

Answer: Option B

288 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


9. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 32, j = 0x20, k, l, m;
k = i | j;
l = i & j;
m = k ^ l;
printf("%d, %d, %d, %d, %d\n", i, j, k, l, m);
return 0;
}

A.

0, 0, 0, 0, 0

B.

0, 32, 32, 32, 32

C.

32, 32, 32, 32, 0

D.

32, 32, 32, 32, 32

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 289


10. What will be the output of the program?

#include <stdio.h>

int main()
{
printf("%d %d\n", 32 << 1, 32 << 0);
printf("%d %d\n", 32 << -1, 32 << -0);
printf("%d %d\n", 32 >> 1, 32 >> 0);
printf("%d %d\n", 32 >> -1, 32 >> -0);
return 0;
}

A.

Garbage values

B.

64 32
0 32
16 32
0 32

C.

All zeros

D.

8 0
0 0
32 0
0 16

Answer: Option B

290 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


11. What will be the output of the program?

#include <stdio.h>

int main()
{
unsigned int res;
res = (64 >> (2 + 1 - 2)) & (~(1 << 2));
printf("%d\n", res);
return 0;
}

A.

32

B.

64

C.

D.

128

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 291


12. What will be the output of the program?

#include <stdio.h>

int main()
{
int i = 4, j = 8;
printf("%d, %d, %d\n", i | j & j | i, i | j && j | i, i ^ j);
return 0;
}

A.

4, 8, 0

B.

1, 2, 1

C.

12, 1, 12

D.

0, 0, 0

Answer: Option C

292 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Typedef

1. What will be the output of the program?

#include <stdio.h>

int main()
{
enum color {red, green, blue};
typedef enum color mycolor;
mycolor m = red;
printf("%d", m);
return 0;
}

A.

B.

C.

D.

red

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 293


2. What will be the output of the program?

#include <stdio.h>

int main()
{
typedef int arr[5];
arr iarr = {1, 2, 3, 4, 5};
int i;
for(i = 0; i < 4; i++)
printf("%d,", iarr[i]);
return 0;
}

A.

1, 2, 3, 4

B.

1, 2, 3, 4, 5

C.

No output

D.

Error: Cannot use typedef with an array

Answer: Option A

294 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>

int main()
{
typedef int LONG;
LONG a = 4;
LONG b = 68;
float c = 0;
c = b;
b += a;
printf("%d, ", b);
printf("%f\n", c);
return 0;
}

A.

72, 68.000000

B.

72.000000, 68

C.

68.000000, 72.000000

D.

68, 72.000000

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 295


4. What will be the output of the program?

#include <stdio.h>

int main()
{
typedef float f;
static f *fptr;
float fval = 90;
fptr = &fval;
printf("%f\n", *fptr);
return 0;
}

A.

B.

C.

90.000000

D.

90

Answer: Option C

296 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program?

#include <stdio.h>

typedef struct error {int warning, err, exception;} ERROR;

int main()
{
ERROR e;
e.err = 1;
printf("%d\n", e.err);
return 0;
}

A.

B.

C.

D.

Error

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 297


298 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Const

1. What will be the output of the program?

#include <stdio.h>

int main()
{
int y = 128;
const int x = y;
printf("%d\n", x);
return 0;
}

A.

128

B.

Garbage value

C.

Error

D.

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 299


Explanation:

Step 1:

int y = 128;

The variable 'y' is declared as an integer type and initialized to value


"128".

Step 2:

const int x = y;

The constant variable 'x' is declared as an integer and it is initialized


with the variable 'y' value.

Step 3:

printf("%d\n", x);

It prints the value of variable 'x'.

Hence the output of the program is "128".

300 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


2. What will be the output of the program?

#include <stdio.h>
#include<stdlib.h>

union employee
{
char name[15];
int age;
float salary;
};

const union employee e1;

int main()
{
strcpy(e1.name, "K");
printf("%s %d %f", e1.name, e1.age, e1.salary);
return 0;
}

A.

Error: RValue required

B.

Error: cannot convert from 'const int *' to 'int *const'

C.

Error: LValue required in strcpy

D.

No error

Answer: Option D

Explanation:

The output will be (in 16-bit platform DOS):

K 75 0.000000

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 301


3. What will be the output of the program?

#include <stdio.h>

int fun(int **ptr);

int main()
{
int i = 10;
const int *ptr = &i;
fun(&ptr);
return 0;
}

int fun(int **ptr)


{
int j = 223;
int *temp = &j;
printf("Before changing ptr = %5x\n", *ptr);
const *ptr = temp;
printf("After changing ptr = %5x\n", *ptr);
return 0;
}

A.

Address of i
Address of j

B.

10
223

C.

Error: cannot convert parameter 1 from 'const int **' to 'int **'

D.

Garbage value

Answer & Explanation

Answer: Option C

302 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


4. What will be the output of the program?

#include <stdio.h>

int main()
{
const int x = 5;
const int *ptrx;
ptrx = &x;
*ptrx = 10;
printf("%d\n", x);
return 0;
}

A.

B.

10

C.

Error

D.

Garbage value

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 303


Explanation:

Step 1:

const int x = 5;

The constant variable x is declared as an integer data type and


initialized with value '5'.

Step 2:

const int *ptrx;

The constant variable ptrx is declared as an integer pointer.

Step 3:

ptrx = &x;

The address of the constant variable x is assigned to integer pointer


variable ptrx.

Step 4:

*ptrx = 10;

Here we are indirectly trying to change the value of the constant vaiable
x. This will result in an error.

To change the value of const variable x we have to use *(int *)&x = 10;

304 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program in TurboC?

#include <stdio.h>

int fun(int **ptr);

int main()
{
int i=10, j=20;
const int *ptr = &i;
printf("i = %5X ", ptr);
printf("ptr = %d ", *ptr);
ptr = &j;
printf("j = %5X ", ptr);
printf("ptr = %d", *ptr);
return 0;
}

A.

i= FFE2 ptr=12 j=FFE4 ptr=24

B.

i= FFE4 ptr=10 j=FFE2 ptr=20

C.

i= FFE0 ptr=20 j=FFE1 ptr=30

D.

Garbage value

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 305


6. What will be the output of the program?

#include <stdio.h>

int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", *s++);

return 0;
}

A.

Error

B.

C.

Hello

D.

Hel

Answer: Option C

306 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

const char *s = "";

The constant variable s is declared as an pointer to an array of


characters type and initialized with an empty string.

Step 2:

char str[] = "Hello";

The variable str is declared as an array of charactrers type and


initialized with a string "Hello".

Step 3:

s = str;

The value of the variable str is assigned to the variable s. Thereforestr


contains the text "Hello".

Step 4:

while(*s)
{
printf("%c", *s++);
}

Here the while loop got executed untill the value of the variable s is
available and it prints the each character of the variable s.

Hence the output of the program is "Hello".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 307


7. What will be the output of the program?

#include <stdio.h>

int get();

int main()
{
const int x = get();
printf("%d", x);
return 0;
}

int get()
{
return 20;
}

A.

Garbage value

B.

Error

C.

20

D.

Answer: Option C

308 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

int get();

This is the function prototype for the funtion get(), it tells the
compiler returns an integer value and accept no parameters.

Step 2:

const int x = get();

The constant variable x is declared as an integer data type and


initialized with the value "20".

The function get() returns the value "20".

Step 3:

printf("%d", x);

It prints the value of the variable x.

Hence the output of the program is "20".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 309


8. What will be the output of the program (in Turbo C)?

#include <stdio.h>

int fun(int *f)


{
*f = 10;
return 0;
}

int main()
{
const int arr[5] = {1, 2, 3, 4, 5};
printf("Before modification arr[3] = %d", arr[3]);
fun(&arr[3]);
printf("\nAfter modification arr[3] = %d", arr[3]);
return 0;
}

A.
Before modification arr[3] = 4
After modification arr[3] = 10

B.
Error: cannot convert parameter 1 from const int * to int *

C.
Error: Invalid parameter

D.
Before modification arr[3] = 4
After modification arr[3] = 4

Answer: Option A

310 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Step 1:

const int arr[5] = {1, 2, 3, 4, 5};

The constant variable arr is declared as an integer array and initialized


to arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5.

Step 2:

printf("Before modification arr[3] = %d", arr[3]);

It prints the value of arr[3] (ie. 4).

Step 3:

fun(&arr[3]);

The memory location of the arr[3] is passed to fun() andarr[3] value is


modified to 10.

A const variable can be indirectly modified by a pointer.

Step 4:

printf("After modification arr[3] = %d", arr[3]);

It prints the value of arr[3] (ie. 10).

Hence the output of the program is:

Before modification arr[3] = 4


After modification arr[3] = 10

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 311


9. What will be the output of the program?

#include <stdio.h>

int main()
{
const int i = 0;
printf("%d\n", i++);
return 0;
}

A.

10

B.

11

C.

No output

D.

Error: ++needs a value

Answer: Option D

Explanation:

This program will show an error "Cannot modify a const object".

Step 1:

const int i = 0;

The constant variable 'i' is declared as an integer and initialized with


value of '0'(zero).

Step 2:

printf("%d\n", i++);

Here the variable 'i' is increemented by 1 (one). This will create an


error "Cannot modify a const object".

Because, we cannot modify a const variable.

312 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


10. What will be the output of the program?

#include <stdio.h>
int main()
{
const c = -11;
const int d = 34;
printf("%d, %d\n", c, d);
return 0;
}

A.

Error

B.

-11, 34

C.

11, 34

D.

None of these

Answer: Option B

Explanation:

Step 1:

const c = -11;

The constant variable 'c' is declared and initialized to value "-11".

Step 2:

const int d = 34;

The constant variable 'd' is declared as an integer and initialized to


value '34'.

Step 3:

printf("%d, %d\n", c, d);

The value of the variable 'c' and 'd' are printed.

Hence the output of the program is -11, 34.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 313


314 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Memory Allocation

1. What will be the output of the program?

#include <stdio.h>
#include<stdlib.h>

int main()
{
int *p;
p = (int *)malloc(20); /* Assume p has address of 1314 */
free(p);
printf("%u", p);
return 0;
}

A.

1314

B.

Garbage value

C.

1316

D.

Random address

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 315


2. What will be the output of the program (16-bit platform)?

#include <stdio.h>
#include<stdlib.h>

int main()
{
int *p;
p = (int *)malloc(20);
printf("%d\n", sizeof(p));
free(p);
return 0;
}

A.

B.

C.

D.

Garbage value

Answer: Option B

316 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
char *s;
char *fun();
s = fun();
printf("%s\n", s);
return 0;
}

char *fun()
{
char buffer[30];
strcpy(buffer, "RAM");
return (buffer);
}

A.

0xffff

B.

Garbage value

C.

0xffee

D.

Error

Answer: Option B

Explanation:

The output is unpredictable since buffer is an auto array and will die
when the control go back to main. Thus s will be pointing to an array,
which not exists.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 317


4. What will be the output of the program?

#include <stdio.h>
#include<stdlib.h>

int main()
{
union test
{
int i;
float f;
char c;
};
union test *t;
t = (union test *)malloc(sizeof(union test));
t->f = 10.10f;
printf("%f", t->f);
return 0;
}

A.

10

B.

Garbage value

C.

10.100000

D.

Error

Answer: Option C

318 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. Assume integer is 2 bytes wide. How many bytes will be allocated
for the following code?

#include <stdio.h>
#include<stdlib.h>

#define MAXROW 3
#define MAXCOL 4

int main()
{
int (*p)[MAXCOL];
p = (int (*)[MAXCOL])malloc(MAXROW *sizeof(*p));
return 0;
}

A.

56 bytes

B.

128 bytes

C.

24 bytes

D.

12 bytes

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 319


6. Assume integer is 2 bytes wide. What will be the output of the
following code?

#include <stdio.h>
#include<stdlib.h>

#define MAXROW 3
#define MAXCOL 4

int main()
{
int (*p)[MAXCOL];
p = (int (*)[MAXCOL])malloc(MAXROW *sizeof(*p));
printf("%d, %d\n", sizeof(p), sizeof(*p));
return 0;
}

A.

2, 8

B.

4, 16

C.

8, 24

D.

16, 32

Answer: Option A

320 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. How many bytes of memory will the following code reserve?

#include <stdio.h>
#include<stdlib.h>

int main()
{
int *p;
p = (int *)malloc(256 * 256);
if(p == NULL)
printf("Allocation failed");
return 0;
}

A.

65536

B.

Allocation failed

C.

Error

D.

No output

Answer: Option B

Explanation:

Hence 256 * 256 = 65536 is passed to malloc() function which can allocate
upto 65535. So the memory allocation will be failed in 16 bit platform
(Turbo C in DOS).

If you compile the same program in 32 bit platform like Linux (GCC
Compiler) it may allocate the required memory.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 321


322 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Variable Number of Arguments

1. What will be the output of the program?

#include <stdio.h>
#include<stdarg.h>

void fun(char *msg, ...);

int main()
{
fun("IndiaREC", 1, 4, 7, 11, 0);
return 0;
}

void fun(char *msg, ...)


{
va_list ptr;
int num;
va_start(ptr, msg);
num = va_arg(ptr, int);
num = va_arg(ptr, int);
printf("%d", num);
}

A.

IndiaREC 1 7 11 0

B.

C.

D.

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 323


2. What will be the output of the program?

#include <stdio.h>
#include<stdarg.h>
void fun1(char, int, int *, float *, char *);
void fun2(char ch, ...);
void (*p1)(char, int, int *, float *, char *);
void (*p2)(char ch, ...);
int main()
{
char ch='A'; int i=10;
float f=3.14; char *p="Hello";
p1=fun1;
p2=fun2;
(*p1)(ch, i, &i, &f, p);
(*p2)(ch, i, &i, &f, p);
return 0;
}
void fun1(char ch, int i, int *pi, float *pf, char *p)
{
printf("%c %d %d %f %s \n", ch, i, *pi, *pf, p);
}
void fun2(char ch, ...)
{
int i, *pi; float *pf; char *p;
va_list list;
printf("%c ", ch);
va_start(list, ch);
i = va_arg(list, int);
printf("%d ", i);

pi = va_arg(list, int*);
printf("%d ", *pi);
pf = va_arg(list, float*);
printf("%f ", *pf);
p = va_arg(list, char *);
printf("%s", p);
}

A.
A 10 3.14
A 10 3.14
B.
A 10 10 3.140000 Hello
A 10 10 3.140000 Hello
C.
A 10 Hello
A 10 Hello
D.
Error
Answer: Option B

324 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>
#include<stdarg.h>

void dumplist(int, ...);

int main()
{
dumplist(2, 4, 8);
dumplist(3, 6, 9, 7);
return 0;
}

void dumplist(int n, ...)


{
va_list p; int i;
va_start(p, n);

while(n-->0)
{
i = va_arg(p, int);
printf("%d ", i);
}
va_end(p);
printf("\n");
}

A.

2 4
3 6

B.

2 4 8
3, 6, 9, 7

C.

4 8
6 9 7

D.

1 1 1
1 1 1 1

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 325


4. What will be the output of the program?

#include <stdio.h>
#include<stdarg.h>
void display(int num, ...);

int main()
{
display(4, 'A', 'B', 'C', 'D');
return 0;
}
void display(int num, ...)
{
char c, c1; int j;
va_list ptr, ptr1;
va_start(ptr, num);
va_start(ptr1, num);
for(j=1; j<=num; j++)
{
c = va_arg(ptr, int);
printf("%c, ", c);
c1 = va_arg(ptr1, int);
printf("%d\n", c1);
}
}

A.
A, A
B, B
C, C
D, D

B.
A, a
B, b
C, c
D, d

C.
A, 65
B, 66
C, 67
D, 68

D.
A, 0
B, 0
C, 0
C, 0

Answer: Option C

326 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program?

#include <stdio.h>
#include<stdarg.h>

void fun1(int num, ...);


void fun2(int num, ...);

int main()
{
fun1(1, "Apple", "Boys", "Cats", "Dogs");
fun2(2, 12, 13, 14);
return 0;
}
void fun1(int num, ...)
{
char *str;
va_list ptr;
va_start(ptr, num);
str = va_arg(ptr, char *);
printf("%s ", str);
}
void fun2(int num, ...)
{
va_list ptr;
va_start(ptr, num);
num = va_arg(ptr, int);
printf("%d", num);
}

A.

Dogs 12

B.

Cats 14

C.

Boys 13

D.

Apple 12

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 327


328 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Complicated Declarations

1. What will be the output of the program?

#include <stdio.h>

int main()
{
char far *near *ptr1;
char far *far *ptr2;
char far *huge *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
return 0;
}

A.

4, 4, 8

B.

4, 4, 4

C.

2, 4, 4

D.

2, 4, 8

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 329


2. What will be the output of the program in DOS (Compiler - Turbo C)?

#include <stdio.h>

double i;

int main()
{
(int)(float)(char) i;
printf("%d", sizeof((int)(float)(char)i));
return 0;
}

A.

B.

C.

D.

Answer: Option B

Explanation:

Due to the C language is being platform dependent:

In Turbo C (DOS - 16 bit platform), the output will be 2.

But in GCC (Unix/Linux - 32 bit platform), the output will be 4.

330 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


3. What will be the output of the program?

#include <stdio.h>

int main()
{
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
printf("%d, %d, %d\n", sizeof(**ptr1), sizeof(ptr2), sizeof(*ptr3));
return 0;
}

A.

4, 4, 4

B.

2, 2, 2

C.

2, 8, 4

D.

2, 4, 8

Answer: Option A

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 331


4. What will be the output of the program (in Turbo C under DOS)?

#include <stdio.h>

int main()
{
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
return 0;
}

A.

4, 4, 8

B.

2, 4, 4

C.

4, 4, 2

D.

2, 4, 8

Answer: Option C

332 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


5. What will be the output of the program?

#include <stdio.h>

typedef void v;
typedef int i;

int main()
{
v fun(i, i);
fun(2, 3);
return 0;
}

v fun(i a, i b)
{
i s = 2;
float i;
printf("%d,", sizeof(i));
printf(" %d", a * b * s);
}

A.

2, 8

B.

4, 8

C.

2, 4

D.

4, 12

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 333


6. What will be the output of the program?

#include <stdio.h>

int main()
{
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(*ptr2), sizeof(**ptr3));
return 0;
}

A.

4, 4, 4

B.

2, 4, 4

C.

4, 4, 2

D.

2, 4, 8

Answer: Option A

334 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What will be the output of the program?

#include <stdio.h>
typedef unsigned long int uli;
typedef uli u;

int main()
{
uli a;
u b = -1;
a = -1;
printf("%lu, %lu", a, b);
return 0;
}

A.

4343445454, 4343445454

B.

4545455434, 4545455434

C.

4294967295, 4294967295

D.

Garbage values

Answer: Option C

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 335


Explanation:

The system will treat the negative numbers with 2's complement method.

For 'long int' system will occupy 4 bytes (32 bits).

Therefore,

Binary 1 : 00000000 00000000 00000000 00000001

To represent -1, system uses the 2's complement value of 1. Add 1 to the
1's complement result to obtain 2's complement of 1.

So, first take 1's complement of binary 1 (change all 0s to 1s and all 1s
to 0s)

1's complement of Binary 1:

11111111 11111111 11111111 11111110

2's complement of Binary 1: (Add 1 with the above result)

11111111 11111111 11111111 11111111

In HexaDecimal

11111111 11111111 11111111 11111111 = FFFF FFFF FFFF FFFF

In Unsigned Integer

11111111 11111111 11111111 11111111 = 4294967295.

336 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


8. What will be the output of the program in DOS (Compiler - Turbo C)?

#include <stdio.h>

double i;

int main()
{
(int)(float)(char) i;
printf("%d", sizeof(i));
return 0;
}

A.

B.

C.

16

D.

22

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 337


9. What will be the output of the program under DOS?

#include <stdio.h>

int main()
{
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(**ptr2), sizeof(ptr3));
return 0;
}

A.

4, 4, 4

B.

4, 2, 2

C.

2, 8, 4

D.

2, 4, 8

Answer: Option B

338 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


10. What will be the output of the program?

#include <stdio.h>

int main()
{
struct s1
{
char *z;
int i;
struct s1 *p;
};
static struct s1 a[] = {{"Nagpur", 1, a+1} ,
{"Chennai", 2, a+2} ,
{"Bangalore", 3, a} };
struct s1 *ptr = a;
printf("%s,", ++(ptr->z));
printf(" %s,", a[(++ptr)->i].z);
printf(" %s", a[--(ptr->p->i)].z);
return 0;
}

A.

Nagpur, Chennai, Bangalore

B.

agpur, hennai, angalore

C.

agpur, Chennai, angalore

D.

agpur, Bangalore, Bangalore

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 339


11. What will be the output of the program?

#include <stdio.h>

int main()
{
char huge *near *ptr1;
char huge *far *ptr2;
char huge *huge *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
return 0;
}

A.

4, 4, 8

B.

2, 4, 4

C.

4, 4, 2

D.

2, 4, 8

Answer: Option B

340 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


12. What will be the output of the program in Turbo C?

#include <stdio.h>

int main()
{
char near *near *ptr1;
char near *far *ptr2;
char near *huge *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
return 0;
}

A.

4, 4, 8

B.

4, 4, 4

C.

2, 4, 8

D.

2, 4, 4

Answer: Option D

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 341


342 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai
Library Functions

1. What will be the output of the program?

#include <stdio.h>
int main()
{
int i;
i = printf("How r u\n");
i = printf("%d\n", i);
printf("%d\n", i);
return 0;
}

A.

How r u
7
2

B.

How r u
8
2

C.

How r u
1
1

D.

Error: cannot assign printf to variable

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 343


Explanation:

In the program, printf() returns the number of charecters printed on the


console

i = printf("How r u\n");

This line prints "How r u" with a new line character and returns the
length of string printed then assign it to variable i.

So i = 8 (length of '\n' is 1).

i = printf("%d\n", i);

In the previous step the value of i is 8. So it prints "8" with a new


line character and returns the length of string printed then assign it to
variable i. So i = 2 (length of '\n' is 1).

printf("%d\n", i);

In the previous step the value of i is 2. So it prints "2".

344 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


2. What will be the output of the program?

#include <stdio.h>
#include<math.h>

int main()
{
float i = 2.5;
printf("%f, %d", floor(i), ceil(i));
return 0;
}

A.

2, 3

B.

2.000000, 3

C.

2.000000, 0

D.

2, 0

Answer: Option C

Explanation:

Both ceil() and floor() return the integer found as a double.

floor(2.5) returns the largest integral value (round down) that is not
greater than 2.5. So output is 2.000000.

ceil(2.5) returns 3, while converting the double to int it returns '0'.


So, the output is '2.000000, 0'.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 345


3. What will be the output of the program?

#include <stdio.h>

int main()
{
int i;
i = scanf("%d %d", &i, &i);
printf("%d\n", i);
return 0;
}

A.

B.

C.

Garbage value

D.

Error: cannot assign scanf to variable

Answer: Option B

Explanation:

scanf() returns the number of variables to which you are provding the
input.

i = scanf("%d %d", &i, &i);

Here scanf() returns 2. So i = 2.

printf("%d\n", i); Here it prints 2.

346 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


4. What will be the output of the program?

#include <stdio.h>

int main()
{
int i;
char c;
for(i = 1; i <= 5; i++)
{
scanf("%c", &c); /* given input is 'b' */
ungetc(c, stdout);
printf("%c", c);
ungetc(c, stdin);
}
return 0;
}

A.

bbbb

B.

bbbbb

C.

D.

Error in ungetc statement.

Answer: Option C

Explanation:

The ungetc() function pushes the character c back onto the named input
stream, which must be open for reading.

This character will be returned on the next call to getc or fread for
that stream.

One character can be pushed back in all situations.

A second call to ungetc without a call to getc will force the previous
character to be forgotten.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 347


5. What will be the output of the program?

#include <stdio.h>
#include<stdlib.h>

int main()
{
char *i = "55.555";
int result1 = 10;
float result2 = 11.111;
result1 = result1 + atoi(i);
result2 = result2 + atof(i);
printf("%d, %f", result1, result2);
return 0;
}

A.

55, 55.555

B.

66, 66.666600

C.

65, 66.666000

D.

55, 55

Answer: Option C

348 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


Explanation:

Function atoi() converts the string to integer.

Function atof() converts the string to float.

result1 = result1 + atoi(i);

Here result1 = 10 + atoi(55.555);

result1 = 10 + 55;

result1 = 65;

result2 = result2+atof(i);

Here result2 = 11.111 + atof(55.555);

result2 = 11.111 + 55.555000;

result2 = 66.666000;

So the output is "65, 66.666000".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 349


6. What will be the output of the program?

#include <stdio.h>
#include<string.h>

int main()
{
char dest[] = {97, 97, 0};
char src[] = "aaa";
int i;
if((i = memcmp(dest, src, 2)) == 0)
printf("Got it");
else
printf("Missed");
return 0;
}

A.

Missed

B.

Got it

C.

Error in memcmp statement

D.

None of above

Answer: Option B

Explanation:

memcmp compares the first 2 bytes of the blocks dest and src as unsigned
chars. So, the ASCII value of 97 is 'a'.

if((i = memcmp(dest, src, 2)) == 0)

When comparing the array dest and src as unsigned chars, the first 2
bytes are same in both variables.so memcmp returns '0'.

Then, the if(0 = 0) condition is satisfied. Hence the output is "Got it".

350 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


7. What will function gcvt() do?

A.

Convert vector to integer value

B.

Convert floating-point number to a string

C.

Convert 2D array in to 1D array.

D.

Covert multi Dimensional array to 1D array

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 351


Explanation:

The gcvt() function converts a floating-point number to a string. It


converts given value to a null-terminated string.

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
char str[25];
double num;
int sig = 5; /* significant digits */

/* a regular number */
num = 9.876;
gcvt(num, sig, str);
printf("string = %s\n", str);

/* a negative number */
num = -123.4567;
gcvt(num, sig, str);
printf("string = %s\n", str);

/* scientific notation */
num = 0.678e5;
gcvt(num, sig, str);
printf("string = %s\n", str);

return(0);
}

Output:

string = 9.876
string = -123.46
string = 67800

352 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


8. What will be the output of the program?

#include <stdio.h>

int main()
{
int i;
char c;
for(i = 1; i <= 5; i++)
{
scanf("%c", &c); /* given input is 'a' */
printf("%c", c);
ungetc(c, stdin);
}
return 0;
}

A.

aaaa

B.

aaaaa

C.

Garbage value.

D.

Error in ungetc statement.

Answer: Option B

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 353


Explanation:

for(i = 1; i <= 5; i++) Here the for loop runs 5 times.

Loop 1:

scanf("%c", &c);

Here we give 'a' as input.

printf("%c", c);

prints the character 'a' which is given in the previous "scanf()"


statement.

ungetc(c, stdin);

"ungetc()" function pushes character 'a' back into input stream.

Loop 2:

Here the

scanf("%c", &c);

get the input from "stdin" because of "ungetc" function.

printf("%c", c);

Now variable c = 'a'. So it prints the character 'a'.

ungetc(c, stdin);

"ungetc()" function pushes character 'a' back into input stream.

This above process will be repeated in Loop 3, Loop 4, Loop 5.

354 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai


B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 355
356 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai

Você também pode gostar