Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What header defines NULL in C++?

Tags:

c++

null

According to C++ primer, <cstdlib> header defines NULL. cpluspplus says it is defined in <cstddef>.

Ultimately, if the right header is not included, I thought NULL can't be referenced.

From what i can see, however it can be referenced and produce programs and that compile and run without warnings or errors, after including only <iostream>

Please help me understand this.

like image 563
James Leonard Avatar asked Aug 19 '12 01:08

James Leonard


People also ask

How do you define null in C?

Null is a built-in constant that has a value of zero. It is the same as the character 0 used to terminate strings in C. Null can also be the value of a pointer, which is the same as zero unless the CPU supports a special bit pattern for a null pointer.

Is null in Stdio H?

Strictly speaking, NULL expands to an implementation-defined null pointer constant which is defined in many header files such as “stdio. h”, “stddef. h”, “stdlib.

Does C have a null keyword?

Several programming languages make use of the concept of null. Go has nil , JavaScript has null , Python has None , and so on. C has NULL . NULL however is used differently from other languages.

Where is null defined in GCC?

The C standard requires that NULL be defined in locale. h , stddef. h , stdio.


1 Answers

The C standard requires that NULL be defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.

The C++ standard requires that NULL be defined in the c* header corresponding to each of those.

The C standard is very strict about the names a standard can define--each standard header must define precisely the names the standard requires that header to define. The only other names it can define are those that are reserved for the implementation, such as those starting with an underscore followed by another underscore or a capital letter.

The C++ standard is much more permissive in this respect--including any one standard header can have the same effect as including any or all other standard headers.

From a practical viewpoint, C++ implementations used to take quite a bit of advantage of this permissiveness--that is, including one standard header frequently defined the names from a number of other standard headers. More recent implementations tend to work more like the C standard requires, staying much closer to each header defining only the names required by to be defined by that header. They're still probably not as strict about it as the C standard requires, but much closer than they used to be (as a rule).

like image 157
Jerry Coffin Avatar answered Sep 26 '22 12:09

Jerry Coffin