What type is NULL?

I am wondering what type of Null is in C. This is probably a duplicate, but I kept getting information about the void type when searching. Maybe the best way is to return NULL for any type function? For instance:

int main(){
    dosomething();
    return NULL;
}

It works?

+3
source share
2 answers

Per C 2011 7.19 3 NULL"expands to a null pointer constant, determined implementation" (when you turn on any one of several titles: <locale.h>, <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>or <wchar.h>).

6.3.2.3 3 "[A] n 0 , void *." , C NULL 0 ((void *) 0), . , int, , , if (p == NULL) โ€ฆ.

, NULL , int.

C NULL . C. (, , (int) (void *) 0 == 0.)

+7

NULL?

: NULL - void* int, long, unsigned,...


@Eric Postpischil , .

() NULL, , . C11dr ยง7.19 3

0 , void *, . ยง6.3.2.3 3

NULL , " ". . NULL .

// Poor code as NULL may not match the type of the specifier - undefined behavior
printf("%p\n", NULL); // poor
printf("%d\n", NULL); // poor

// Better
printf("%d\n", (int) NULL);

// Best.  NULL is best used in a pointer context, print as a pointer
printf("%p\n", (void *) NULL);
0

All Articles