sizeof(struct emp) = 0

S

sophia.agnes

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
 
H

Harald van Dijk

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?
 
J

Joachim Schmitz

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
 
S

Serve Laurijssen

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?
 
K

Keith Thompson

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


...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.
 
H

Harald van Dijk


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.
 
J

Jack Klein

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top