What is the nullable type in C.

Nullable type - Nullable type

Nullable types are a feature of some programming languages ​​with which the value is based on the special value ZERO can be set instead of the usual possible values ​​of the data type. In statically typed languages, a nullable type is an option type, while in dynamically typed languages ​​(where values ​​have types but variables do not), equivalent behavior is provided by a single null value.

A common use of NULL to represent a missing or invalid value, such as: B. from a function that could not be returned or a missing field in a database, as in NULL in SQL.

Primitive types such as integers and Boolean values ​​cannot generally be null, but the corresponding nullable types (nullable integers or nullable Boolean values) can also take the NULL value.


An integer variable can represent integers, but 0 (zero) is a special case because 0 can mean "false" in many programming languages. Nor does this give us any idea that the variable is empty, which is required in many circumstances. This need can be met with a nullable type. In programming languages ​​such as C # 2.0, for example, a nullable integer can be declared using a question mark (int? X). In programming languages ​​such as C # 1.0, nullable types can be defined as new types by an external library (e.g. NullableInteger, NullableBoolean).

A Boolean variable makes the effect clearer. Its values ​​can be either "true" or "false", while a nullable Boolean value can also contain a representation for "undecided". However, the interpretation or treatment of a logical operation involving such a variable depends on the language.

Compared to zero pointers

In contrast, object pointers can be set to NULL by default in most popular languages, which means that the pointers or reference points are nowhere that no object is assigned (the variable does not point to an object). Nullable references were invented by CAR Hoare in 1965 as part of the Algol W language. Hoare later described his invention as a "billion dollar mistake". This is because object pointers, which can be NULL, require the user to check the pointer before it is used and specific code is required to handle the case when the object pointer is NULL.

Java has classes that correspond to scalar values, such as: B. Integer, Boolean and Float. In combination with autoboxing (automatic usage-controlled conversion between object and value), nullable variables can be used effectively for scalar values ​​in this way.

Compared to option types

Nullable implementations usually adhere to the null object pattern.

There is a more general and formal concept that extends the concept of the nullable type. It comes from option types that force the exception to be handled explicitly. Option-type implementations usually follow the special case pattern.

Language support

The following programming languages ​​support nullable types.

Statically typed languages ​​with native null support include:

Statically typed languages ​​with library null support include:

Dynamically typed languages ​​with zero include:

  • Perl scalar variables are set to by default and can be set to.
  • PHP with NULL type and is_null () method, native nullable type in version 7.1
  • Python has the value.
  • Julia has the value (which is of the type) and the type language.
  • Ruby with null value and NilClass type.
  • JavaScript has value

See also