pratik said:
i am working a turbo c;
the code i typed in is as follows
#include<stdio.h>
main()
{
int a=5;
printf("%d%d%d",a++,++a,a);
}
The output of the above code is very interesting.
can anyone help me with the output of the above code
Please tell me the reason for the same
Thanking you
in advance
This program can said to be unportable. The reason is that the order
of evaluation of arguments is unspecified. The above program, upon compilation,
gives the following warning messages.
F:\Vijay\C> gcc printf_params.c -Wall
printf_params.c: In function `main':
printf_params.c:5: warning: operation on `a' may be undefined
printf_params.c:5: warning: operation on `a' may be undefined
printf_params.c:5: warning: operation on `a' may be undefined
Consider an example,
(*fptr)( (i=2), k );
Here, which expression -- i.e., "fptr", (i=2), or k -- would be evaluated first
is unspecified by the standard. And in the same way, which argument will pushed
onto the stack -- if that is _the_ method of parameter passing -- is also
unspecified. However, it is guaranteed that all the side effects would take
place before the function is called.
Your answer is particular to the compiler you are using, though, generally
passing parameters from right to left seems to be common.
There are many ways in which parameters can be passed to a function. This
again depends on the underlying CPU architecture. Many heavy-performance based
CPUs (sorry, don't know any particulare name) may have separate sets of
registers for parameter passing; the compiler doesn't even use stack! For
maximum portability of programs, no assumptions should be made. Consider
another example:
void
mango ( register int a, int b )
{
/* .. */
}
int
main ( void )
{
int k, j;
/* .. */
mango ( k, j );
return 0;
}
Assume that the compiler fulfills the request to use a register for the
first parameter (what if only one register is available?). How about the
second parameter?