Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C printf based Aptitude Questions and Answers
This section contains Aptitude Questions and Answers on C language C printf based (with multiple answers) with explanation.
Submitted by Ashish Varshney, on February 21, 2018
List of C programming printf based Aptitude Questions and Answers
1) What will be the answer of the code?
int main()
{
int a=1,b=2,c=3;
printf("%d %d %d");
return 0;
}
- Garbage value
- compile_time Error
- 3 2 1
- None of above
Correct answer: 1
Gargabe value
Most of the compiles give the garbage values because the list of the variables are not defined and some of the compilers may give "3 2 1" because C compiler use stack memory allocation, so, if we not give an any order in print statement then it uses LIFO (Last In First Out).
2) What will be the answer of the code?
#define print "%sincludehelp "
int main()
{
int a=1,b=2,c=3;
printf(print,print);
return 0;
}
- Garbage value
- includehelp %sincludehelp
- %sincludehelp includehelp
- None of the above
Correct answer: 3
%sincludehelp includehelp
After pre-processing phase of compilation, printf statement will become.
printf ("%sincludehelp ","%sincludehelp ");
It prints "%sincludehelp includehelp"
3) What will be the output?
int main()
{
int a=34;
printf("%d %i %p %n",a,a,a,a);
return 0;
}
- 34 34 00 22
- compilation error
- 34
- None of these.
Correct answer: 2
compilation error
%p is to print address of pointer or any other variable in hexadecimal value. Hence, it needs &a as parameter. Thus it shows compilation error.
4) What will be the output?
int main()
{
char *str="includehelp";
printf("%s",str+7);
return 0;
}
- help
- includehelp
- ehelp
- None of these
Correct answer: 1
help
str is a character pointer array of length 11 from index 0 - 10. In printf statement str+7 means base address plus 7 to get starting index.
5) What will be the output?
int main()
{
printf("%d",printf("includehelp"));
return 0;
}
- 11includehelp
- includehelp11
- compile_time error
- None of these
Correct answer: 2
includehelp11
printf("%d",printf("includehelp"));
There are two printf statements...
The first one actually prints what the second printf function returns. printf function returns the length of message being printed by the printf function. The second printf function executes first & hence it prints "includehelp" whereas, the first printf prints 11 then, which is the length of the message printed by second print function.
So, output is "includehelp11"
6) What will be the output?
int main()
{
int n=0;
while(n<printf("MENU\n"))
{
n++;
}
return 0;
}
- MENU
MENU
MENU
MENU
MENU
MENU
- MENU
MENU
MENU
MENU
MENU
- compile_time error
- None of these
Correct answer: 1
MENU
MENU
MENU
MENU
MENU
MENU
Explanation:
n=0
0th iteration:
Prints "Menu" returns 5 //count of "Menu" printing 1
n<5
n=n+1=1
1st iteration:
Prints "Menu" returns 5 //count of "Menu" printing 2
n<5
n=n+1=2
2nd iteration:
Prints "Menu" returns 5 //count of "Menu" printing 3
n<5
n=n+1=3
3rd iteration:
Prints "Menu" returns 5 //count of "Menu" printing 4
n<5
n=n+1=4
4th iteration:
Prints "Menu" returns 5 //count of "Menu" printing 5
n<5
n=n+1=5
5th iteration:
Prints "Menu" returns 5 //count of "Menu" printing 6
N is not <5
Thus program terminates
"Menu" has been printed 6 times