The sizeof operator : sizeof(++i)

Discussion in 'C Programming' started by Kislay, Oct 18, 2007.

  1. Kislay

    Kislay Guest

    int main()
    {
    int i=10;
    printf("\n Size of i = %d ",sizeof(++i));
    printf("\n i = %d ",i);
    system("pause");
    return 0;
    }

    On executing the above code , the value of i obtained as 10 . What
    happens to the increment ? Why does not not that take place ? Has it
    got something to do with the fact that sizeof is a compile-time
    operator ?
     
    Kislay, Oct 18, 2007
    #1
    1. Advertising

  2. Kislay wrote:
    > int main()
    > {
    > int i=10;
    > printf("\n Size of i = %d ",sizeof(++i));
    > printf("\n i = %d ",i);
    > system("pause");
    > return 0;
    > }
    >
    > On executing the above code , the value of i obtained as 10 . What
    > happens to the increment ? Why does not not that take place ? Has it
    > got something to do with the fact that sizeof is a compile-time
    > operator ?


    sizeof doesn't evaluate its operand.
     
    christian.bau, Oct 18, 2007
    #2
    1. Advertising

  3. Kislay

    Guest

    On Oct 18, 1:22 pm, Kislay <> wrote:
    > On executing the above code , the value of i obtained as 10 . What
    > happens to the increment ? Why does not not that take place ? Has it
    > got something to do with the fact that sizeof is a compile-time
    > operator ?


    sizeof does not evaluate what it's given, that's why sizeof *p works
    with uninitialized pointers, for example.
    sizeof is not preprocessed, but evaluated/replaced by the compiler.
     
    , Oct 18, 2007
    #3
  4. Kislay

    Guest

    Also, take this for example
    assume sizeof(char) < sizeof(long long)

    char c;
    printf("%zu \n", sizeof (c + 1LL));

    Would print something >1, c is promoted to long long.
     
    , Oct 18, 2007
    #4
  5. Kislay

    Old Wolf Guest

    On Oct 18, 11:32 pm, wrote:
    > On Oct 18, 1:22 pm, Kislay <> wrote:
    >
    > > On executing the above code , the value of i obtained as 10 . What
    > > happens to the increment ? Why does not not that take place ? Has it
    > > got something to do with the fact that sizeof is a compile-time
    > > operator ?

    >
    > sizeof is not preprocessed, but evaluated/replaced by the compiler.


    As is every operator..

    > sizeof does not evaluate what it's given, that's why sizeof *p works
    > with uninitialized pointers, for example.


    For some arguments to sizeof, it is allowed to evaluate
    the argument. for example:

    void func(int n, int array[n][n])
    {
    int i = 0;
    printf("size is %zu\n", sizeof array[i++]);
    printf("i is %d\n", i); // could be either 0 or 1
    }
     
    Old Wolf, Oct 18, 2007
    #5
  6. On Thu, 18 Oct 2007 03:22:06 -0700, in comp.lang.c , Kislay
    <> wrote:

    >int main()
    >{
    > int i=10;
    > printf("\n Size of i = %d ",sizeof(++i));


    warning: incompatible implicit declaration of built-in function printf

    > printf("\n i = %d ",i);


    warning: no newline after output - text may not appear onscreen
    (and on Linux, it doesn't...)

    >On executing the above code , the value of i obtained as 10 .


    I believe that sizeof() doesn't evaluate its operand (unless its a
    VLA, I think), so the increment is never executed.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Oct 18, 2007
    #6
  7. Old Wolf wrote:
    > On Oct 18, 11:32 pm, wrote:
    >
    >>sizeof is not preprocessed, but evaluated/replaced by the compiler.

    >
    > As is every operator..


    Really? Compilers must have progressed quite a lot while I wasn't
    looking. From now on, I will keep in mind that a+b is evaluated at
    compile time.
     
    Peter Pichler, Oct 18, 2007
    #7
  8. Mark McIntyre <> writes:
    [...]
    > I believe that sizeof() doesn't evaluate its operand (unless its a
    > VLA, I think), so the increment is never executed.


    Correct.

    For the record, here's what the standard says (C99 6.5.3.4p2):

    The sizeof operator yields the size (in bytes) of its operand,
    which may be an expression or the parenthesized name of a
    type. The size is determined from the type of the operand. The
    result is an integer. If the type of the operand is a variable
    length array type, the operand is evaluated; otherwise, the
    operand is not evaluated and the result is an integer constant.

    There are some odd cases that this doesn't cover, where either the
    standard says the operand isn't evaluated enen though it really needs
    to be, or the operand is evaluated whne it really doesn't need to be.
    All such cases involve indirect uses of VLAs (variable length arrays).
    We discussed this recently in comp.std.c, subject "Evaluating the
    operand of sizeof".

    But if you're not using VLAs, none of this is relevant; the operand of
    sizeof won't be evaluated if no VLAs are involved.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 19, 2007
    #8
  9. Kislay

    Old Wolf Guest

    On Oct 19, 11:42 am, Peter Pichler <> wrote:
    > Old Wolf wrote:
    > > On Oct 18, 11:32 pm, wrote:

    >
    > >>sizeof is not preprocessed, but evaluated/replaced by the compiler.

    >
    > > As is every operator..

    >
    > Really? Compilers must have progressed quite a lot while I wasn't
    > looking. From now on, I will keep in mind that a+b is evaluated at
    > compile time.


    All expression evaluation occurs at runtime, according
    to the C standard.
     
    Old Wolf, Oct 19, 2007
    #9
  10. On Thu, 18 Oct 2007 18:51:05 -0700, Old Wolf wrote:
    > On Oct 19, 11:42 am, Peter Pichler <> wrote:
    >> Old Wolf wrote:
    >> > On Oct 18, 11:32 pm, wrote:

    >>
    >> >>sizeof is not preprocessed, but evaluated/replaced by the compiler.

    >>
    >> > As is every operator..

    >>
    >> Really? Compilers must have progressed quite a lot while I wasn't
    >> looking. From now on, I will keep in mind that a+b is evaluated at
    >> compile time.

    >
    > All expression evaluation occurs at runtime, according to the C
    > standard.


    enum { zero = 1 - 1 };
    #if 1 > 2
    #error
    #elif 3 * 4
    enum { one = !zero };
    #endif
    int main(int argc, char *argv[zero - one]) {}

    All expressions in this simple invalid program are required to be
    evaluated at compile time. I suppose you could say if the value is
    determined at compile time, this isn't an evaluation. I haven't found the
    definition of evaluation. However, the standard does say at least in the
    description of #if directives that the expressions are evaluated, and
    that must happen at compile time.
     
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F, Oct 19, 2007
    #10
  11. Old Wolf wrote:

    > On Oct 19, 11:42 am, Peter Pichler <> wrote:
    >
    >>Old Wolf wrote:
    >>
    >>>On Oct 18, 11:32 pm, wrote:

    >>
    >>>>sizeof is not preprocessed, but evaluated/replaced by the compiler.

    >>
    >>>As is every operator..

    >>
    >>Really? Compilers must have progressed quite a lot while I wasn't
    >>looking. From now on, I will keep in mind that a+b is evaluated at
    >>compile time.

    >
    > All expression evaluation occurs at runtime, according
    > to the C standard.


    Not all, as Harald described. But my point was that you claimed that
    "every operator is evaluated/replaced by the compiler". At least that's
    how I read your answer to vipvipvipvip..., whoever he/she/it is. The +
    in my a+b is indeed "replaced" by a compiler (with some machine code),
    but I do not think that's what either you or vipvipvipvip... meant.

    Sorry, I used what is usually referred to as "irony".

    irony [n]
    having the properties of iron
     
    Peter Pichler, Oct 19, 2007
    #11
    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. Derek
    Replies:
    7
    Views:
    24,413
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    660
    CBFalconer
    Apr 10, 2004
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,630
    Keith Thompson
    Feb 14, 2005
  4. Vinu
    Replies:
    13
    Views:
    1,499
    Lawrence Kirby
    May 12, 2005
  5. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    578
    Joe Smith
    May 22, 2006
Loading...

Share This Page