GCC 13 Supports New C2x Features, Including nullptr, Enhanced Enumerations, and More

MMS Founder
MMS Sergio De Simone

Article originally posted on InfoQ. Visit InfoQ

The latest release of GCC, GCC 13.1, adds support for a number of new features and improvements to the C language defined in the upcoming C2x standard, including nullptr, enhanced enumerations, auto type inference, and more.

C2x is adopting a number of changes to the language that first made their appearance in C++, to make it easier for the two languages to coexist in the same codebase. Such changes include nullptr, enhanced enumerations, auto type inference, constexpr specifier, and others.

The new nullptr constant will attempt to remove one of the issues with the old NULL macro, whose definition is implementation-dependent. This leads to a number of potential shortcomings when using it with type-generic macros, with conditional expression such as (true ? 0 : NULL) (since NULL could be defined as (void*)0), with vararg functions where NULL is used as a sentinel, and in other cases.

To improve this, nullptr will have its own type nullptr_t; will not have a scalar value, so it cannot be used in arithmetic expressions; will be convertible to any pointer type; and will always convert to a boolean with false value.

Enhanced enumerations is another change to the language that tries to fix a case of under-specification of the previous standard, in this case concerning the underlying type associated to enums. In fact, while C normally uses int for enumerations, this is not always the case, which can cause portability issues. Additionally, when ints are effectively used, this rules out the possibility of using enumerations at bitfield level. The new standard thus allows developers to state the enumeration type, as in the following example:

enum a : unsigned long long {
    a0 = 0xFFFFFFFFFFFFFFFFULL
    // ^ not a constraint violation with a 64-bit unsigned long long
};

C2x also tries to make it away with some restrictions to variadic functions that are considered leaky or dictated by outdated assumptions concerning their requirements on the argument list. Specifically, the new standard is going to permit that a function declaration’s parameter list may just consist of an ellipsis not preceded by a named argument, e.g. int a_variadic_function(...). This change is considered safe as it does not break any existing code.

Another new C feature inspired by C++ is the use of the auto keyword to leave the definition of a variable type implicit. This is possible when the definition includes an initializer from which the variable type can be derived. It is worth noting that C auto is a much more limited feature than C++ auto, which relies on C++ template type deduction rules, and can be used less generally.

The introduction of constexpr in C2x responds to the goal of improving initialization of object with static storage duration, which require the use of constant expressions or macros evaluating to constant expressions. Thanks to this, the following definition, valid in C17:

static size_t const bignum = 0x100000000;

can be replaced by

constexpr size_t bignum = 0x100000000;

which has the added property of ensuring the constant is checked at compile time.

Other new C2x features that are supported by GCC 13 are storage-class specifiers for compound literals, typeof and typeof_unqual, the noreturn attribute, support for empty initializer braces, removal of trigraphs and unprototyped functions, the adoption of the Unicode Identifier and Pattern Syntax, and more.

C2x is the upcoming C language standard revision, expected to be approved this year, thus getting the official C23 denomination.

About the Author

Subscribe for MMS Newsletter

By signing up, you will receive updates about our latest information.

  • This field is for validation purposes and should be left unchanged.