# NULL

From cppreference.com
< c‎ | types

Defined in header `<stddef.h>` |  |
Defined in header `<string.h>` |  |
Defined in header `<wchar.h>` |  |
Defined in header `<time.h>` |  |
Defined in header `<locale.h>` |  |
Defined in header `<stdio.h>` |  |
Defined in header `<stdlib.h>` |  |
#define NULL /*implementation-defined*/ |  |

The macro `NULL` is an implementation-defined null pointer constant, which may be

- an integer constant expression with the value ​0​
- an integer constant expression with the value 0 cast to the type void*

A null pointer constant may be converted to any pointer type; such conversion results in the null pointer value of that type.

### Possible implementation

    // C++ compatible:
    #define NULL 0
    // C++ incompatible:
    #define NULL (10*2 - 20)
    #define NULL ((void*)0)

### Example

Run this code

    #include <stdlib.h>
    #include <stdio.h>
    int main(void)
    {
        // any kind of pointer can be set to NULL
        int* p = NULL;
        struct S *s = NULL;
        void(*f)(int, double) = NULL;

        // many pointer-returning functions use null pointers to indicate error
        char *ptr = malloc(10);
        if (ptr == NULL) printf("Out of memory");
        free(ptr);
    }

### See also

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/types/NULL&oldid=96432"