what's this

V

vicky

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
 
?

=?iso-8859-1?q?Lars_Rune_N=F8stdal?=

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?
 
B

Bill Pursell

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;
}
 
F

Flash Gordon

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;
}
 
M

Mark McIntyre

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
 
C

CBFalconer

vicky said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top