What is The Output of the following program? #include<stdio.h> void main() { int i = printf("00"); int i =printf("00"); printf("%d",i); } 0 00 002
Question
What is The Output of the following program?
#include<stdio.h>
void main()
{
int i = printf("00"); int i =printf("00");
printf("%d",i);
}
0 00 002
Solution
The output of the program will be "002". Here's why:
-
The
printffunction returns the number of characters printed. So, when you doint i = printf("00");, it prints "00" and returns 2 (the number of characters printed), which is then stored ini. -
However, you have a duplicate declaration of
iin the same scope, which is not allowed in C. So, the program should actually give a compile error.
But if we assume that the second declaration of i is a mistake and ignore it, then the printf("%d",i); will print the value of i, which is 2.
So, the output will be "002".
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.