Reputation: 209
First of all here's the code :
//functions
char dernier(char* s){
return s[strlen(s)-1];
}
char* debut(char* s){
s[strlen(s)-1] = '\0';
return s;
}
//problematic bit :
printf("%s %s %c", debut(s), s, dernier(s)); //s = "test"
I have expected the output to be tes tes s
but I am getting tes tes t
which I find odd.
Any idea why? Thank you!
Upvotes: 2
Views: 85
Reputation: 21572
There is no mandated order by the C standard in which arguments to functions are to be evaluated. It's entirely up to the implementation/ABI.
If you're on the ubiquitous x86-32 with a variadic function like printf
, you are almost certainly using the calling convention cdecl
, where arguments are pushed right-to-left. This means the assembly code near the call site for printf
will most probably look something like (pseudo-assembly code):
push @s
call _dernier
push <ret val from _dernier>
push @s
push @s
call _debut
push <ret val from _debut>
push @format_string
call _printf
Note, however, that even with the cdecl
calling convention, all that is dictated is the order in which the arguments are to be pushed. They can still be evaluated in any order, as long as they are pushed on the stack right-to-left; you need to look at the generated assembly code from your own compiler to know for sure.
Upvotes: 3