Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I always include stddef.h if I use sizeof and size_t

Tags:

c

sizeof

size-t

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

like image 923
yCalleecharan Avatar asked Apr 09 '10 05:04

yCalleecharan


People also ask

What is Stddef h used for?

h is a header file in the standard library of the C programming language that defines the macros NULL and offsetof as well as the types ptrdiff_t, wchar_t, and size_t.

What header file is sizeof in?

The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type. The result for either kind of operand is not an lvalue, but a constant integer value. The type of the result is the unsigned integral type size_t defined in the header file stddef. h .

Which header file contains sizeof in C?

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

Is Size_t the word size?

No; size_t is not necessarily whatever you mean by 'the word size' of the machine that will run the code (in the case of cross-compilation) or that compiled the code (in the normal case where the code will run on the same type of machine that compiled the code).


2 Answers

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

like image 32
codaddict Avatar answered Sep 21 '22 10:09

codaddict


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.

like image 131
DevSolar Avatar answered Sep 23 '22 10:09

DevSolar