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)

85 comments:

IAS-IFS 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????

Priyanka kumari 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

Amardeep Kumar 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

Priyanka kumari 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

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

Priyanka kumari 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

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.

satyaprakash vaag(knr) said...

ankita nice explanation.
thanks a lot.

Anonymous said...

plz explain the answers......

Amrita said...

Excellent questions.We get to know the facts that we cannot find in any academic books.
My question is,two variables of same structure type can be copied from one to other then why is the answer to Question-10 is "Compile error"?
Please explain.

Santosh Maurya said...

good question . and it is very helpful for any interview

Anonymous said...

printf(char *format,...)
~char *format="%d"+1
"%d"=string constant have three chars '%','d','\0',always give an base address
base address+1 move to location where 'd' from 'd' to print until get null char '\0' which
is next to it,so
output:just d;
"%d"+2:no char means null char output
"%dd"+2:d but p("%d%d"+2,a):null(we cant see)5(2nd arg) because conversion specification begins with a %
p("%de %d"+2,a):e5,p("%d e %d"+2,a):(space)e 5

Anonymous said...

if we are not provide any arg to printf checks on stack(where all initialized local variables, formal args of functions,return addr are store)
first come(in declaration) has a->b->c,but stack in LIFO(last in first out)
so 5->6->11 comes as last in 11 first out next last in 6 first out and so on
means 11->6->5

Unknown said...

u r awesome !!!!!!!!!!!!!

Unknown said...

It is completely compiler dependent so taking answer based over stack is illogical so always answer it garbage collection that obviously come out on standard compilers(Dev CPP or CodeBlocks... etc) except Turbo C which remains outdated.

Unknown said...

@Amrita
Simply we cannot initialize any variable inside structure, there is no problem in coping structure as struct game g1=g3, perfectly correct problem is with g2={"anil"};. Just remove it, will work fine.
Enjoy Have Fun

Hriday Kumar Gupta said...

Good answer.....

And due to , operator next argument (i.e a) will be ignored by compiler.

Unknown said...

u are too good for highly advanced.......

Anonymous said...

It's too helpful ...
gives deep knowledge..

Anonymous said...

a2zofc@gmail.com

Many questions are compiler dependent. Please run them before forming an opinion about it. What is the best way to identify if the question asked is by a professional and is logical ( or is asked by an amateur and is irrational) ?

Answer: An Indian student will use turbo C / getch / conio.h while the world continue to grow/ improve learn in-depth.

Anonymous said...

excellent questions ,very helpful for understanding c better and deeply

Anonymous said...

can you tell me a c program which will tell no. of odd digits and even digits in a given number?

Anonymous said...

Enter length:5
Enter width:5

area is 25

can u give me the code?

Unknown said...

could u pls explain the 5)&6)printf statements

Anonymous said...

i dont understand 10th question..can you please explain..

NagadineshK said...

can you explain question 5

Pankaj Tirpude said...

but how "%d"+1="d"
....please explain

Unknown said...

Nice questions...thanks to all for telling the solutions....

Anonymous said...

can anyone tell me solution of ques. 1

Anonymous said...

here %d is considerd as a string literal and 1 is the susbscipt in the literal..so we get d
if we have anything char follwing it it wll also b printed,..

Unknown said...

hi Manoj and Hriday...answer of this question is varies from compiler to compiler.As am i write then in Gcc compiler it gives 500 answer means
first division takes place den multiplication..have a nice day

Anonymous said...

Awesome.......!!!!!!!!!!!!!!!!!!!!

Unknown said...

Please keep explanations freshers are not getting only with the answers; you have to keep the light explanation in order to go through it clearly please

Unknown said...

can anyone give the explanation for the 10th question??

Unknown said...

nice explnation...thanks

Stephen said...

can anyone explain the logic behind fourth problem

Anonymous said...

in gcc int range is 4 bytes by default so 250000 is in the range of int. therefore answer = 500
gcc also follows (L->R) left to right associativity for these operators

Anonymous said...

1 q ans is wrong

Joginder Singh said...

yea it's depend on compiler , in cpp file pick up a garbage value.

Unknown said...

why questiono. 4 gives compilation error

unknown said...

printf("%de %d"+2)output:e 0 and for printf("%de %d"+2,a)output:e 5.explain?

unknown said...

how do we get o/p for 500*500 as -12144

Koushik Ahmed said...

thanks@Anonymous

Unknown said...

can anyone explain 6th question

Unknown said...

printf("%d"+1,a); printf("%d k"+3,a); between these two no difference is there. if you give 1 its print 1st offset 'd' & if you give 3 its print 3rd offset 'k'.

printf("%d k",+3); //But here the +3 is after the comma operator. So "%d" takes the +3 as argument and prints it.

Unknown said...
This comment has been removed by the author.
Unknown said...

#include
main()
{
int a, b, c;
a = 10;
b = 20;
c = printf("%d",a)+ ++b;
printf ("%d",c);
}

Unknown said...

answer: 1023. explain it, ...............please.

Unknown said...

First question: it is printed using stack operation. So it prints from right to left and we get 11 6 5

Unknown said...

here a=10(two digit number)...(%d,a)+ ++b gives add 2 digits to incremented b value thai is 21+2digits=23....( but b value is 21 only).

ex1: a= 100(three digits)...(%d,a)+ ++b gives add 3 digits to incremented b value that is ....21+3=25.....the ans is 1025.....
ex2: a=10(two digits) ...(%d\n,a)+ ++b gives add 2 digit + one digit for \n total of three digits.....the ans gives 1024.

Unknown said...

regarding to gcc compiler

Unknown said...

%d is string and 1 is subscript so answer is obviously d

Unknown said...

i think itz simply BODMAS

Unknown said...

can u explain how %p and%fp works.!!

Unknown said...

hi Surender Naregudam ,printf returns the number of characters s0c = printf("%d",a)+ ++b; in this case a has 2 numbers it returns 2 and we are incrementing b by 1 and adding with 2 gives you three.Therefore when you print c it gives 23.

Unknown said...
This comment has been removed by the author.
Unknown said...

For question no. 4:
when executed the "%d", then "%d" is reserved in heap and allocates total 3 bytes(2 for '%' and 'd' char, 1 for '\0' char) and then returns the address of the first byte (i.e. address of the '%' char), but we know if we increase a char * type pointer then it points the next byte, so now for ("%d" + 1), the returned value will be the address of 'd' char (byte). so the printf function will get the address of 'd' char, not the '%', so it will read it as "d" only.
but in "d" string, there are no format specifier, so, it will just print "d".
It is behind the scene. :D

Shinde Balaji said...

Can anyone give explanation of 5th question

Unknown said...

For fifth question:
%p and %Fp print out a pointer, specifically the address to which the pointer refers, and since it is printing out a part of your computer's architecture, it does so in Hexadecimal.
Simply %p is for printing a pointer address.

%Fp is not for pointer, depending on compilers. For gcc/clang, %F is for float, similar to %f with a little different. The following p will be treated as a normal char to print.

Unknown said...

For fifth question:
%p and %Fp print out a pointer, specifically the address to which the pointer refers, and since it is printing out a part of your computer's architecture, it does so in Hexadecimal.
Simply %p is for printing a pointer address.

%Fp is not for pointer, depending on compilers. For gcc/clang, %F is for float, similar to %f with a little different. The following p will be treated as a normal char to print.

Unknown said...

Please give explanation to the answers sir....

Unknown said...

Please give explanation to the answers sir....