Hovestar Hovestar - 5 months ago 22
C Question

What type is NULL?

I'm wondering what type Null is in C. This is probably a duplicate, but I kept getting information about void type on searches. Maybe a better way is can NULL be returned for any type function? For example:

int main(){
return NULL;

Does that work?

Answer Source

Per C 2011 7.19 3, NULL “expands to an implementation-defined null pointer constant” (when any of several headers have been included: <locale.h>, <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, or <wchar.h>).

Per 3, a null pointer constant is “[A]n integer constant expression with the value 0, or such an expression cast to a type void *.” Thus, the C implementation may define NULL as simply 0 or as ((void *) 0), for example. Note that the former is an int, not a pointer, but it may be compared to pointers, as in if (p == NULL) ….

Generally, NULL is intended to be used for pointers, not for int values.

In most C implementations, converting NULL to an integer will yield zero. However, I do not see this guaranteed in the C standard. (E.g., I do not see a guarantee that (int) (void *) 0 == 0.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download