Logo
Audiobook Image

What's New in the C23 Standard for C Programming?

July 17th, 2024

00:00

Play

00:00

Star 1Star 2Star 3Star 4Star 5

Summary

  • C23 to replace C17, initiated as C2x in 2016, expected in 2024
  • New functions like memset_explicit(), memccpy(), bit utility enhancements
  • Syntax closer to C++, includes C++ style attributes, new directives
  • Removal of trigraphs, K&R declarations, certain integer types
  • GCC, Clang, Pelles C support C23 with experimental flags

Sources

The C23 standard, formally identified as ISO/IEC 9899:2024, represents the forthcoming protocol for the C programming language, set to supersede the C17 standard. Initiated informally as C2x in the year twenty-sixteen, its development has been a focal point of anticipation with its expected finalization and publication slated for twenty-twenty-four. The journey to this new standard began with the first Working Group fourteen, or WG14, meeting in October of twenty-nineteen which marked the commencement of drafting efforts. Subsequently, the global pandemic necessitated a shift to virtual and remote meetings throughout twenty-twenty, continuing with various teleconferences up to twenty-twenty-four. Despite the high hopes and meticulous planning, a significant twist emerged. On July fourteenth, twenty-twenty-four, the ongoing revisions and inability to finalize the edits before the publication deadline led to an unprecedented outcome. The International Organization for Standardization, ISO, was unable to approve the final draft of C23 on time. Consequently, this resulted in the automatic cancellation of the C23 designation, briefly rendering the C language without a current standard. This unexpected turn of events underscores the complex and challenging nature of developing and ratifying new standards in the programming world, reflecting both the intricate technical demands and the crucial collaborative efforts required across the global community of developers and experts. As the C programming community anticipated the integration of new features and enhancements that C23 promised, this development certainly marked a notable moment in the continuing evolution of this foundational programming language. Despite the initial setbacks in the standardization process, the development of C23 included several proposed enhancements and new features aimed at modernizing the C programming language and expanding its functionality. One of the significant additions was a suite of new functions in the standard library, designed to provide developers with more tools and streamlined options for common programming tasks. The introduction of the `memset_explicit()` function in the `less thanstring.hgreater than` library is one such feature. This function is specifically designed to erase sensitive data from memory, ensuring that the memory store operation is always performed, irrespective of compiler optimizations. This is crucial for applications where security and data privacy are paramount, providing a reliable way to clear sensitive information. Another addition to the `less thanstring.hgreater than` library is the `memccpy()` function, which enables efficient concatenation of strings. This function is modeled after similar extensions in POSIX and SVID C, enhancing the C standard librarys capabilities in handling string operations more effectively. The standard library also saw the introduction of `strdup()` and `strndup()` functions, facilitating the allocation of memory for duplicates of strings, which is a common requirement in many applications. Moreover, the `memalignment()` function was added to the `less thanstdlib.hgreater than` library to aid in determining the byte alignment of pointers, an essential aspect of programming that deals with performance optimization and system compatibility. In addition to these utility functions, C23 was set to introduce a comprehensive set of bit manipulation functions housed within a new header, `less thanstdbit.hgreater than`. These functions, prefixed with `stdc_` to minimize conflicts with existing code, include operations like `stdc_count_ones()` and `stdc_count_zeros()` for counting the number of one or zero bits in a value, respectively. Other functions like `stdc_leading_ones()`, `stdc_trailing_zeros()`, and `stdc_bit_width()` provide detailed bit-level analysis capabilities, which are invaluable in embedded systems programming, cryptography, and other fields requiring precise control over data at the bit level. The enhancements were not limited to functions alone; significant improvements in data type handling and preprocessor directives were also part of the C23 draft. These enhancements included the introduction of new directives such as `#elifdef` and `#elifndef`, making the preprocessor more powerful and flexible. These additions align with similar features in the C++23 standard, showcasing an ongoing effort to harmonize C with C++, thus easing cross-language development and portability. Collectively, these proposed additions and enhancements in the C23 standard aimed to equip the C programming language with modern capabilities, addressing the evolving needs of software development while maintaining the efficiency and performance that C is known for. As the community awaited the resolution of the standardization process, these features highlighted the potential for significant advancements in how developers could utilize C in various applications. The evolution of the C23 standard also focused on enhancing compatibility with C++, aiming to bridge some of the gaps that have traditionally existed between these two predominant programming languages. This alignment not only facilitates easier code integration and migration between C and C++ but also aids developers who operate in mixed-language environments. One of the most notable compatibility enhancements in C23 is the adoption of C++ style attributes using double square brackets, such as `[[deprecated]]`, `[[nodiscard]]`, and `[[maybe_unused]]`. These attributes provide additional metadata about the code that can help the compiler to optimize or issue warnings, thereby making the code more robust and maintainable. The inclusion of these attributes in C23 mirrors their usage in C++, fostering a more unified approach to coding practices across the two languages. Furthering this goal, C23 introduced new preprocessing directives, specifically `#elifdef` and `#elifndef`, which simplify conditional compilation by combining the functionality of `#if defined` and `#if !defined` into more concise forms. This addition not only streamlines the preprocessor syntax but also aligns with recent updates in the C++ standard, making it easier for developers to work across both languages with fewer modifications to conditional compilation logic. Changes to function declarations also mark a significant step towards better compatibility with C++. In C23, functions that are declared without any parameters, using an empty set of parentheses, are understood to take no arguments. This change eliminates ambiguities associated with older C-style function declarations and aligns with the C++ approach, where an empty parameter list explicitly denotes a function that accepts no arguments. This modification aids in reducing common sources of bugs and misunderstandings when migrating functions between C and C++ or when linking code written in both languages. These compatibility enhancements in C23 not only strengthen the syntactical and functional ties between C and C++ but also underscore a broader trend in programming language development. By fostering greater interoperability between these core languages, the updates aim to enhance developer productivity, encourage code reuse, and support more seamless integration of legacy C code into modern C++ applications. As such, these changes are critical for developers who need to maintain and evolve codebases that leverage the strengths of both C and C++. As the C23 standard aimed to modernize and streamline the C programming language, certain features that were deemed obsolete or less useful in contemporary programming practices were slated for deprecation or removal. This process of refinement is crucial not only for simplifying the language and making it more efficient but also for avoiding potential security risks and maintenance challenges associated with outdated constructs. One of the more significant changes proposed in the C23 standard was the removal of trigraphs. Trigraphs were originally introduced to allow characters not directly available on all keyboards to be represented in source code, but with the widespread adoption of Unicode and more standardized keyboard layouts, the need for trigraphs has dramatically diminished. Removing them simplifies the languages parsing phase and eliminates a source of confusion and errors for programmers, as trigraphs could be mistakenly used within string literals or comments, leading to unexpected results. Another outdated feature set for removal was the Kernighan and Ritchie (KandR) style function declarations. This older style of function declaration, which does not require declaring the types of parameters, has long been superseded by the more explicit ANSI C function prototypes that require full type declarations. The removal of KandR function declarations from C23 aligns with modern programming practices that emphasize code clarity and type safety, thus reducing the likelihood of type-related errors and enhancing the overall robustness of the code. C23 also proposed the removal of certain integer representations, specifically mandating the use of twos complement for representing signed integers. Twos complement is universally supported across modern computing architectures and provides a consistent behavior for signed integer arithmetic, which simplifies cross-platform development and optimizes performance. By standardizing on twos complement, C23 eliminates the complications that arise from supporting multiple integer representations, such as ones complement or signed magnitude, which are rarely used in practice today. The rationale behind these removals is grounded in the ongoing efforts to adapt the C language to contemporary programming environments, where simplicity, security, and cross-platform consistency are highly valued. By shedding these obsolete features, C23 not only reduces the languages complexity but also lowers the barrier to entry for new programmers, making C more accessible and maintainable. These changes reflect a broader recognition within the programming community that language evolution must balance backward compatibility with the need to embrace modern standards and practices. This approach ensures that C remains a powerful tool for system-level programming, embedded systems, and other applications where efficiency and control over hardware are critical. The adoption of any new programming standard critically depends on support from widely-used compilers, as these are the tools that translate high-level code into executable programs. For C23, mainstream compilers such as GCC, Clang, and Pelles C have been instrumental in testing and implementing the new features and changes proposed in the standard. Each of these compilers has introduced experimental flags specifically designed to enable features of the C23 standard, allowing developers to test and provide feedback on these features in real-world scenarios. GCC, one of the most popular open-source compilers, introduced specific compiler flags that enable experimental support for C23, allowing developers to compile their programs using the new standard and test its compatibility and performance. Similarly, Clang, known for its emphasis on offering robust support for multiple language standards, has also provided flags to experiment with C23 features. Pelles C, primarily used in Windows environments, has followed suit, ensuring that developers working in different operating systems have the opportunity to leverage the new capabilities of C23. The introduction of these experimental flags is significant not only for testing and refining the features of C23 but also for encouraging early adoption among the community. By experimenting with these features, developers can uncover potential issues and provide feedback, which is crucial for the iterative process of standardization. This collaborative effort between compiler developers and the programming community helps ensure that the final standard is robust, practical, and well-supported across different platforms and environments. Looking towards the future, the impact of C23 on the programming community is expected to be substantial. As the standard introduces several enhancements aimed at improving security, compatibility with C++, and overall language efficiency, it is poised to enhance the way developers write, maintain, and optimize C code. The focus on removing obsolete features and standardizing modern practices is likely to make C programming more accessible to new generations of programmers while still retaining the powerful capabilities that have made C the language of choice for system programming, embedded systems, and performance-critical applications. Moreover, as the boundaries between C and C++ become less distinct due to compatibility improvements, the ease of integrating and migrating code between these two languages could lead to more cohesive and flexible development environments. This could foster innovation and efficiency, particularly in complex, multi-language projects. As compiler support continues to evolve and the standard becomes fully ratified and adopted, the programming community will likely witness a reinvigorated interest in C, reaffirming its place as a foundational technology in the software development landscape.