What is the library stddef.h for?

1

I am doing some functions in a program that I have already started, and I see that it has in the header:

#include <stddef.h>
#include <stdio.h>

And I do not understand why to include the library stddef.h since I searched in google what the library contributes is the constant NULL for the leading type variables (among other things but that I do not use in the program).

When using pointers I use NULL many times.

But it turns out that if I remove that inclusion the program works correctly anyway. As if the library stdio.h already gave me the constant of NULL likewise.

What do you think it can be?

I can not show code and the one that passed me the program can not answer me.

    
asked by Edu 05.04.2018 в 03:03
source

2 answers

3

Header <stddef.h> .

According to the documentation that can be consulted in cppreference, the header <stddef.h> provides the following:

Before C11 :
  • size_t : aliases of type ( typedef ) to a integral type without sign. Used as operator return sizeof .
  • ptrdiff_t type alias ( typedef ) to a integral type with sign. It is used as a result of pointer arithmetic.
  • NULL a constant defined as macro whose value is implementation dependent and is the value of a null pointer .
  • offsetof function macro that calculates the offset between the start of a structure and a member of that structure.
After C11 additionally provides :

Differences between headers and headers .

C ++ adapts many of the C libraries to its own idiosyncrasies, classifying functions in namespaces or transforming some functions into templates, when a C library has been adapted to C ++ its file receives a c as a prefix and it removes the extension. You should use the specific header of each language, so if you are programming in C ++ instead of using <stddef.h> you should use <cstddef> , read this thread to know more about this topic.

  

When using pointers I use NULL many times.

     

But it turns out that if I remove that inclusion the program works correctly anyway. As if the library stdio.h already gave me the constant of NULL likewise.

     

What do you think it can be?

Headers can include other headers. Other headers that you are using could include <stddef.h> and therefore you would be receiving NULL . For example, if we consult the GCC implementation of <stdio.h> , we see that one of the first things you do is, effectively, include <stddef.h> :

/*
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *      @(#)stdio.h     5.3 (Berkeley) 3/15/86
 */

/*
 * NB: to fit things in six character monocase externals, the
 * stdio code uses the prefix '__s' for stdio objects, typically
 * followed by a three-character attempt at a mnemonic.
 */

#ifndef _STDIO_H_
#ifdef __cplusplus
extern "C" {
#endif
#define _STDIO_H_

#define _FSTDIO                 /* ''function stdio'' */

#define __need_size_t
#include <stddef.h>
    
answered by 05.04.2018 / 11:09
source
2

The header sttdef.h defines several standard definitions (types and macros) ). Many of these definitions also appear in other headers, for example, in stdio.h , and this is why you do not see modifications in your code by not including sttdef.h. This header also defines (as indicated by here ), in addition to the macro NULL , the variables:

typedef ptrdiff_t

typedef size_t

typedef wchar_t

in the link that I left you define each variable and also show an example of using one of the macros (offsetof).

Maybe they are looking for you to make a similar use somewhere in your program, but if you say everything works, then it is not necessary to include the header.

    
answered by 05.04.2018 в 04:21