By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 821 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

what's this

P: n/a
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me

Feb 24 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Sat, 24 Feb 2007 06:53:46 -0800, vicky wrote:
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me

Some mistakes there. This is what you mean:

#include <stdio.h>

int main(){
int a = 2;
printf("%d\n", printf("%d %d ", a, a));
return 0;
}
...and that will print 2 2 4. Now for the reason for that see `man 3
printf' ( http://www.die.net/doc/linux/man/man3/printf.3.html ) which
states:

int printf(const char *format, ...);
...which means printf is a function call that returns a value of type int.
It further states:

Return value
Upon successful return, these functions return the number of characters
printed (not including the trailing ’\0’ used to end output to
strings). ... If an output error is encountered, a negative value is
returned.
Now take a look:

#include <stdio.h>

int a(int x){
return x * x;
}

int main(){
printf("%d\n", a(a(a(2))));
return 0;
}
...that will print 256 just as this will print 256:

#include <stdio.h>

int a(int x){
return x * x;
}

int main(){
int tmp1 = a(2);
int tmp2 = a(tmp1);
int tmp3 = a(tmp2);
printf("%d\n", tmp3);
return 0;
}
...get it?

--
Lars Rune Nøstdal
http://nostdal.org/

Feb 24 '07 #2

P: n/a
On Feb 24, 2:53 pm, "vicky" <goelvi...@gmail.comwrote:
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();

}

i did not understand what these two printf in a command signifies.
please help me

Perhaps it will be clearer if you see the similarity
between your code and this:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int a=2;
int b;

b = printf( "\n%d %d", a, a );
printf("%d", b);

return EXIT_SUCCESS;
}

--
Bill Pursell

Feb 24 '07 #3

P: n/a
vicky wrote, On 24/02/07 14:53:
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me
It isn't on my system. On my system the output is a compilation error.
Perhaps if you posted your actual code (copy and past, do not retype)
then a better suggestion could be given. Although I suspect even then
the answer will be "because it is since you invoked undefined behaviour".

A few general comment though, main returns an int and it is better to be
explicit about that (it is required in the latest C standard) and it is
also better to be explicit about not taking any parameters. Having
realised main returns an int you should then return one! Calling printf
requires a prototype, so you should include <stdio.h>. Standard C does
not have a getch function, it has a getchar function (prototype in
stdio.h) which probably does what you want, although learning to drive
your IDE would be better so you do not have to make the program pause.
So a slightly improved form would be:

#include <stdio.h>

int main(void)
{
int a=2;
/* Some printf calls, but I'm not sure what you intended */
getchar(); /* better to remove this, but I'll leave it in for you */
return 0;
}
--
Flash Gordon
Feb 24 '07 #4

P: n/a
On 24 Feb 2007 06:53:46 -0800, in comp.lang.c , "vicky"
<go*******@gmail.comwrote:
>why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me
This is essentially FAQs 3.1 & 3.2 & 3.3
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 24 '07 #5

P: n/a
vicky wrote:
>
why is the output of the following command is 4 4 5.

main()
{
int a=2;
printf("%d",printf("\n%d %d",a,a,a);
getch();
}

i did not understand what these two printf in a command signifies.
please help me
Perfectly legitimate. Any behaviour whatsoever is allowed when you
invoke undefined behavior. On another system you might invoke
WWIII, or find demons in your snot.

Lack of #include <stdio.h>
Failure to include a '\n' in the output, or a fflush(stdout).
getch undefined
main returns int. Say so and do so.

In addition, blanks are no longer on allocation, so you can use
them without fouling the supply lines. They are quite cheap too.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 24 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.