* why right-to-left evaluation in printf argument list?
@ 2007-05-24 4:28 Shriramana Sharma
2007-05-25 16:43 ` Mariusz Kozlowski
2007-05-25 22:54 ` Glynn Clements
0 siblings, 2 replies; 3+ messages in thread
From: Shriramana Sharma @ 2007-05-24 4:28 UTC (permalink / raw)
To: Linux C Programming List
For you people this is probably old hat but none of my book sources
explain the C / C++ idiosyncrasy of evaluating arguments of printf (and
maybe other functions too, I don't know) from right-to-left. For
instance, the following code:
# include <stdio.h>
void main ( void )
{
int i = 0 ;
printf ( "%d %d\n", i ++, i ++ ) ;
}
prints out:
1 0
instead of:
0 1
as expected. I remember having heard of this years ago and got around to
testing it today, but I don't get the logic behind this behaviour. Is
there one? If yes, what is it?
Thanks.
Shriramana Sharma.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: why right-to-left evaluation in printf argument list?
2007-05-24 4:28 why right-to-left evaluation in printf argument list? Shriramana Sharma
@ 2007-05-25 16:43 ` Mariusz Kozlowski
2007-05-25 22:54 ` Glynn Clements
1 sibling, 0 replies; 3+ messages in thread
From: Mariusz Kozlowski @ 2007-05-25 16:43 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Hello,
> For you people this is probably old hat but none of my book sources
> explain the C / C++ idiosyncrasy of evaluating arguments of printf (and
> maybe other functions too, I don't know) from right-to-left. For
> instance, the following code:
>
> # include <stdio.h>
> void main ( void )
> {
> int i = 0 ;
> printf ( "%d %d\n", i ++, i ++ ) ;
> }
>
> prints out:
>
> 1 0
>
> instead of:
>
> 0 1
>
> as expected. I remember having heard of this years ago and got around to
> testing it today, but I don't get the logic behind this behaviour. Is
> there one? If yes, what is it?
Take a look here -> http://www.c-faq.com/expr/comma.html
Regards,
Mariusz
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: why right-to-left evaluation in printf argument list?
2007-05-24 4:28 why right-to-left evaluation in printf argument list? Shriramana Sharma
2007-05-25 16:43 ` Mariusz Kozlowski
@ 2007-05-25 22:54 ` Glynn Clements
1 sibling, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2007-05-25 22:54 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> For you people this is probably old hat but none of my book sources
> explain the C / C++ idiosyncrasy of evaluating arguments of printf (and
> maybe other functions too, I don't know) from right-to-left. For
> instance, the following code:
>
> # include <stdio.h>
> void main ( void )
> {
> int i = 0 ;
> printf ( "%d %d\n", i ++, i ++ ) ;
> }
>
> prints out:
>
> 1 0
>
> instead of:
>
> 0 1
>
> as expected. I remember having heard of this years ago and got around to
> testing it today, but I don't get the logic behind this behaviour. Is
> there one? If yes, what is it?
Function calls (for all functions, not just variadic functions such as
printf) are typically implemented by evaluating arguments
right-to-left, pushing each value onto the stack as it is calculated,
then calling the function's start address with a CALL/JSR/BL/etc
instruction (which saves the current PC/IP somewhere then jumps to the
specified address).
The result is that the function's arguments are stored at increasing
addresses when read left-to-right.
This allows for variadic functions, where the function knows where to
find the first argument, but has to determine for itself how many
arguments are present.
Note that this evaluation order isn't dictated by any standard; it's
an implementation detail. If you use side-effects in the arguments to
a function call such that the values depend upon the evaluation order,
the result is implementation-dependent.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-05-25 22:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-24 4:28 why right-to-left evaluation in printf argument list? Shriramana Sharma
2007-05-25 16:43 ` Mariusz Kozlowski
2007-05-25 22:54 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).