Printf function questions and answer with solution

Printf objective types interview questions and answers  


(1)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=6,c=11;
clrscr();
printf("%d %d %d");
getch();
}

What will output when you compile and run the above code?

(a)Garbage value garbage value garbage value
(b)5 6 11
(c)11 6 5
(d)Compiler error

Answer: (c)
(2)

#include<stdio.h>

void main()
{
char *str="CQUESTIONBANK";
clrscr();
printf(str+9);
getch();
}

What will output when you compile and run the above code?

(a)CQESTIONBANK
(b)CQUESTION
(c)BANK
(d)Compiler error


Answer: (c)
(3)

#include<stdio.h>
void main()
{
clrscr();
printf("%d",printf("CQUESTIONBANK"));
getch();
}

What will output when you compile and run the above code?

(a)13CQUESTIONBANK
(b)CQUESTIONBANK13
(c)Garbage CQUESTIONBANK
(d)Compiler error


Answer: (b)

(4)
#include<stdio.h>
#include<conio.h>
void main()
{
short int a=5;
clrscr();
printf("%d"+1,a);
getch();
}

What will output when you compile and run the above code?
(a)6
(b)51
(c)d
(d)Compiler error


Answer: (c)

(5)
#include<stdio.h>
void main()
{
int i=85;
clrscr();
printf("%p %Fp",i,i);
getch();
}

What will output when you compile and run the above code?

(a)85 85
(b)0055 034E:0055
(c)0055 FFFF:0055
(d)Compiler error


Answer: (b)
 (6)
#include<stdio.h>
static struct student
{
int a;
    int b;
    int c;
int d;
}s1={6,7,8,9},s2={4,3,2,1},s3;
void main()
{
s3=s1+s2;
clrscr();
printf("%d %d %d %d",s3.a,s3.b,s3.c,s3.d);
getch();
}
What will output when you compile and run the above code?
(a)6789
(b)4321
(c)10101010
(d)Compiler error


Answer: (d)

(7)
#include<stdio.h>
extern struct student
{
int a;
    int b;
    int c;
int d;
}s={6,7,8,9};

void main()
{
clrscr();
printf("%d %d %d %d",s.a,s.b,s.c,s.d);
getch();
}

What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error


Answer: (a)

(8)
#include<stdio.h>
struct student
{
static int a;
register int b;
auto int c;
extern int d;
}s={6,7,8,9};
void main()
{
printf("%d %d % %d",s.a,s.b,s.c,s.d);
}

What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error


Answer: (d)

(9)
#include<stdio.h>
struct student
{
int roll;
int cgpa;
int sgpa[8];
};
void main()
{
struct student s={12,8,7,2,5,9};
int *ptr;
ptr=(int *)&s;
clrscr();
printf("%d",*(ptr+3));
getch();
}
What will output when you compile and run the above code?
(a)8
(b)7
(c)2
(d)Compiler error


Answer: (c)
(10)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2={"anil"};
}g3={10,200};
void main()
{
struct game g1=g3;
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}

What will output when you compile and run the above code?
(a)10 200 anil
(b)200 10 anil
(c)10 200 null
(d)Compiler error


Answer: (d)

(11)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2;
}g1;
void main()
{
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}

What will output when you compile and run the above code?
(a)Garbage_value garbage_value garbage_value
(b)0 0 (null)
(c)Run time error
(d)Compiler error

Answer: (b)

32 comments:

vipnip said...

q 4 ans is correct but please explain the mechanism i.e how we are getting always d whether use 5,6 or any thing????

Ritesh kumar said...

Hi Vipnip,

Yes, answer of question no. 4 will always d
As we know
"%d" + 1 = "d"
So second parameter in printf statement i.e. a is meaning less.

Note: Comma is also operator in c language

Anonymous said...

what is the reason behind the answer of question1?can any one help me

Dino said...

pls give explanation of ques. no. 1

karthick said...

for question 1,as the values 5,6 ,11 will be stored in stack(Last In First Out),first %d will take the top value in stack hence 11 similarly 2nd and third will take the successive values.

Anonymous said...

Hi friends....please anyone give explanation for the 5th question

karthick said...

Hi friends....please anyone give explanation for the 5th question

Anonymous said...

nice question posted can u explain the solutions for question 2,3,4
thanks in advance

Amar Gupta said...

nice collection

Anonymous said...

hii anyone give explanation of question 5

Varun Ramesh said...

any one please give clear explanation
how "%d" + 1 = "d"

and for the 5th question

anjali said...

%p and %Fp stands for what????

anjali said...

plz someone explain qus 4& 5 in detail

Anonymous said...

pls give the code for declaring,getting,adding and printing a number in a single statement.

Anonymous said...

any one explanation of 11 question

Ritesh kumar said...

Default value of extern variable of type numeric is zero and non-numeric type is null.

kavitha said...

4th question output is different so plz explain

Anonymous said...

explain the working of ques.5

Anonymous said...

Can anybody explain why value of "d"+1=d??

Anonymous said...

gud effort for begineers to increase efficiency

Anonymous said...

hi ritesh.
but answer of question no 4 is "a".and it always remain "a" when i we replace "a" by any letter

manoj kumar said...

anyone can tell the logic of the question no 1 and 3

manoj kumar said...

what is the logic behind of this program
#include
main()
{
nt a=1;
a=500*500/500;
printf("%d",a);
}

aparna said...

in 3rd ques it prints the string and the length of the string

Anonymous said...

5 th question +1 will truncate the first character in %d hence it is left with printf("d",a) as there are no format specifiers it prints d;

Anonymous said...

%p -normal pointer having size of 2 bytes. 85 is converted into hexadecimal i.e 0055
%Fp -Far Pointer 4 byte 85 is converted into hexadecimal but with base:Offset

Anonymous said...

can any one tell how q.no 4 is executed

Ritesh kumar said...

As we know
"%d" + 1 = "d"
So second parameter in printf statement i.e. a is meaning less.

Hriday Kumar Gupta said...

a=500*500/500

As u know in the "precedence operator and associativity table for C language" both * and / operator comes at Precedence level 3 and associativity is left to right.
it means * and / have same level so we examine their associativity i.e left to right .

so in given expression move left to right and calculate 500*500 that is 250000 but this value is out of range of an int(-32768 to +32767) so 250000 will be circulated and only result of 500*500 will give output as -12144, now divide this value with 500 according to given expression (-12144/500) so final result will be -24 (only quotient 24 bcos of divide operator).

sanjeev said...

its very interesting to note d following:
int a=5;// take anything
1) printf("%d"+0,a);
2) printf("%d"+0);
3) printf("%d"+1,a);
4) printf("%d"+1);
5) printf("%d k"+3,a);
6) printf("%d k",+3);
likewise check d output n c d magic... :D

dharmendra said...

answer of first question is compiler error........

Ankita said...

printf(5+"hellothere");
this means that skip 5 charachters and print the rest. so printf("%d"+1) is d as it skips 1 charachter that is % hre and pints the rest.

C compiler: gcc 4.1.2