printf questions and answers in c programming language


Printf questions and answers in c programming language


1.

What will be output of the following c code?
#include<stdio.h>
int main(){
int a=5,b=6,c=11;
printf("%d %d %d");
return 0;
}

(A) 5 6 11
(B) Garbage values
(C) 11 6 5
(D) Compiler error
(E) None of these


Explanation:

Automatic variables are stored in the stack which follows LIFO data structure.


2.

What will be output of the following c code?

#include<stdio.h>
int main(){
char *str="CQUESTIONBANK";
printf(str+9);
    return 0;
}

(A) CQUESTIONBANK
(B) CQUESTION
(C) BANK
(D) Compiler error
(E) None of these


Explanation:

Str + 9
= Address of character ‘C’ + 9
= Address of character ‘B’

3.

What will be output of the following c code?

#include<stdio.h>
int main(){
printf("%d",printf("CQUESTIONBANK"));
    return 0;
}
(A) CQUESTIONBANKCQUESTIONBANK
(B) CQUESTIONBANK13
(C) 13CQUESTIONBANK
(D) Compiler error
(E) None of these

Explanation:

Printf function returns and integer type data which is equal to the number of characters it prints. 

4.

What will be output of the following c code?

#include<stdio.h>
int main(){
short int a=5;
printf("%d"+1,a);
return 0;
}

(A) a
(B) d
(C) 1a
(D) Compiler error
(E) None of these


Explanation:

“%d” + 1
= Address of character ‘%’ + 1
= Address of character ‘d’

5.

What will be output of the following c code?

#include<stdio.h>
int main(){
int i=85;
printf("%p %Fp",i,i);
    return 0;
}
(A) 034E 034E:0055
(B) 0055 0055:034E
(C) 0055 034E:0055
(D) Compiler error
(E) None of these


Explanation:

%p : It prints offset address of variable
%Fp: It prints offset address with segment number separated by colon.

6.

What will be output of the following c code?

#include<stdio.h>
int main(){
char p[]="ok%s%c%s";
int a=8;
printf(p+2,"mone",a,"key");
return 0;
}

(A)
okmonekey
(B) okmonekey
(C) monkey
(D) Compiler error
(E) None of these


Explanation:

“ok%s%c%s” + 2 = “%s%c%s”
8 is ASCII value of back space character constantan. 

7.

What will be output of the following c code?

#include<stdio.h>
int main(){
int a=13;
printf("india%cpak",a);
    return 0;
}

(A) india13pak
(B) pakia
(C) indiapak
(D) Compiler error
(E) None of these


Explanation:

13 is ASCII value of carriage return.

8.

What will be output of the following c code?

#include<stdio.h>
int main(){
char name1[]="sachin";
char name2[]="al" ;
printf("%s\b\b\b%s",name1,name2);
    return 0;
}

(A) sacaln
(B) sachinal
(C) scahin   al
(D) Compiler error
(E) None of these


Explanation:

\b is backspace character. It moves the cursor one space back.

9.

What will be output of the following c code?

#include<stdio.h>
int main(){
char name1[]="qwertyudgbank";
char name2[]="cquestion" ;
printf("%s\r%s",name1,name2);
    return 0;
}

(A) qwertyudgbank
(B) cquestion
(C) cquestionbank
(D) Compiler error
(E) None of these


Explanation:
‘\r’ is carriage return character constantan. It moves the cursor beginning of same line. 

10.

What will be output of the following c code?

#include<stdio.h>
int main(){
    static int x;
    printf("%d %d %d",++x,x++,x++);
    return 0;
}

(A) 0 0 1
(B) 3 0 0
(C) 3 1 0
(D) 1 1 2
(E) Compilation error


Explanation:

Printf function is cdecl type. So it passes the parameter from right to left.

11.

What will be output of the following c code?

#include<stdio.h>
int main(){
    printf("%d",printf("one"));
    return 0;
}

(A) 0one
(B) one0
(C) 3one
(D) one3
(E) Compilation error


Explanation:

Printf function returns and integer type data which is equal to the number of characters it prints. 

12.

What will be output of the following c code?

#include<stdio.h>
int main(){
    char *str="cquestionbank.blogspot.com";
    printf("%*.*s",8,5,str);
    return 0;
}

(A) cquest
(B) cquestion
(C)    cquest
(D)    cques
(E)
Compilation error


Explanation:

*.* in printf statements represents width.Precision


13.

What will be output of the following c code?

#include<stdio.h>
int main(){
    char *str="cquestionbank.blogspot.com";
    printf("%*.*s",8,5,str);
    return 0;
}

(A) cquest
(B) cquestion
(C)    cquest
(D)    cques
(E) Compilation error


Explanation:

*.* in printf statements represents width.Precision

14.

What will be output of the following c code?

#include<stdio.h>
extern int a;
static int a;
int main(){
    printf("%d+%i",a,a);
    return 0;
}
int a=10;

(A) 20
(B) 10+10
(C) 10
(D) 10+0
(E) Compilation error


Explanation:

We can declare same variable more than one time globally.

15.

What will be output of the following c code?

#include<stdio.h>
int main(){
    printf("\\\\");
    return 0;
}

(A) \\\\
(B) ////
(C) \\
(D)
null
(E)
Compilation error


Explanation:

‘\’ is special character in c. To remove its special meaning we add another ‘\’ character that is two ‘\’ characters will print single ‘\’ character.  

16.

What will be output of the following c code?

#include<stdio.h>
int main(){
    int num= 054;
    printf("%x %X",num,num);
    return 0;
}

(A) 054 054
(B) 032 032
(C) 2d 2D
(D) 2c 2C
(E) Compilation error


Explanation:

054 is octal number
%x: Prints number in hexadecimal format with a,b,c,d,e,f
%X: Prints number in hexadecimal format with A,B,C,D,E,F

17.

What will be output of the following c code?

#include<stdio.h>
int main(){
    int num= 0x21;
    printf(1+"%s %#o",'=',num);
    return 0;
}

(A) = 21
(B) = 33
(C) s 075
(D) = 34
(E) Compilation error


Explanation:

1 + “%s” is equivalent to “s”
ASCII value of ‘=’ is 75

18.

What will be output of the following c code?

#include<stdio.h>
int main(){
    float f= 0x21E02F;
    char *str="%.2f";
    printf(str,f);
    return 0;
}

(A) 0x21E02.00
(B) 222079.00
(C) 21*10^2
(D) 33*10^2
(E) Compilation error


Explanation:

Number which starts 0x are hexadecimal numbers.

19.

What will be output of the following c code?

#include<stdio.h>
int main(){
    char *str="cquestionbank";
    printf("%s",str);
    return 0;
}

int printf(char const *str,...){
    puts(str);
    return 0;
}

(A) cquestionbank
(B) cquestion
(C) %s
(D) Run time error
(E) Compilation error


Explanation:

Here it will not call printf function which has been declared in the stdio.h

20.

What will be output of the following c code?

#include<stdio.h>
int main(){
    auto val=-25;
    auto num= 25;
    printf("%+d\t",val);
    printf("%#x\t",val);
    printf("%     d",num);
    return 0;
}

(A) -25 0x25 %   d
(B) -25 0xffe7 25
(C) -25 -0x25 25
(D) 25 0xffe7 %  d 
(E) Compilation error


Explanation:

+: This flag is used to print sign of any number.
#: This flag is used to print 0x with %x
Blank space: This flag has not any effect with %d.



If you have any quires or suggestions in above printf function or input or output (I/O) questions in c, you can ask here. 

1 comment:

Alex Bandit said...

hebrew translation It's good to see this information in your post, i was looking the same but there was not any proper resource.