sizeof(x)

Discussion in 'C Programming' started by Ioannis Vranos, Mar 30, 2008.

  1. In C99 it is mentioned:

    "The sizeof operator yields the size (in bytes) of its operand, which
    may be an expression or the parenthesized name of a type.".


    If I am not wrong, this implies that

    int x;

    size_t y= sizeof(x);


    is not valid.


    and only the following is valid:


    int x;

    size_t y= sizeof x;


    However I am puzzled, and thought the first was also valid in
    C90/C95(/C++03).
     
    Ioannis Vranos, Mar 30, 2008
    #1
    1. Advertising

  2. On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
    > In C99 it is mentioned:
    >
    > "The sizeof operator yields the size (in bytes) of its operand, which
    > may be an expression or the parenthesized name of a type.".
    >
    > If I am not wrong, this implies that
    >
    > int x;
    >
    > size_t y= sizeof(x);
    >
    >
    > is not valid.


    You are; (x) is a perfectly valid expression, so there's no problem
    taking the size of (x).
     
    Harald van Dijk, Mar 30, 2008
    #2
    1. Advertising

  3. Harald van Dijk wrote:
    > On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
    >> In C99 it is mentioned:
    >>
    >> "The sizeof operator yields the size (in bytes) of its operand, which
    >> may be an expression or the parenthesized name of a type.".
    >>
    >> If I am not wrong, this implies that
    >>
    >> int x;
    >>
    >> size_t y= sizeof(x);
    >>
    >>
    >> is not valid.

    >
    > You are; (x) is a perfectly valid expression, so there's no problem
    > taking the size of (x).



    I first saw that only sizeof x is valid at the pdf hosted at
    http://cprog.tomsweb.net.

    Then I checked the C99 standard and it mentions what is shown above.
    Clearly C99 doesn't mention parenthesized expression.
     
    Ioannis Vranos, Mar 30, 2008
    #3
  4. Ioannis Vranos wrote:
    >
    > I first saw that only sizeof x is valid at the pdf hosted at
    > http://cprog.tomsweb.net.



    More specifically the above writes:

    "sizeof Returns size of operand in bytes; two forms:
    1) sizeof(type)
    2) sizeof expression"



    > Then I checked the C99 standard and it mentions what is shown above.
    > Clearly C99 doesn't mention parenthesized expression.
     
    Ioannis Vranos, Mar 30, 2008
    #4
  5. Ioannis Vranos

    Ian Collins Guest

    Ioannis Vranos wrote:
    > Ioannis Vranos wrote:
    >> I first saw that only sizeof x is valid at the pdf hosted at
    >> http://cprog.tomsweb.net.

    >
    >
    > More specifically the above writes:
    >
    > "sizeof Returns size of operand in bytes; two forms:
    > 1) sizeof(type)
    > 2) sizeof expression"
    >
    >

    Did you read what Harald said: "(x) is a perfectly valid expression"?

    --
    Ian Collins.
     
    Ian Collins, Mar 30, 2008
    #5
  6. Ian Collins wrote:
    > Ioannis Vranos wrote:
    >> Ioannis Vranos wrote:
    >>> I first saw that only sizeof x is valid at the pdf hosted at
    >>> http://cprog.tomsweb.net.

    >>
    >> More specifically the above writes:
    >>
    >> "sizeof Returns size of operand in bytes; two forms:
    >> 1) sizeof(type)
    >> 2) sizeof expression"
    >>
    >>

    > Did you read what Harald said: "(x) is a perfectly valid expression"?


    .... right. However sizeofx doesn't compile and if (x) was considered an
    expression it should be sizeof (x), and sizeof(x) shouldn't compile.
     
    Ioannis Vranos, Mar 30, 2008
    #6
  7. On Sun, 30 Mar 2008 23:07:05 +0300, Ioannis Vranos wrote:
    > Harald van Dijk wrote:
    >> On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
    >>> In C99 it is mentioned:
    >>>
    >>> "The sizeof operator yields the size (in bytes) of its operand, which
    >>> may be an expression or the parenthesized name of a type.".
    >>>
    >>> If I am not wrong, this implies that
    >>>
    >>> int x;
    >>>
    >>> size_t y= sizeof(x);
    >>>
    >>>
    >>> is not valid.

    >>
    >> You are; (x) is a perfectly valid expression, so there's no problem
    >> taking the size of (x).

    >
    > I first saw that only sizeof x is valid at the pdf hosted at
    > http://cprog.tomsweb.net.


    That doesn't say sizeof(x) is invalid any more than the standard does.

    > Then I checked the C99 standard and it mentions what is shown above.
    > Clearly C99 doesn't mention parenthesized expression.


    Yes, it does. Look at the grammar.

    unary-expression:
    sizeof unary-expression

    unary-expression:
    postfix-expression

    postfix-expression:
    primary-expression

    primary-expression:
    ( expression )

    A parenthesised expression is a primary-expression, which is a postfix-
    expression, which is a unary-expression, which is a valid operand of
    sizeof.

    The standard doesn't explicitly mention that parenthesised expressions
    are valid operands of +, -, *, /, ^, &, or pretty much any other
    operator. The grammar makes that clear already.
     
    Harald van Dijk, Mar 30, 2008
    #7
  8. On Sun, 30 Mar 2008 23:11:55 +0300, Ioannis Vranos wrote:
    > Ian Collins wrote:
    >> Ioannis Vranos wrote:
    >>> Ioannis Vranos wrote:
    >>>> I first saw that only sizeof x is valid at the pdf hosted at
    >>>> http://cprog.tomsweb.net.
    >>>
    >>> More specifically the above writes:
    >>>
    >>> "sizeof Returns size of operand in bytes; two forms:
    >>> 1) sizeof(type)
    >>> 2) sizeof expression"
    >>>
    >>>

    >> Did you read what Harald said: "(x) is a perfectly valid expression"?

    >
    > ... right. However sizeofx doesn't compile and if (x) was considered an
    > expression it should be sizeof (x), and sizeof(x) shouldn't compile.


    sizeofx doesn't compile because there's no sizeof operator. There's a
    sizeofx identifier. sizeof(x) does and should compile because sizeof( is
    not a valid identifier. This is the same reason why a+++++b is not valid,
    but a+++--b is: the first is split into {a}{++}{++}{+}{b}, and the second
    is split into {a}{++}{+}{--}{b}.
     
    Harald van Dijk, Mar 30, 2008
    #8
  9. Ioannis Vranos

    Eric Sosman Guest

    Ioannis Vranos wrote:
    > Ian Collins wrote:
    >> Ioannis Vranos wrote:
    >>> Ioannis Vranos wrote:
    >>>> I first saw that only sizeof x is valid at the pdf hosted at
    >>>> http://cprog.tomsweb.net.
    >>> More specifically the above writes:
    >>>
    >>> "sizeof Returns size of operand in bytes; two forms:
    >>> 1) sizeof(type)
    >>> 2) sizeof expression"
    >>>
    >>>

    >> Did you read what Harald said: "(x) is a perfectly valid expression"?

    >
    > ... right. However sizeofx doesn't compile and if (x) was considered an
    > expression it should be sizeof (x), and sizeof(x) shouldn't compile.


    Try sizeof(((((((((((((((((((((x)))))))))))))))))))))
    and perhaps the answer will come to you.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Mar 30, 2008
    #9
  10. Eric Sosman wrote:
    > Ioannis Vranos wrote:
    >> Ian Collins wrote:
    >>> Ioannis Vranos wrote:
    >>>> Ioannis Vranos wrote:
    >>>>> I first saw that only sizeof x is valid at the pdf hosted at
    >>>>> http://cprog.tomsweb.net.
    >>>> More specifically the above writes:
    >>>>
    >>>> "sizeof Returns size of operand in bytes; two forms:
    >>>> 1) sizeof(type)
    >>>> 2) sizeof expression"
    >>>>
    >>>>
    >>> Did you read what Harald said: "(x) is a perfectly valid expression"?

    >>
    >> ... right. However sizeofx doesn't compile and if (x) was considered an
    >> expression it should be sizeof (x), and sizeof(x) shouldn't compile.

    >
    > Try sizeof(((((((((((((((((((((x)))))))))))))))))))))
    > and perhaps the answer will come to you.



    Yes, it had already arrived. :) Thanks anyway.
     
    Ioannis Vranos, Mar 30, 2008
    #10
  11. Ioannis Vranos <> writes:
    > Ian Collins wrote:
    >> Ioannis Vranos wrote:
    >>> Ioannis Vranos wrote:
    >>>> I first saw that only sizeof x is valid at the pdf hosted at
    >>>> http://cprog.tomsweb.net.
    >>>
    >>> More specifically the above writes:
    >>>
    >>> "sizeof Returns size of operand in bytes; two forms:
    >>> 1) sizeof(type)
    >>> 2) sizeof expression"
    >>>
    >>>

    >> Did you read what Harald said: "(x) is a perfectly valid expression"?

    >
    > ... right. However sizeofx doesn't compile and if (x) was considered an
    > expression it should be sizeof (x), and sizeof(x) shouldn't compile.


    C code is split into tokens before those tokens are parsed. (The
    process actually involves "preprocessor tokens", which are later
    converted to "tokens", but that doesn't matter in this case.)

    ``sizeof'' is a keyword; like all keywords, it has the form of an
    identifier. ``('' and ``)'' are punctuators. Two adjacent
    identifiers, keywords, or numeric constants must be separated by
    whitespace (a comment counts as whitespace). A keyword and a
    punctuator don't need any whitespace to separate them.

    ``sizeofx'' is just a single identifier that has nothing to do with
    the ``sizeof'' keyword. It compiles just fine if you happen to have
    declared it:

    int sizeofx = 42;
    sizeofx;

    ``sizeof x'' is two tokens, ``sizeof'' and ``x''. If ``x'' is an
    expression (actually a unary-expression; see the grammar), then that's
    a legal expression.

    ``sizeof(x)'' is four tokens, ``sizeof'', ``('', ``x'', and ``)''. If
    ``x'' is a type-name, then that's a valid expression. If ``x'' is an
    expression, then ``(x)'' is also a valid expression, and the whole
    thing is also a valid expression.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 30, 2008
    #11
  12. Ioannis Vranos

    Kaz Kylheku Guest

    On Mar 30, 1:11 pm, Ioannis Vranos <>
    wrote:
    > expression it should be sizeof (x), and sizeof(x) shouldn't compile.


    Good grief!

    Do you realize that children that were in junior highschool back when
    you first started being a goof here now have CS degrees and software
    jobs?

    You need to disable your anti-learning filter, or at least reduce the
    coefficients in its confusion matrix a little bit.
     
    Kaz Kylheku, Mar 31, 2008
    #12
  13. In article <fsort9$26if$>,
    Ioannis Vranos <> wrote:
    >Then I checked the C99 standard and it mentions what is shown above.
    >Clearly C99 doesn't mention parenthesized expression.


    A parenthesized expression is an expression, (that's a fact about C
    grammar, not the English language). sizeof(x) is no more a problem
    than a[(x)].

    -- Richard
    --
    :wq
     
    Richard Tobin, Mar 31, 2008
    #13
    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,389
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    655
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,480
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    573
    Joe Smith
    May 22, 2006
  5. Alex Vinokur
    Replies:
    7
    Views:
    510
    Clark S. Cox III
    Aug 14, 2006
Loading...

Share This Page