Re: question about printf
From: Vijay Kumar R Zanvar (vijoeyz_at_globaledgesoft.com)
Date: 05/26/04
- Next message: Daniel Fischer: "Re: question about printf"
- Previous message: Thomas stegen: "Re: question about printf"
- In reply to: pratik: "question about printf"
- Next in thread: Richard Bos: "Re: question about printf"
- Reply: Richard Bos: "Re: question about printf"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 26 May 2004 16:11:24 +0530
"pratik" <pratikthakkar007@yahoo.com> wrote in message news:c14f7963.0405260222.72d65f59@posting.google.com...
> 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?
-- Vijay Kumar R Zanvar My Home Page - http://www.geocities.com/vijoeyz/
- Next message: Daniel Fischer: "Re: question about printf"
- Previous message: Thomas stegen: "Re: question about printf"
- In reply to: pratik: "question about printf"
- Next in thread: Richard Bos: "Re: question about printf"
- Reply: Richard Bos: "Re: question about printf"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|