stddef.h in C

The file header named stddef.h in C Library defines various types of variables and macros. Many of these definitions are also present in other headers.

The file header named stddef.h in C Library defines various types of variables and macros. Many of these definitions are also present in other headers.

The variables are defined in stddef.h

The following lists some types of variables defined in stddef.h in Library C:

Description variable ptrdiff_t

This is a signed integer type and is the result of subtraction of two pointers

size_t

This is an unsigned integer type and the result of the sizeof keyword

wchar_t

This is an integer type, the size of a large character constant (wide char)

Macros are defined in stddef.h

The table below lists some macros defined in stddef.h in Library C:

Macro Description NULL

This macro is the value of a null pointer constant

offsetof (type, member-designator)

These results are in an integer constant of type size_t which is the offset (byte unit) of a structure member from the beginning of that structure. Members are provided by the member-designator parameter, and the name of the structure provided by type

1. Macro NULL () in C

The NULL macro in Library C is the value of a null pointer constant. It can be defined as ((void *) 0), 0 or 0L depending on the Compiler.

Macro declaration NULL () in C

The following may be the declaration for NULL Macro depending on the compiler.

 #define NULL (( char *) 0 ) ho ặ c #define NULL 0L ho ặ c #define NULL 0 

Parameters

This macro does not receive any parameters

Returns the value

This macro does not return any values

For example

The following C program illustrates how to use NULL Macro.

 #include #include int main () { FILE * fp ; fp = fopen ( "nofile.txt" , "r" ); if ( fp == NULL ) { printf ( "Khong the mo nofile.txt !!!n" ); } return ( 0 ); } 

Assuming we want to open a non-existent nofile.txt, when compiling and running the above C program will result:

stddef.h in C Picture 1stddef.h in C Picture 1

2. The offsetof macro () in C

The offsetof (type, member-designator) macro in standard C Library results in an integer constant of type size_t which is the offset (byte unit) of a structure member from the beginning of that structure. Members are provided by member-designator, and the name of the structure provided by type.

Offof () Macro declaration in C

Below is the declaration for offsetof () Macro.

 offsetof ( type , member - designator ) 

Parameters

type - This is the type of class where member-designator is a valid identifier.

member-designator - This is the member name of the class type.

Returns the value

This macro returns the value of type size_t which is the member offset in the type.

For example

The following C program illustrates the use of offsetof () Macro.

 #include #include struct student { char name [ 50 ]; char school [ 50 ]; int rollno ; }; int main () { printf ( "name offset = %d byte trong student struct.n" , offsetof ( struct student , name )); printf ( "class offset = %d byte trong student struct.n" , offsetof ( struct student , school )); printf ( "rollno offset = %d byte trong student struct.n" , offsetof ( struct student , rollno )); return ( 0 ); } 

Compiling and running the above C program will result:

stddef.h in C Picture 2stddef.h in C Picture 2

According to Tutorialspoint

Last lesson: stdarg.h in C

Next lesson: stdio.h in C

4 ★ | 2 Vote