7

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(){
    dosomething();
    return NULL;
}

Does that work?

Hovestar
  • 1,544
  • 4
  • 18
  • 26
  • http://www.cplusplus.com/reference/cstring/NULL/ – Robert Harvey Feb 04 '14 at 17:28
  • `int` - it's in the signature – w.b Feb 04 '14 at 17:29
  • 1
    `NULL` doesn't exist in C. You're probably using something like `#define NULL 0`, which should answer your question. – SLaks Feb 04 '14 at 17:29
  • 6
    In C it's usually a `void *`, i.e. `#define NULL ((void *)0)`. In C++ it's just `0`. – Paul R Feb 04 '14 at 17:30
  • @PaulR Can I return a void then in an int function? – Hovestar Feb 04 '14 at 17:30
  • 1
    `int` is not a pointer. – Robert Harvey Feb 04 '14 at 17:31
  • @Hovestar: You mean a `void *` ? That should generate a warning, at least (pointer to integer conversion) - you do have warnings enabled, I hope ? (`gcc -Wall ...` if not). – Paul R Feb 04 '14 at 17:31
  • @PaulR I guess I'll just refactor so I don't need to do it. But yes I have warnings enabled – Hovestar Feb 04 '14 at 17:33
  • Why don't you simply return zero? – Robert Harvey Feb 04 '14 at 17:37
  • @RobertHarvey I'm actually using a struct in my code, but I wanted a more general answer. – Hovestar Feb 04 '14 at 18:10
  • NULL is a mnemonic for invalid pointer value. It is silly ( unless you habituate those weird language lawyer fetish tags ) to consider it has a non-zero representation. It should only be used where an address is expected, and it clarifies the code. – mevets Apr 05 '21 at 22:52
  • @mevets *It is silly ( unless you habituate those weird language lawyer fetish tags ) to consider it has a non-zero representation.* Nah, I habituate to code that's not based on unguaranteed assumptions so my customer's production system doesn't crash because I assumed something false. If you want to be silly and aim for lower standards in your code, that's on you. You are going to introduce bugs into your code. Why do ***anything*** that even might increase the number of bugs? Especially when you know it's a false assumption. Aim higher. – Andrew Henle Apr 05 '21 at 23:09

2 Answers2

14

The type of NULL may be either an integer type or void *. This is because the C standard allows it to be defined as either an integer constant expression or the result of a cast to void *.

C 2018 7.19 3 says 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>).

C 6.3.2.3 3 says a null pointer constant is “An integer constant expression with the value 0, or such an expression cast to a type void *.”

Thus, a C implementation may define NULL as, for example:

  • 0, which has type int,
  • ((void *) 0), which has type void *, or
  • (1+5-6), which is an integer constant expression with value 0 and type int.

Even though NULL may have an integer type, it may be compared to and assigned to pointers, as in if (p == NULL) …. The rules for these operations say that an integer constant zero will be converted to the appropriate pointer type for the operation.

Although NULL may be defined to be 0, it is intended to be used for pointers, not as an integer zero. Programs should avoid doing that, and C implementations are generally better off defining it as ((void *) 0) to help avoid mistakes where it might be accepted as an integer value.

In most C implementations, converting NULL to an integer will yield zero. However, this is not guaranteed in the C standard. It is allowed that (int) NULL or (uintptr_t) NULL will produce the address of some special “do not use” location rather than zero. Even (int) (void *) 0 might produce such an address rather than zero.

When an integer constant zero is converted to a pointer type, it is treated specially by the C implementation; it produces a null pointer for that implementation even if its null pointer uses an address other than zero. The fact that it is an integer constant means the compiler can apply this special treatment where it recognizes the conversion in the source code. If we have some non-constant expression, such as an int variable x, then (void *) x is not guaranteed to yield a null pointer even if the value of x is zero.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Please note that this _does not guarantee_ the cast to `void *`. – Brave Sir Robin Feb 04 '14 at 17:46
  • `stddef.h` or the other standard headers where the macro is defined. – ouah Feb 04 '14 at 18:06
  • Curious, then, if I have a function that checks whether or not an input `s` is an empty string, can I be reasonably assured that `if (s && strlen(s) == 0) return true;` (assuming I have access to `strlen`) won't return true if s somehow happens to be NULL? Or is it necessary to first check explicitly if `s != NULL`? – Blake Schwartz Jun 04 '18 at 02:46
  • @BlakeSchwartz: If `s` is `NULL`, `s && strlen(s)` evaluates to false, and `strlen` is not evaluated. There is no need for a preliminary test. – Eric Postpischil Jun 04 '18 at 15:11
2

What type is NULL?

Short answer: The type of NULL is void* or int, long, unsigned, ...


Addition to @Eric Postpischil fine answer to point out more coding pitfalls.

(macro) NULL which expands to an implementation-defined null pointer constant. C11dr §7.19 3

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. §6.3.2.3 3

The type of NULL has many possibilities given "integer constant". Portable code should not assume a particular type. NULL is best used in pointer contexts.

// 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);
onlycparra
  • 607
  • 4
  • 22
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256