31

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

codaddict
  • 445,704
  • 82
  • 492
  • 529
yCalleecharan
  • 4,656
  • 11
  • 56
  • 86
  • Possible duplicate of [What's the difference between size\_t and int in C++?](http://stackoverflow.com/questions/502856/whats-the-difference-between-size-t-and-int-in-c) – Jim Fell Apr 04 '17 at 12:42

3 Answers3

35

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
  • 67,862
  • 21
  • 134
  • 209
  • 1
    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 ` anyway.) – Chris Lutz Apr 09 '10 at 05:21
  • 1
    He also mentioned "the preprocessor directive stddef.h", so I gave it the benefit of doubt. ;-) – DevSolar Apr 09 '10 at 05:23
  • 1
    @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 Apr 09 '10 at 08:40
  • @ Chris, standard headers are NOT allowed to include each other. – DevSolar Apr 09 '10 at 11:03
  • @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 ` with something built into the compiler. It is, as the c standard beautifily puts it, implementation defined. – Joe D Aug 12 '10 at 22:38
  • 1
    @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.. GitHub STOP HELPING ICE Aug 13 '10 at 06:50
  • @Joe D: Including `` 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 Aug 13 '10 at 07:10
  • C99 (N1256), C11 (N1570) and C17 (N2176) section 7.21.1 all state that `stdio.h` declares `size_t`. But they don't say that it *defines* `size_t`. Is there any language loophole where a header can declare a POD type without simultaneously defining it? It would be very unusual for such an implementation to exist, so I realise this is just a corner case. – AJM Apr 08 '21 at 09:42
  • 1
    @AJM-Reinstate-Monica 6.7 Declarations (5): A *definition* for an identifier is a declaration for that identifier that, [...] for a typedef, is the first (or only) declaration of that identifier. – DevSolar Apr 08 '21 at 19:56
12

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
  • 445,704
  • 82
  • 492
  • 529
3

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
  • 333,147
  • 50
  • 533
  • 760
  • 1
    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 Apr 09 '10 at 05:41
  • 1
    Actually, it's like driving without a brake. – Mark Adler Dec 04 '16 at 05:34
  • C99 (N1256), C11 (N1570) and C17 (N2176) section 7.21.1 all confirm that `stdio.h` declares `size_t`. But they don't explicitly say that it *defines* `size_t`. Is there any language loophole where a header can declare a POD type without simultaneously defining it? – AJM Apr 08 '21 at 09:40