Why does post or pre incremenent or decrement does not work inside a sizeof operator?

Discussion in 'C Programming' started by Tarun, Jul 14, 2005.

  1. Tarun

    Tarun Guest

    Hi All,
    I have written the following prog.

    int main()
    {
    int val = 4;
    printf("size of val = %d, val = %d",sizeof(++val),val);
    }
    output: size of val = 2, val = 4
    The output expected was : size of val = 2, val = 5

    Can somebody explain why?

    Thanks in advance

    Regards
    Tarun
    Tarun, Jul 14, 2005
    #1
    1. Advertising

  2. In article <>,
    Tarun <> wrote:
    >I have written the following prog.


    >int main()
    >{
    > int val = 4;
    > printf("size of val = %d, val = %d",sizeof(++val),val);
    >}


    >output: size of val = 2, val = 4
    >The output expected was : size of val = 2, val = 5


    >Can somebody explain why?


    The order of evaluation of parameters to a function is
    undefined in the standard. It is legal (and not uncommon)
    that the last parameter, val, would be evaluated before
    the next-to-last parameter, ++val .

    The operation of ++val and val++ is defined in terms of
    "sequence points": the increment takes place -sometime- between
    the beginning of the sequence point and the end, with it being
    undefined as to exactly when. If you attempt to use the same
    variable within the same sequence point, it isn't defined as to
    whether you will get the old or new value for the other
    references -- it isn't even certain that you will always get
    the same order for the same compiler (as compilers are allowed
    to apply optimizations within sequence points.)

    As a rough approximation, sequence points are usually present
    at the beginning and end of expressions, but within expressions
    only && and || force sequence points (and possibly the ','
    sequencing operator... I use that so seldom that I
    would have to look up it's properties to be sure.)
    --
    Any sufficiently advanced bug is indistinguishable from a feature.
    -- Rich Kulawiec
    Walter Roberson, Jul 14, 2005
    #2
    1. Advertising

  3. Tarun

    Jirka Klaue Guest

    Walter Roberson:
    > Tarun:
    >
    >> int main()
    >> {
    >> int val = 4;
    >> printf("size of val = %d, val = %d",sizeof(++val),val);
    >> }

    >
    >> output: size of val = 2, val = 4
    >> The output expected was : size of val = 2, val = 5

    >
    > The order of evaluation of parameters to a function is
    > undefined in the standard. It is legal (and not uncommon)
    > that the last parameter, val, would be evaluated before
    > the next-to-last parameter, ++val .


    That's true but unrelated to the code above. ;-)
    The operand of sizeof is evaluated only in terms of its type.

    Jirka
    Jirka Klaue, Jul 14, 2005
    #3
  4. Tarun

    pete Guest

    Walter Roberson wrote:
    >
    > In article <>,
    > Tarun <> wrote:
    > >I have written the following prog.

    >
    > >int main()
    > >{
    > > int val = 4;
    > > printf("size of val = %d, val = %d",sizeof(++val),val);
    > >}

    >
    > >output: size of val = 2, val = 4
    > >The output expected was : size of val = 2, val = 5

    >
    > >Can somebody explain why?

    >
    > The order of evaluation of parameters to a function is
    > undefined


    The order is called "unspecified".

    > in the standard. It is legal (and not uncommon)
    > that the last parameter, val, would be evaluated before
    > the next-to-last parameter, ++val .


    No. That's not it.
    The sizeof operator doesn't evaluate it's operand.

    /* BEGIN new.c */

    #include<stdio.h>

    int main(void)
    {
    int val = 4;

    sizeof(++val);
    sizeof(++val);
    sizeof(++val);
    sizeof(++val);
    sizeof(++val);
    sizeof(++val);
    printf("size of val = %d, val = %d",sizeof(++val),val);
    return 0;
    }

    /* END new.c */


    --
    pete
    pete, Jul 14, 2005
    #4
  5. Tarun

    Richard Bos Guest

    -cnrc.gc.ca (Walter Roberson) wrote:

    > In article <>,
    > Tarun <> wrote:
    > >I have written the following prog.

    >
    > >int main()
    > >{
    > > int val = 4;
    > > printf("size of val = %d, val = %d",sizeof(++val),val);
    > >}

    >
    > >output: size of val = 2, val = 4
    > >The output expected was : size of val = 2, val = 5

    >
    > >Can somebody explain why?


    Yes. The ISO C Standard demands it.

    > The order of evaluation of parameters to a function is
    > undefined in the standard.


    This has nothing whatsoever to do with the problem (though it _is_ true,
    and would have been a problem had the OP used any other operator than
    sizeof. For example, printf("%d, %d", 3 * ++val, val) would have had the
    problem you describe. Except that it's even worse: it's not just the
    order of evaluation that is undefined, but the entire outcome of the
    program from then on. A crash would not be out of line.)

    The real problem, if it is one, is that the Standard includes this:

    # If the type of [sizeof's] operand is a variable length array
    # type, the operand is evaluated; otherwise, the operand is not
    # evaluated and the result is an integer constant.

    Since ++val is not a VLA, it is not evaluated. It needn't be; its size
    does not depend on its value.

    Richard
    Richard Bos, Jul 14, 2005
    #5
  6. Tarun

    Tarun Guest

    Re: Why does post or pre incremenent or decrement does not work inside a sizeof operator?

    Thanks all for the comments being provided.
    Tarun, Jul 14, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Turney
    Replies:
    11
    Views:
    4,262
    dibeas
    Nov 13, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,733
    Smokey Grindel
    Dec 2, 2006
  3. Spiros Bousbouras
    Replies:
    40
    Views:
    1,453
    Keith Thompson
    Jan 20, 2007
  4. Angel Tsankov
    Replies:
    8
    Views:
    458
    Ian Collins
    Feb 27, 2006
  5. Kislay

    The sizeof operator : sizeof(++i)

    Kislay, Oct 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    669
    Peter Pichler
    Oct 19, 2007
Loading...

Share This Page