0% found this document useful (0 votes)
596 views8 pages

Playing With 'Printf ' Statement

The document contains a series of questions and answers about using printf statements in C programming. It provides examples of code snippets using printf and analyzes the expected output. The document assumes a DOS environment using Turbo C/C++ compiler. It covers various aspects of printf like format specifiers, escape sequences, argument order evaluation, macros, comments etc.

Uploaded by

ManoharChinta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
596 views8 pages

Playing With 'Printf ' Statement

The document contains a series of questions and answers about using printf statements in C programming. It provides examples of code snippets using printf and analyzes the expected output. The document assumes a DOS environment using Turbo C/C++ compiler. It covers various aspects of printf like format specifiers, escape sequences, argument order evaluation, macros, comments etc.

Uploaded by

ManoharChinta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

By : Nanda Kishor K N

Mail ID : nandakishor
[email protected]
________________________________________________________________________________
_________________
C For Swimmers
______________
If u don't know how to swim, learn it now. Otherwise participate in the co
mpetition.
________________________________________________________________________________
_________________
Topic :
Playing with 'printf()' statement
_____ _____________________________________
********************************************************************************
*****************
* NOTE : All the programs are tested under Turbo C/C++ compilers.
*
* It is assumed that,
*
*
*=> Programs run under DOS environment,
*
*
*=> The underlying machine is an x86 system,
*
*
*=> Necessary header files are included.
*
*
*=> Program is compiled using Turbo C/C++ compiler.
*
* The program output may depend on the information based on this assumptions.
*
* (For example sizeof(int) = 2 bytes may be assumed).
*
********************************************************************************
*****************
[Q001]. What will be the output of the following program :
void main()
{
printf();
}
(a)Run-Time Error
(b)Compile-Time Error
(c)No Output
(d)None of these
Ans. (b) Since there must be enough arguments for the format.
________________________________________________________________________________
_________________
[Q002]. What will be the output of the following program :
void main()
{
printf(NULL);
}
(a)Run-Time Error
(b)Compile-Time Error
(c)No Output
(d)None of these
Ans. (c) Since NULL is a constant value or NULL pointer value or a NULL string.
________________________________________________________________________________

_________________
[Q003]. What will be the output of the following program :
void main()
{
printf("%%",7);
}
(a)7
(b)Compile-Time Error
(c)%
(d)%%
Ans. (c) Since % is a format specifier & excess arguments (more than required by
the format) are
merely ignored.
________________________________________________________________________________
_________________
[Q004]. What will be the output of the following program :
void main()
{
printf("//",5);
}
(a)5
(b)Compile-Time Error
(c)/
(d)//
Ans. (d) Since / is an escape sequence character & excess arguments (more than r
equired by the format) are merely ignored.
________________________________________________________________________________
_________________
[Q005]. What will be the output of the following program :
void main()
{
printf("d%",8);
}
(a)8
(b)Compile-Time Error
(c)d%
(d)None of these
Ans. (c) Since excess arguments (more than required by the format) are merely ig
nored.
________________________________________________________________________________
_________________
[Q006]. What will be the output of the following program :
void main()
{
printf("%d"+0,123);
}
(a)123
(b)Compile-Time Error
(c)No Output
(d)None of these
Ans. (a) "%d"+0 has no effect on the output operation.
________________________________________________________________________________
_________________
[Q007]. What will be the output of the following program :
void main()
{
printf("%d"+1,123);
}
(a)123
(b)Compile-Time Error
(c)d
(d)No Output
Ans. (c) "%d"+1 (i.e 1 or > 0) affects the program output by considering "%d" as
string and ignores 123
Where 1 refers to the index i.e. 2nd character in the array or string "%d".

________________________________________________________________________________
_________________
[Q008]. What will be the output of the following program :
void main()
{
printf("%d",printf("Hi!")+printf("Bye"));
}
(a)ByeHi!6
(b)Hi!Bye6
(c)Compile-Time Error
(d)None of these
Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is 3+3=6
________________________________________________________________________________
_________________
[Q009]. What will be the output of the following program :
void main()
{
printf("%d",printf("Hi!")*printf("Bye"));
}
(a)ByeHi!6
(b)Hi!Bye9
(c)Hi!Bye
(d)None of these
Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is 3*3=9
________________________________________________________________________________
_________________
[Q010]. What will be the output of the following program :
void main()
{
printf("%d",printf("")+printf(""));
}
(a)0
(b)No Output
(c)Compile-Time Error
(d)None of these
Ans. (a) Since L->R priority & the length of the 2 empty strings are : 0+0=0
________________________________________________________________________________
_________________
[Q011]. What will be the output of the following program :
void main()
{
printf("Hi Friends"+3);
}
(a)Hi Friends
(b)Friends
(c)Hi Friends3
(d)None of these
Ans. (b) Since (base adress)+0 points to the value 'H'. Now the NEW (base addres
s) equals
(base address)+3 that points to the character 'F'. Thus it prints the string fro
m 'F' onwards.
________________________________________________________________________________
_________________
[Q012]. What will be the output of the following program :
void main()
{
printf("C For ") + printf("Swimmers");
}
(a)Compile-Time Error (b)C For Swimmers
(c)Run-Time Error
(d)None of these
Ans. (b) It is a VALID C statement. Change the operators but the output remains
same(NO EFFECT).
________________________________________________________________________________

_________________
[Q013]. What will be the output of the following program :
void main()
{
printf("\/\*\-*\/");
}
(a)Run-Time Error
(b)\/*-*\/
(c)/*-*/
(d)None of these
Ans. (c) Since \ is an escape sequence character. Be careful while analyzing suc
h statements.
________________________________________________________________________________
_________________
[Q014]. What will be the output of the following program :
int main()
{
int main=7;
{
printf("%d",main);
return main;
}
printf("Bye");
}
(a)Compile-Time Error (b)Run-Time Error
(c)7Bye
(d)7
Ans. (d) It is a VALID C statement. Prints 7 and returns the same to the OS. NOT
E: Last printf
statement will not be executed.
________________________________________________________________________________
_________________
[Q015]. What will be the output of the following program :
void main()
{
main();
}
(a)Compile-Time Error (b)Run-Time Error
(c)Infinite Loop
(d)None of these
Ans. (c) It is a VALID C statement. It is like a recursive function & the statem
ents get executed
infinite number of times.
________________________________________________________________________________
_________________
[Q016]. What will be the output of the following program :
void main()
{
printf("Work" "Hard");
}
(a)Work
(b)Hard
(c)No Output
(d)WorkHard
Ans. (d) Since L->R priority. First it prints the word 'Work' & then 'Hard'.
________________________________________________________________________________
_________________
[Q017]. What will be the output of the following program :
void main()
{
char str[]="%d";

int val=25;
printf(str,val);
}
(a)Compile-Time Error (b)Run-Time Error
(c)25
(d)None of these
Ans. (c) It is a VALID C statement. First parameter contains the format specifie
r & the Second
parameter contains the actual value 25.
________________________________________________________________________________
_________________
[Q018]. What will be the output of the following program :
void main()
{
int val=75;
printf("%d",val,.,.);
}
(a)Compile-Time Error (b)Unpredictable
(c)75
(d)None of these
Ans. (b) Output is Unpredictable B'coz there are not enough arguments for the fo
rmat. But it is a
VALID C statement.
________________________________________________________________________________
_________________
[Q019]. What will be the output of the following program :
void main()
{
int val=10;
printf("%d",val+1,"%d",val--);
}
(a)10
(b)11 10
(c)11 9
(d)10 9
Ans. (a) Since R->L priority. The second format specifier '%d' is an excess argu
ment and it is
ignored.
________________________________________________________________________________
_________________
[Q020]. What will be the output of the following program :
void main()
{
int val=5;
printf("%d %d %d %d",val,--val,++val,val--);
}
(a)3 4 6 5
(b)5 5 6 5
(c)4 4 5 5
(d)None of these
Ans. (c) Since R->L priority.
________________________________________________________________________________
_________________
[Q021]. What will be the output of the following program :
void main()
{
int val=5,num;
printf("%d",scanf("%d %d",&val,&num));
}
[NOTE : ASSUME 2 values are entered by the user are stored in the variables 'val
' & 'num' respectively.]
(a)1
(b)2
(c)5

(d)None of these
Ans. (b) Since scanf statement returns the number of input fields successfully s
canned, converted
& stored.
________________________________________________________________________________
_________________
[Q022]. What will be the output of the following program :
#define Compute(x,y,z) (x+y-z)
void main()
{
int x=2,y=3,z=4;
printf("%d",Compute(y,z,(-x+y)) * Compute(z,x,(-y+z)));
}
(a)40
(b)30
(c)Compile-Time Error
(d)None of these
Ans. (b) Since it is macro function. NOTE : Be careful while doing such type of
calculations.
________________________________________________________________________________
_________________
[Q023]. What will be the output of the following program :
void main()
{
int m=10,n=20;
printf("%d %d %d",m/* m-value */,/* n-value */n,m*/* Compute m*n */n);
}
(a)Run-Time Error
(b)10 20 200
(c)Compile-Time Error
(d)None of these
Ans. (b) Since comments /*...*/ are ignored by the compiler.
________________________________________________________________________________
_________________
[Q024]. What will be the output of the following program :
void main()
{
int m=10,n=20;
/* printf("%d",m*n);
}
(a)VALID but No Output (b)VALID : Prints 200 (c)Compile-Time Error
(d)None of these
Ans. (c) Since COMMENT statement not ended properly i.e */ is missing in the abo
ve program.
________________________________________________________________________________
_________________
[Q025]. What will be the output of the following program :
void main()
{
int val=97;
"Printing..."+printf("%c",val);
}
(a)Printing...97
(b)97
(c)Compile-Time Error
(d)a
Ans. (d) Since alphabet 'a' is the ASCII equivalent of 97.
________________________________________________________________________________
_________________
[Q026]. What will be the output of the following program :
void main()

{
int val=5;
val=printf("C") + printf("Skills");
printf("%d",val);
}
(a)Skills5
(b)C1
(c)Compile-Time Error
(d)CSkills7
Ans. (d) VALID Since 'printf' function return the no. of bytes output.
________________________________________________________________________________
_________________
[Q027]. What will be the output of the following program :
void main()
{
char str[]="Test";
if ((printf("%s",str)) == 4)
printf("Success");
else
printf("Failure");
}
(a)TestFailure
(b)TestSuccess
(c)Compile-Time Error
(d)Test
Ans. (b) VALID Since 'printf' function return the no. of bytes output.
________________________________________________________________________________
_________________
[Q028]. What will be the output of the following program :
void main()
{
int val=5;
printf("%*d",val);
}
(a)
5
(b)5
(c)Compile-Time Error
(d)None of these
Ans. (a) VALID Since '*' specifies the precision (i.e. the next argument in the
precision). If no
precision is specified then the value itself will be the precision value. Thus i
t prints 5 BLANK
SPACES & then the value 5.
________________________________________________________________________________
_________________
[Q029]. What will be the output of the following program :
void main()
{
int val=5;
printf("%d5",val);
}
(a)Compile-Time Error (b)5
(c)55
(d)
5
Ans. (c)
________________________________________________________________________________
_________________
[Q030]. What will be the output of the following program :
void main()
}
int val=5;
printf("%d",5+val++);
{

(a)Compile-Time Error (b)5


(c)10
(d)11
Ans. (a) Since incorrect usage of pair of braces } and {. Correct usage : Each c
ompound statement
should be enclosed within a pair of braces, i.e { and }.
________________________________________________________________________________
_________________
Thanx for using "C For Swimmers".
Regarding this material, you can send Bug Reports, Suggestions, Comments,etc. to
[email protected]

You might also like