sizeof(struct emp) = 0

Discussion in 'C Programming' started by sophia.agnes@gmail.com, Feb 10, 2008.

  1. Guest

    Dear all,

    why the following program is giving o/p as

    sizeof(struct emp) = 0

    #include<stdio.h>
    #include<stdlib.h>

    struct emp
    { };

    int main(void)
    {
    printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

    puts ("");
    return (EXIT_SUCCESS);
    }

    i have seen this same code giving o/p as 1 in other compilers
     
    , Feb 10, 2008
    #1
    1. Advertising

  2. On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
    > Dear all,
    >
    > why the following program is giving o/p as
    >
    > sizeof(struct emp) = 0
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    >
    > struct emp
    > { };


    This isn't valid C. C requires at least one member in every structure.
    That said, ...

    > i have seen this same code giving o/p as 1 in other compilers


    ....why would you expect any bytes to be required to store nothing?
    Shouldn't you be asking why other compilers make sizeof(struct emp)
    anything other than zero? Or do you already know why?
     
    Harald van Dijk, Feb 10, 2008
    #2
    1. Advertising

  3. wrote:
    > Dear all,
    >
    > why the following program is giving o/p as
    >
    > sizeof(struct emp) = 0
    >
    > #include<stdio.h>
    > #include<stdlib.h>

    Some white space might improve readability

    >
    > struct emp
    > { };

    Gives me an error "expected a declaration"

    > int main(void)
    > {
    > printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

    sizeof() returns size_t, %d expects int.

    >
    > puts ("");

    Not needed if the printf string would end with a \n

    > return (EXIT_SUCCESS);

    () not needed, return is not a function

    > }
    >
    > i have seen this same code giving o/p as 1 in other compilers

    Which compiler and where they called in conforming mode? Conforming to which
    version of the standard?

    Bye, Jojo
     
    Joachim Schmitz, Feb 10, 2008
    #3
  4. <> schreef in bericht
    news:...
    > Dear all,
    >
    > why the following program is giving o/p as
    >
    > sizeof(struct emp) = 0
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    >
    > struct emp
    > { };
    >
    > int main(void)
    > {
    > printf("\n sizeof(struct emp) = %d", sizeof(struct emp));
    >
    > puts ("");
    > return (EXIT_SUCCESS);
    > }
    >
    > i have seen this same code giving o/p as 1 in other compilers


    empty structs is illegal C
    It is legal C++ though, there the size of an empty struct is always 1. Maybe
    you were confused with that?
     
    Serve Laurijssen, Feb 10, 2008
    #4
  5. Harald van Dijk <> writes:
    > On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
    >> Dear all,
    >>
    >> why the following program is giving o/p as
    >>
    >> sizeof(struct emp) = 0
    >>
    >> #include<stdio.h>
    >> #include<stdlib.h>
    >>
    >> struct emp
    >> { };

    >
    > This isn't valid C. C requires at least one member in every structure.
    > That said, ...
    >
    >> i have seen this same code giving o/p as 1 in other compilers

    >
    > ...why would you expect any bytes to be required to store nothing?
    > Shouldn't you be asking why other compilers make sizeof(struct emp)
    > anything other than zero? Or do you already know why?


    Padding.

    --
    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, Feb 10, 2008
    #5
  6. On Sun, 10 Feb 2008 12:36:05 -0800, Keith Thompson wrote:
    > Harald van Dijk <> writes:
    >> On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
    >>> Dear all,
    >>>
    >>> why the following program is giving o/p as
    >>>
    >>> sizeof(struct emp) = 0
    >>>
    >>> #include<stdio.h>
    >>> #include<stdlib.h>
    >>>
    >>> struct emp
    >>> { };

    >>
    >> This isn't valid C. C requires at least one member in every structure.
    >> That said, ...
    >>
    >>> i have seen this same code giving o/p as 1 in other compilers

    >>
    >> ...why would you expect any bytes to be required to store nothing?
    >> Shouldn't you be asking why other compilers make sizeof(struct emp)
    >> anything other than zero? Or do you already know why?

    >
    > Padding.


    In C, padding is not allowed at the immediate start of a structure.

    More seriously, padding is used to ensure the structure's members are
    properly aligned, and there's no problem with alignment of an empty
    structure's members. There's also no problem with an array of zero-byte
    objects. They all have the same address, so they are all identically
    aligned.

    Of course, the fact that they all have the same address is why a certain
    other language places extra requirements on empty structures, but
    extensions by any C compiler don't have to follow rules for other
    languages.
     
    Harald van Dijk, Feb 10, 2008
    #6
  7. Jack Klein Guest

    On Sun, 10 Feb 2008 11:53:57 +0100, "Serve Laurijssen" <>
    wrote in comp.lang.c:

    >
    > <> schreef in bericht
    > news:...
    > > Dear all,
    > >
    > > why the following program is giving o/p as
    > >
    > > sizeof(struct emp) = 0
    > >
    > > #include<stdio.h>
    > > #include<stdlib.h>
    > >
    > > struct emp
    > > { };
    > >
    > > int main(void)
    > > {
    > > printf("\n sizeof(struct emp) = %d", sizeof(struct emp));
    > >
    > > puts ("");
    > > return (EXIT_SUCCESS);
    > > }
    > >
    > > i have seen this same code giving o/p as 1 in other compilers

    >
    > empty structs is illegal C
    > It is legal C++ though, there the size of an empty struct is always 1. Maybe
    > you were confused with that?


    <off-topic>

    The size of an empty struct in C++ is greater than 0. There is
    neither requirement nor guarantee that it be exactly equal to 1.

    </off-topic>

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Feb 11, 2008
    #7
  8. On Feb 10, 2:30 pm, wrote:
    > Dear all,
    >
    > why the following program is giving o/p as
    >
    >  sizeof(struct emp) =  0
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    >
    > struct emp
    > { };


    Not allowed in C .
    There may be unnamed padding within a structure object, but not at its
    beginning.
    There may be unnamed padding at the end of a structure or union.
    Refer -> http://c0x.coding-guidelines.com/6.7.2.1.html

    >
    > int main(void)
    > {
    >   printf("\n sizeof(struct emp) =  %d", sizeof(struct emp));
    >
    >   puts ("");
    >   return (EXIT_SUCCESS);
    >
    > }
    >
    > i have seen this same code giving o/p as 1 in other compilers
     
    karthikbalaguru, Feb 17, 2008
    #8
    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,341
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    636
    CBFalconer
    Apr 10, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  4. Vinu
    Replies:
    13
    Views:
    1,432
    Lawrence Kirby
    May 12, 2005
  5. blufox

    sizeof( int ) != sizeof( void * )

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

Share This Page