Hi,
i tired the following code in
c & c++

int main()
{
   struct dum
   {
    // nothing here
   }
printf("%d",sizeof(struct dum));
return 0;
}

the out put is 0

int main()
{
   struct dum
   {
    // nothing here
   }
cout<<sizeof(struct dum);
return 0;
}

the out put is 1.
i dont understand why is it so?
any idea?

A struct with no members is not legal C. Your compiler should not allow it. C++ allows empty struct definitions, but all objects must have a size, so it will always be at least 1.

//#define CPP 1 //if want to check for cpp
#define C   1
#if(CPP)
        using namespace std;
        #include<iostream>

#endif
#if(C)
        #include<stdio.h>
#endif
int main()
{
        struct stu
        {
             //empty structure no members
        };
#if(CPP)
        cout<<sizeof(struct stu);
#endif

#if(C)
        printf("%d",sizeof(struct stu));
#endif

return 0;
}

A struct with no members is not legal C. Your compiler should not allow it.

But my compiler is allowing it. I am not getting any errors, not even warnings(even if i use gcc -Wall test.c).

i am uisng gcc compiler.

i checked with the turbo compiler there cpp version is working but c version is giving error "sizeof type is unknown or zero" at the declaration of structure.

so can i say it is compiler dependent.

Edited 7 Years Ago by Gaiety: added extra contents.

I am not getting any errors, not even warnings(even if i use gcc -Wall test.c).

Empty structs are an extension in gcc. If you compile with switches that force standard compliance, you should at least get a warning:

gcc -Wall -ansi -pedantic test.c

Empty structs are an extension in gcc. If you compile with switches that force standard compliance, you should at least get a warning:

gcc -Wall -ansi -pedantic test.c

ya i got the warning with all these on.
"struct has no members"

thank you very much TOM.

by the way, i dont know what is -pedantic mean
i know
-Wall - enable all warnings
-ansi - i think with compliance to ansi standards
but the latest compilers are ISO do we still need to stick to
ansi

i dont know what is -pedantic mean

Here is a quote from the online manual for gcc:

-pedantic

Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.

i think with compliance to ansi standards
but the latest compilers are ISO do we still need to stick to
ansi

Gcc probably uses -ansi as a historic artifact, but it really does mean ISO C90 and not ANSI C89:

-ansi

In C mode, support all ISO C90 programs. In C++ mode, remove GNU extensions that conflict with ISO C++.

Functionally there is not really much difference between ANSI C89 and ISO C90. Alternatively, you can use -std= to get the same effect and also for C99 conformance:

// ISO C90
gcc -Wall -std=c89 -pedantic test.c

// ISO C99
gcc -Wall -std=c99 -pedantic test.c

There are a bunch of other options. You can read about them here.

This article has been dead for over six months. Start a new discussion instead.