tags:

views:

206

answers:

3

Hi,

if I'm using the sizeof operator and making use of size_t in my code, do I have necessarily have to include stddef.h? I haven't included stddef.h, and my code compiles without warning with both MVS2008 and with Borland C++ BuilderX.

Thanks a lot...

+3  A: 

sizeof(), while looking like a function call, is actually an operator and part of the language core. No include needed.

size_t is defined in various headers: stddef.h, string.h, stdlib.h, and stdio.h. Including any one of them is enough to use size_t in your code.

DevSolar
He mentioned in the answer "the `sizeof` operator," so he already knows that much. (And `string.h`, `stdlib.h`, and `stdio.h` probably just `#include <stddef.h>` anyway.)
Chris Lutz
He also mentioned "the preprocessor directive stddef.h", so I gave it the benefit of doubt. ;-)
DevSolar
Thanks. I have stdlib.h and stdio.h in my code.
yCalleecharan
@Chris, stddef.h defines offsetof which may not defined by other headers in a conformant implementation, so they can't just include stddef.h.
AProgrammer
@ Chris, standard headers are NOT allowed to include each other.
DevSolar
@DevSolar, they are allowed to include each other as long as it appears that they don't, stdio.h can include stddef.h to define size_t but nothing else and allowing stddef.h to be included again.The standard headers don't even have to be files, the preprocessor can just replace `#include <stdio.h>` with something built into the compiler. It is, as the c standard beautifily puts it, implementation defined.
Joe D
@Joe, while you're correct, that's a horribly ugly implementation full of preprocessor conditionals that become impossible for laypeople to read. I.e. it's what the glibc headers look like. A much saner implementation is to put all the types that might be defined by more than one header in a special `__types.h` file with clean logic for defining the requested types if they haven't already been defined, and nothing else.
R..
@Joe D: Including `<stddef.h>` puts those identifiers into the namespace that are specified by the standard to be defined in that header - which is not allowed for other standard headers. I don't care much if some braindead implementation decides to hack around it, but that's the letter of the standard.
DevSolar
+4  A: 

No, you can include a header which in turn includes stddef.h

The size_t definition shall be provided to a referencing piece of code by including stdlib.h header file. In fact most implementations don't have it defined literally in this file but instead do sub include the file stddef.h as for example the standard library of the GNU C compiler does. The direct inclusion of stddef.h for application code is totally valid and thus can replace stdlib.h in cases where no other members from this file are needed or desired.

Source

codaddict
Thanks. I have stdlib.h and stdio.h in my code.
yCalleecharan
+1  A: 

In c the definition for size_t comes from one of several headers: stddef.h, stdio.h, stdlib.h, string.h, time.h or wchar.h.

There are any number of ways that the compiler implementation can arrange for this, but note that one way that can't be used is by having the compiler include one of these headers for you behind your back - that's not something a C compiler is permitted to do (this restriction was lifted for C++, which is allowed to include any of the standard headers for its own purposes).

Michael Burr
Yes C is a strict language. I don't remember where I read that, but it says that C is like driving without an airbag. Java for instance has many error checking mechanisms and it's like driving with an airbag. But to learn the art programming well, I believe that driving without airbag gives one character.
yCalleecharan