N3485
Literal Suffixes for size_t

Published Proposal,

Previous Revisions:
N2998 (r1) N2961 (r0)
Author:
Latest:
https://thephd.dev/_vendor/future_cxx/papers/C - Literal Suffixes for size_t.html
Paper Source:
github.com/ThePhD/future_cxx
Issue Tracking:
GitHub
Proposal Category:
Feature Request
Target:
C2y
Project:
ISO/IEC 9899 Programming Languages — C, ISO/IEC JTC1/SC22/WG14

Abstract

C++ has suffixed literals for size_t and its signed counterpart. This paper gives C the same ability.

1. Changelog

1.1. Revision 2 - February 20th, 2025

1.2. Revision 1 - June 17th, 2022

1.3. Revision 0 - April 12th, 2022

2. Design

This is a (minor) compatibility issue with literal suffixes that may be potentially used in shared code in the future. C++ adopted suffixed literals for size_t in [p0330]. The design is as follows:

#include <stddef.h>

size_t ulit_val = 0zu;

The u, as with current literals, can be placed on either the left or the right of the z suffix to make it unsigned. For symmetry with existing suffix literal rules, it also has a signed variant. That type is the "signed integer type of size_t", which normally resolves to ptrdiff_t:

#include <stddef.h>

ptrdiff_t lit_val = 0z;

The signed variant lacks the u as a piece of the z suffix. This also matches the way printf adjusts specific codes to display size_t or ptrdiff_t-sized variables. The design is simple and, thankfully, compatible with C++. It also provides a way to avoid signed comparison warnings in compilers which implement more strict comparisons checks, e.g., when comparing a size_t value against some fixed constant value.

2.1. Even After Compatibility, Do We Really Need This?

Yes. The trip to put this paper into C++ was an extremely long one and came with a ton of reasons. All of it can be seen in [p0330] and 90% of that reasoning applies to C, especially in the face of _Generic.

3. Wording

Wording is relative to the latest working draft.

3.1. Intent

The goal of this wording is to provide:

3.2. Specification

3.2.1. Add two new grammar productions to §6.4.5.2 Integer literals, Syntax, ¶1

6.4.5.2 Integer literals
Syntax

integer-suffix:
unsigned-suffix long-suffixopt
unsigned-suffix long-long-suffix
unsigned-suffix size-suffix
long-suffix unsigned-suffixopt
long-long-suffix unsigned-suffixopt
size-suffix unsigned-suffixopt

long-long-suffix: one of
ll LL
size-suffix: one of
z Z

3.2.2. Add two new table rows to §6.4.5.2 Integer literals¸ Table 6.2

Table 6.2 — Relationship between constants, suffixes, and types
Suffix Decimal Literal Octal, Binary, or Hexadecimal Literal
z or Z the corresponding signed integer type of size_t (7.21) the corresponding signed integer type of size_t (7.21)
Both u or U and z or Z size_t (7.21) size_t (7.21)

If an integer literal that does not have suffixes wb, WB, uwb, or UWB wb, WB, z, or Z in its integer suffix cannot be represented by any type in its list, it may have an extended integer type, if the extended integer type can represent its value. If all the types in the list for the literal are signed, the extended integer type shall be signed. If all the types in the list for the literal are unsigned, the extended integer type shall be unsigned. If the list contains both signed and unsigned types, the extended integer type may be signed or unsigned. If an integer literal cannot be represented by any type in its list and has no extended integer type, then the integer literal has no type.

NOTE     While an integer literal 64800z has the signed integer type corresponding to size_t, such a type itself may not be suitable for representing the complete range for sizes (i.e. [0, SIZE_MAX)).

Forward references: preprocessing numbers (6.4.8), numeric conversion functions (7.22.1) , common definitions <stddef.h> (7.21) .

References

Informative References

[P0330]
JeanHeyd Meneide; Rein Halbersma. Literal Suffixes for ptrdiff_t and size_t. November 4th, 2019. URL: https://wg21.link/p0330