Re: question about printf

From: Vijay Kumar R Zanvar (vijoeyz_at_globaledgesoft.com)
Date: 05/26/04


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/


Relevant Pages

  • Re: CIL question
    ... > compilation as execution methods. ... They need different representations for 32 ... intermediate representation with a infinite number of registers (e.g. ...
    (comp.compilers)
  • Curious about loop optimization C++ - assembly
    ... I sometimes look at the produced assembly after compilation in release mode. ... What you often see, is that CPP, always fully addresses registers to copy values from a to b... ... While stosb,stosw, stosd etc and the same for movsare one statement, and internally use registers ESI and EDI to copy data. ... MOV ESI, ...
    (microsoft.public.dotnet.languages.vc)
  • Re: x86-64 and calling conventions
    ... registers rather than real ones. ... function it will call at the time of compilation perhaps the caller ... footer glue code - possibly the more complex of the two glue code ... Duplicate functions - especially if the function ...
    (comp.compilers)
  • Re: Trying to convert old modules to newer kernels
    ... compilation. ... parameter passing in REGISTERS! ... they've done it to the entire kernel! ... reconfigure to get parameter passing put back like it was? ...
    (Linux-Kernel)
  • Re: "out" parameters
    ... >>passing parameters in processor registers. ... An argument can be passed by reference in a register, ... "The terrorists rejoice in the killing of the innocent, ...
    (comp.lang.ada)