Welcome C23

The next C standard is established and it’s slowly being implemented. If you have the latest version of the C compiler installed on your system, you can take advantage of some of the fun and improved features of the C programming language.

I plan on covering some C23 features over the next few Lessons. Some of the features are available when using the current C standard (C17), providing you employ a few workarounds.

For example, C23 adds the reserved words bool, true, and false.

The bool keyword identifies a Boolean data type, which is a value of either one and zero.

The true and false keywords identify TRUE and FALSE values, where TRUE is any non-zero value and FALSE is zero. Yes, these are technically bool constants.

These new keywords are a great addition to C, and something that other programming languages have naturally. As keywords, bool, true, and false may cause some issues where older C code may use them as identifiers. Still, I still believe them to be a great and necessary addition to the language.

If you want to use these words in your code now, all just add the stdbool.h header to your code, as shown in the following example.

2023_12_16-Lesson-a.c

#include <stdio.h>
#include <stdbool.h>

int main()
{
    bool b = 1;

    if( b==true )
        puts("B is true");
    else
        puts("B is false");

    return(0);
}

This code uses the bool and true keywords, though they’re defined in the stdbool.h header file. As such, this code compiles under the current version of C. Here is the output:

B is true

If you remove the #include directive (or comment it out), the compiler generates some errors:

1216b.c:5:2: error: use of undeclared identifier ‘bool’
        bool b = 1;
        ^
1216b.c:7:6: error: use of undeclared identifier ‘b’
        if( b==true )
            ^
1216b.c:7:9: error: use of undeclared identifier ‘true’
        if( b==true )
               ^
3 errors generated.

But if you compile with C23 features activated, the code builds.

To get those C23 features, or at least some of them, ensure that you have the latest C compiler installed. On my system, I obtained the clang-15 update, which is the most recent stable release available for my distro. According to the LLVM website, we may have to wait until clang-18 until most of the C23 features are implemented. Still, enough of these new C language features are available that you can try them out, as I did with the following code and clang-15:

2023_12_16-Lesson-b.c

#include <stdio.h>

int main()
{
    bool b = 1;

    if( b==true )
        puts("B is true");
    else
        puts("B is false");

    return(0);
}

Yes, this is the same program as before, minus including the stdbool.h header. I used the following command to build the program:

clang-15 -Wall -std=c2x 2023_12_16-Lesson-b.c

I need to type clang-15 as the system version of clang is mapped to clang-14. (I could change it, but meh.) The -Wall switch activates all warnings. The -std=c2x switch directs the compiler to use the C23 standard, as much of it as is implemented.

The program builds with no warnings or errors, which tells me that for clang-15 and when using the -std=c2x switch, I can work with some C23 features. Here’s the output:

B is true

In next week’s Lesson, I describe how to write code that identifies the C23 compiler version and to implement code that takes advantage of some of its features.

Leave a Reply