#include <stdio.h>
void main()
{
printf("%d", printf("%d",printf("Hello world!\n")));
}
Why this gives output as 132 ? hello world is 13 units long.
#include <stdio.h>
void main()
{
printf("%d", printf("%d",printf("Hello world!\n")));
}
Why this gives output as 132 ? hello world is 13 units long.
You may be able to see what's happening more clearly if you split the statement into several statements:
int temp1 = printf("Hello world!\n");
int temp2 = printf("%d", temp1);
printf("%d", temp2);
The first printf
prints Hello world!\n
. Since this is 13 characters, it returns 13
.
The second printf
prints 13
. Since this is 2 characters, it returns 2
.
The third printf
prints 2
.
So the full output will be:
Hello world!
132
It would have been more obvious what's going on if you added more newlines:
printf("%d\n", printf("%d\n",printf("Hello world!\n")));
would print:
Hello world!
13
3
open man 3 printf
and check what it returns
Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).
In your case printf("Hello world!\n")
1st it prints Hello world!
and then returns no of printable char which is 13
and again it prints 2
as 13
has 2
char.