Using sizeof without parentheses

Discussion in 'C Programming' started by Chris McDonald, Apr 10, 2006.

  1. I've been trying to wean myself off using parentheses after the sizeof
    operator (and after the return keyword, too), but my understanding is
    challenged by the 4th use of sizeof in the following code:

    #include <stdio.h>

    struct {
    int i;
    char *p;
    } var;

    typedef struct {
    int i;
    char *p;
    } TYPE;

    int main(int argc, char *argv[])
    {
    printf("1: %d\n", sizeof var);
    printf("2: %d\n", sizeof(var));

    printf("3: %d\n", sizeof TYPE);
    printf("4: %d\n", sizeof(TYPE));

    return 0;
    }

    I understand that (TYPE) is not a value, so is the 3rd use of sizeof a
    sort of 'special case' of what sizeof permits?
    (gcc v4.0.1 and earlier, just to make this possibly OT)

    A bit confused; Thanks,

    --
    Chris.
     
    Chris McDonald, Apr 10, 2006
    #1
    1. Advertisements

  2. Chris McDonald wrote:
    > I've been trying to wean myself off using parentheses after the sizeof
    > operator (and after the return keyword, too), but my understanding is
    > challenged by the 4th use of sizeof in the following code:
    >
    > #include <stdio.h>
    >
    > struct {
    > int i;
    > char *p;
    > } var;
    >
    > typedef struct {
    > int i;
    > char *p;
    > } TYPE;
    >
    > int main(int argc, char *argv[])
    > {
    > printf("1: %d\n", sizeof var);


    This is fine.

    > printf("2: %d\n", sizeof(var));


    No parenthesis required. Even if you opt for them, it's misleading to
    "glue" them to `sizeof`.

    > printf("3: %d\n", sizeof TYPE);


    This is not allowed, as `TYPE` is a type and parenthesis are required.

    If your compiler didn't pick this up, it's either broken, or you need
    to turn the warning/error settings to a higher level.

    > printf("4: %d\n", sizeof(TYPE));


    This is as it should be.

    > return 0;
    > }
    >
    > I understand that (TYPE) is not a value, so is the 3rd use of sizeof a
    > sort of 'special case' of what sizeof permits?
    > (gcc v4.0.1 and earlier, just to make this possibly OT)


    See above. (GCC may allow it as an extension, but it is OT.)
     
    Vladimir S. Oka, Apr 10, 2006
    #2
    1. Advertisements

  3. "Vladimir S. Oka" <> writes:

    >Chris McDonald wrote:
    >> printf("3: %d\n", sizeof TYPE);


    >This is not allowed, as `TYPE` is a type and parenthesis are required.


    >If your compiler didn't pick this up, it's either broken, or you need
    >to turn the warning/error settings to a higher level.


    >> printf("4: %d\n", sizeof(TYPE));


    >This is as it should be.



    Thanks Vladimir; gcc seems to be doing fine reporting #3 as an error,
    even without any additional flags.

    But I'm a little confused as to why if "... `TYPE` is a type"
    that by placing parenthesis around the type name, that it's now OK?

    --
    Chris.
     
    Chris McDonald, Apr 10, 2006
    #3
  4. Chris McDonald

    Eric Sosman Guest

    Chris McDonald wrote:

    > I've been trying to wean myself off using parentheses after the sizeof
    > operator (and after the return keyword, too), but my understanding is
    > challenged by the 4th use of sizeof in the following code:
    >
    > #include <stdio.h>
    >
    > struct {
    > int i;
    > char *p;
    > } var;
    >
    > typedef struct {
    > int i;
    > char *p;
    > } TYPE;
    >
    > int main(int argc, char *argv[])
    > {
    > printf("1: %d\n", sizeof var);
    > printf("2: %d\n", sizeof(var));
    >
    > printf("3: %d\n", sizeof TYPE);
    > printf("4: %d\n", sizeof(TYPE));
    >
    > return 0;
    > }
    >
    > I understand that (TYPE) is not a value, so is the 3rd use of sizeof a
    > sort of 'special case' of what sizeof permits?
    > (gcc v4.0.1 and earlier, just to make this possibly OT)
    >
    > A bit confused; Thanks,


    The third use is an error for which the compiler must
    issue a diagnostic. It is not some kind of special case
    for sizeof; the construct simply doesn't work at all. (If
    it works for your compiler, you are probably running the
    compiler in a "C-with-extras" mode; gcc is famous/notorious
    for creative departures from Standard C. Try using the
    command-line options "-W -Wall -ansi -pedantic" or similar.)

    By the way, "%d" is an incorrect conversion specifier for
    the result of sizeof. "%d" must match an ordinary signed int,
    but sizeof produces a size_t. The precise makeup of size_t
    varies from one compiler to another, but it is certainly not
    a signed value. I have used compilers where all of the examples
    you show (except #3, which doesn't compile) would print zero.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Apr 10, 2006
    #4
  5. Chris McDonald wrote:
    > "Vladimir S. Oka" <> writes:
    >
    > >Chris McDonald wrote:
    > >> printf("3: %d\n", sizeof TYPE);

    >
    > >This is not allowed, as `TYPE` is a type and parenthesis are required.

    >
    > >If your compiler didn't pick this up, it's either broken, or you need
    > >to turn the warning/error settings to a higher level.

    >
    > >> printf("4: %d\n", sizeof(TYPE));

    >
    > >This is as it should be.

    >
    >
    > Thanks Vladimir; gcc seems to be doing fine reporting #3 as an error,
    > even without any additional flags.
    >
    > But I'm a little confused as to why if "... `TYPE` is a type"
    > that by placing parenthesis around the type name, that it's now OK?


    C99
    6.5.3.4.2
    The sizeof operator yields the size (in bytes) of its operand,
    which may be an expression or the parenthesized name
    of a type.
     
    Vladimir S. Oka, Apr 10, 2006
    #5
  6. "Vladimir S. Oka" <> writes:

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


    Thanks;

    --
    Chris.
     
    Chris McDonald, Apr 10, 2006
    #6
    1. Advertisements

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,781
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    953
    CBFalconer
    Apr 10, 2004
  3. Kamilche
    Replies:
    11
    Views:
    1,982
    Nick Coghlan
    Jan 8, 2005
  4. Jerry He

    functions without parentheses

    Jerry He, Jul 28, 2005, in forum: Python
    Replies:
    15
    Views:
    821
    Dan Sommers
    Jul 31, 2005
  5. Manish_Ganvir
    Replies:
    13
    Views:
    1,962
    Keith Thompson
    Feb 14, 2005
  6. Vinu
    Replies:
    13
    Views:
    1,972
    Lawrence Kirby
    May 12, 2005
  7. John Goche
    Replies:
    3
    Views:
    356
    Marcus Kwok
    Sep 14, 2006
  8. Chris Seberino
    Replies:
    8
    Views:
    334
    Chris Seberino
    Jan 4, 2014
Loading...