iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😸

The Next C Standard (C23) Has Apparently Been Finalized

に公開
2024/05/10

I briefly introduced C23 in Modern C and the Next C Standard (C23) as well.

Now that the contents to be included in C23 have mostly been finalized, I would like to introduce them again.

The latest publicly available Working Draft at the time of writing is N2912 N3047 N3054 N3096. However, there is also the first draft for C2y, N3220, which might be closer to the actual content.

I am also referring to posts from meeting participants regarding the details:

So, here are the main features coming to C23:

Main features in C23

  • Incorporation of POSIX features: strdup, strndup, memccpy, gmtime_r, localtime_r

  • Incorporation of C++ features:

    • Attributes with [[]]: In the standard, there are 8 (effectively 7): [[nodiscard]], [[maybe_unused]], [[deprecated]], [[fallthrough]], [[noreturn]], [[_Noreturn]], [[reproducible]], [[unsequenced]]. Additionally, vendor-specific ones like [[vendor::attr]] can be used depending on the implementation.
    • char8_t (unlike C++, this is a typedef of unsigned char)
    • u8 character literals (Note: u8 string literals were already introduced in C11)
    • Predefined bool, true, false, static_assert, alignof, alignas, thread_local
    • Single-argument static_assert
    • auto (N3007)
    • Constants via constexpr. Not applicable to functions. (N3018)
    • enum with a specified underlying type (N3021)
    • nullptr (N3019)
    • Digit separators
    • Binary literals
    • Initialization with = {} (N2900)
  • Floating-point numbers

    • Support for IEEE 754-2019. Previously, it referenced IEEE 754-1985.
    • Parts of TS 18661-1, TS 18661-2, and TS 18661-4 have been incorporated into the main text, and TS 18661-3 (such as the _FloatN types) has been incorporated as Annex H (the previous Annex H was Language Independent Arithmetic).
    • New pragmas include #pragma STDC FENV_ROUND, #pragma STDC FENV_DEC_ROUND, etc.
  • Removal of K&R style function definitions

    • Also, if the parameter list of a function declaration without a definition is empty, it will now represent "the function takes no arguments" (N2841).
  • Ability to define variadic functions without mandatory arguments (N2975)

    • The second argument of va_start becomes optional.
    • In C++, defining variadic functions without mandatory arguments has been possible and used in things like SFINAE, but since va_start could not be used, the arguments could not be accessed.
  • Two's complement representation is now mandatory

  • #elifdef, #elifndef

  • #embed (N3017)

    • Allows for embedding files.
  • #warning

  • typeof (N2927)

    • C++'s decltype depends on reference types, so it couldn't be brought over to C.
    • The reason for not adding underscores to the keyword might be that various compilers already provide the typeof keyword and probably nobody is using it as a variable name (just a guess).
    • In N2927, remove_quals was introduced alongside typeof. On the other hand, there was a proposal (N2930) to use a name like unqual_typeof instead of remove_quals—so which one did it end up being? typeof_unqual is included in addition to typeof.
  • unreachable() (N2826)

    • Similar to GCC/Clang's __builtin_unreachable(), MSVC's __assume(false), or C++23's std::unreachable(). Useful for certain optimizations.
  • _BitInt

  • memset_explicit

    • Previously, memset_s in Annex K was similar, but the arguments are slightly different.
  • Variable-length arrays that are not allocated on the stack (like pointers or arguments) are promoted to mandatory features.

  • Integer operations with overflow checking: ckd_add, ckd_sub, ckd_mul in <stdckdint.h> (N2683)

  • __has_include

  • Bitwise operations such as popcount, clz, and ctz in <stdbit.h>: stdc_count_ones, stdc_leading_zeros, stdc_trailing_zeros, etc. (part of N3022)

There isn't enough time to explain each feature in detail here. I have written articles about some of them:

For some other features, here are some articles by yohhoy:

Articles by members of the standardization committee are also informative:

It seems that a summary of the feature list is also available on cppreference.

GCC Support Status

In recent versions of GCC, some features can be used with -std=c2x (and also with -std=c23 starting from GCC 14).

In GCC 10:

  • Attributes with [[]]: Standard [[deprecated]], [[fallthrough]], [[maybe_unused]], and [[gnu::something]]
  • UTF-8 character literals: u8''

and others were implemented. In GCC 11:

  • true and false defined in <stdbool.h> now have the bool type
  • [[nodiscard]] attribute
  • __has_c_attribute

and others were added. In GCC 12:

  • Digit separators
  • #elifdef, #elifndef

and others were added. In GCC 13:

  • nullptr
  • Extensions to enum
  • auto
  • constexpr
  • typeof, typeof_unqual
  • Keywording of alignas, alignof, bool, false, static_assert, thread_local, true
  • [[noreturn]] attribute
  • = {}
  • unreachable()
  • Removal of functions without prototypes

and others were added. In GCC 14:

  • _BitInt
  • Multiple definitions of compatible structs, unions, and enums
  • <stdckdint.h>

among other features have been implemented.

Clang Support Status

Clang also seems to support some of the features of C23 starting from version 9 with -std=c2x.


This article is updated from time to time. Please see GitHub for the change history.

Discussion