Questions tagged [c23]
C23 is the informal name of the next standard of the C programming language. It replaces C17 and introduces some new features.
66
questions
2
votes
1
answer
59
views
Using previously set fields when initializing structure with compound literals
What is the expected behavior of referring to previously set fields, when declaring initial values for a struct using compound literal: Specifically is it ok to: struct foo v = { .v1 = ..., .v2 = .v1+...
1
vote
3
answers
137
views
In C language, are objects that are not explicitly initialized necessarily implicitly initialized?
The question I'm going to ask might be quite long. If possible, please read it through patiently.
First of all, let's take a look at the content of §6.7.9 Initialization in C11:
¶8:
An initializer ...
17
votes
0
answers
319
views
realloc(ptr, 0) in C23 - now what?
According to the change log of C23, proposal n2464 was voted-in and implemented, making realloc(ptr, 0) explicitly undefined behavior, whereas it was supposedly implementation-defined in previous ...
4
votes
0
answers
94
views
Is there such a thing as "function attribute correctness"?
Ok so C23 adds function attributes to the language but I'm not sure if anyone actually considered how they are supposed to be used. Consider this example:
[[deprecated]] int foo () { return 0; }
[[...
1
vote
0
answers
66
views
Semantics of functions taking a QChar * when passed a void * or const void *
This is a follow-up to: What is the new QChar* in C23?, where @Lundin showed a sample implementation of a function that takes
a QChar * with _Generic, after which I implemented strchrnul() like so:
#...
1
vote
1
answer
113
views
what's the problem of passing parameters to void function in c?
In the C programming language, when you want to declare a function without parameters, you write its signature like this:
int foo(void);
When you want to declare a function with unspecified ...
2
votes
1
answer
177
views
error: must specify at least one argument for '...' parameter of variadic macro
You can define a variadic macro in C like:
#define F(x, ...) f(x, __VA_ARGS__)
But calling F as F(a) results in the macro expansion f(a,) which would not compile.
So C23 includes __VA_OPT__ to ...
1
vote
1
answer
62
views
Detecting _BitInt type in _Generic
From ISO/IEC 9899:2024, 6.2.5 Types:
...
5 A bit-precise signed integer type is designated as _BitInt(N) where N is an integer constant expression
that specifies the number of bits that are used to ...
4
votes
1
answer
111
views
Check if array is a VLA at compile-time
@Lundin shows how to check if a passed expression is an array at compile-time here: Lvalue conversion of _Generic controlling expression involving array clang warning wrong?:
#define IS_ARRAY(T) ...
2
votes
1
answer
90
views
Can the expanded code of an inline function differ between 2 translation units?
Say I have a macro UNIQUE_NAME(PREFIX) that concatenates __LINE__ to PREFIX:
#define CONCAT2_EXPAND(a, b) a ## b
#define CONCAT2(a, b) CONCAT2_EXPAND(a, b)
#define UNIQUE_NAME(prefix) ...
3
votes
1
answer
82
views
How and why use call_once()?
From N3220, §7.28.2.1 “The call_once function”:
Synopsis
#include <threads.h>
void call_once(once_flag *flag, void (*func)(void));
Description
The call_once function uses the
once_flag pointed ...
2
votes
2
answers
154
views
What is the __STDC_VERSION__ value for C23?
What is the STDC_VERSION value for C11? asks about C11, and How can I use "nullptr" for null pointers prior to C23? says that the current versions of GCC and Clang define it to a placeholder ...
3
votes
3
answers
150
views
What are source and execution character sets?
I was looking at the changes in C23, and found this in Annex M of the C23 draft:
added @ (U+0040, COMMERCIAL AT), $ (U+0024, DOLLAR SIGN), and ` (U+0060, GRAVE ACCENT, "Backtick") into the ...
2
votes
0
answers
98
views
Why are there sequence point guarantees for C library functions?
I am looking at the C23 draft standard, but I think this would apply in C11 as well. There are several guarantees about sequence points in relation to function calls in C, such as before the return of ...
4
votes
1
answer
125
views
What is the new QChar* in C23?
cppreference shows these prototypes for strchr():
char *strchr( const char *str, int ch ); (1)
/*QChar*/ *strchr( /*QChar*/ *str, int ch ); (2) (since C23)
and offers this explanation for the ...
0
votes
0
answers
46
views
Restrictions on auto keyword for type inference in C2X
int main()
{
auto status = 203;
auto static const str = "Hello";
auto static const strs = {"Hello", "World"};
return status;
}
From playing around with ...
3
votes
1
answer
53
views
What's an implementation required to do upon seeing unknown attributes?
C2X has introduced "Attribute specifier sequence", which can be of these forms:
1) standard attribute, such as [[fallthrough]]
2) attribute with a namespace, such as [[gnu::unused]]
3) ...
2
votes
2
answers
108
views
error: constexpr pointer initializer is not null
I have this very simple program:
int main(void)
{
static constexpr const char *const str = "hello";
}
which fails to compile with this error:
$ gcc -std=c2x str.c
str.c: In function '...
0
votes
2
answers
87
views
Rationale for allowing unnamed parameters in function definitions
From Annex M of this document:
— parameter names may be omitted in function definitions;
One use I see for this is not having to cast anything to void, or using an unused attribute. But was that the ...
0
votes
1
answer
69
views
Finding the size of a file with C23 embed
If I do:
static char const file[] = {
#embed "this.c"
};
And then:
static char const copy[sizeof file];
I assume sizeof file would be a compile-time constant and would be the actual size ...
0
votes
1
answer
52
views
Expressing a pointer to a const function
In C, I can express a pointer to a const function like this:
typedef void fun_t(void);
const fun_t *fp;
(I don't know if the type of fp has any practical use, but it is a real and distinct type; e.g.,...
6
votes
1
answer
128
views
Can you declare a C pointer with its own address?
Can I declare a C pointer with an intialization to its own address?
void* p = &p;
I am specifically concerned with if this is strictly standard compliant in C23 (as the draft currently stands).
I ...
0
votes
1
answer
107
views
What new types and operations can take place in a constexpr in C23?
C23 added a keyword constexpr which can be used to define objects as compile-time constants instead of enumerations or macros. For example:
constexpr int x = 10;
constexpr int y = x+10; // Valid
...
4
votes
2
answers
118
views
Function pointer compatibility between single pointer and empty parameter lists
I've been reading about function pointer compatibility, but have not found the following scenario documented as being acceptable (below).
With this code, it is allowed (without warnings) to call a ...
3
votes
3
answers
167
views
How to check if a macro argument is an integer literal in C
I'm trying to do a macro like this:
#define STRING(i) \
struct STRING##i \
{ \
size_t len; \
char chars[i]; \
}
but the problem is this works with constexpr arguments like this:
constexpr int ...
1
vote
2
answers
89
views
How to set a pointer to an integer in a compound literal that is initialized in a function?
I tried to initialise a structure via a compound literal in a function. After solving a part of my problem (see Use of compound literal inside a function to init a variable in C), I have a new problem ...
1
vote
2
answers
96
views
Can __VA_OPT__(,) detect a trailing comma with nothing after it?
While playing with __VA_OPT__(,) I noticed the following behavior.
Background
First, note that C is fine with trailing commas in an initializer. These are equivalent:
int foo[] = { 10, 20, 30 };
int ...
0
votes
2
answers
113
views
Consolidating GNU C's and C23's deprecated function attribute
The problem I am facing when using the below macro
// Assume that __GNUC__ or __clang__ is defined.
#if defined(__has_c_attribute)
#if __has_c_attribute(deprecated)
#define ...
7
votes
1
answer
162
views
Existence or not of long_double_t (standard C: C23)
The new standard C (ISO/IEC 9899:2023, aka C23),
in the Annex H, several macros and types are defined,
relative to real and complex arithmetic,
subject to the standard ISO/IEC 60559
(floating-point ...
36
votes
2
answers
3k
views
Why isn't the keyword false an integer constant expression in gcc C23?
Latest gcc 13.x (trunk) gives a compiler error (gcc -std=c23) for this code:
int* p = false;
error: incompatible types when initializing type 'int *' using type '_Bool'
How can this be correct?
C23 ...
3
votes
2
answers
131
views
Using _Generic to implement IS_POINTER(p) with rvalue in C23?
I want to implement IS_POINTER(P) using _Generic. Using this answer that implements IS_ARRAY(A) as a starting point, I have:
#define IS_POINTER(P) \
_Generic( &(P), \
typeof(*P) ** : ...
0
votes
5
answers
153
views
Can C23 endianness macros be used to determine the layout of a bit-field?
I've been reading about the new additions to the C standard, and I've come across macros for determining the compile-time endianness.
However the standard still states that
The order of allocation of ...
2
votes
2
answers
184
views
Plain C equivalent to Rust's unimplemented!() macros
Please note that this is not a question on C++, but on plain C
In Rust there is a handy macro unimplemented!() to let the runtime crash, to be used when a function is unimplemented.
I basically have ...
18
votes
5
answers
3k
views
Are int main() and int main(void) equivalent prototypes in C23?
C23 introduced new semantics in function declarators:
6.7.6.3 Function declarators
[...]
13 For a function declarator without a parameter type list: the effect is as if it were declared with a ...
3
votes
2
answers
630
views
Is the C23 standard backward compatible?
Can C17 code be interpreted as C23 code? If not, what are the breaking changes?
2
votes
0
answers
1k
views
Is there any difference between the C23 _BitInt() and a non-bit-precise integer of the same width?
The upcoming C23 Standard adds a keyword _BitInt() which can be used, as I understand, to define an integer with a specific number of bits. However I could not find much information with regards to ...
7
votes
1
answer
200
views
Why doesn't gcc 13 display the correct binary represenation?
While answering a question here, I made the following example:
#include <stdio.h>
#include <math.h>
int main (void)
{
float_t a = -248.75;
printf("%f\n", a);
unsigned ...
4
votes
1
answer
143
views
does offsetof work with typeof if type is new?
I read on cppreference (https://en.cppreference.com/w/c/types/offsetof):
Even though it is specified in C23 that defining a new type in
offsetof is undefined behavior, such usage is only partially ...
5
votes
5
answers
272
views
How to use typeof_unqual to avoid compiler warnings about discarding const qualifier
In the following code, copyThing() is written so that its return value has the same type as its argument value:
extern void *copyThingImpl(const void *x);
#define copyThing(x) ((typeof(x)) ...
10
votes
2
answers
280
views
Does nullptr_t break type punning or pointer conversions?
Consider this union:
typedef union
{
void* vptr;
nullptr_t nptr;
} pun_intended;
nullptr_t is supposedly compatible with void* 1). Ok so what if we initialize the void* to some non-zero ...
9
votes
3
answers
1k
views
How can I use "nullptr" for null pointers prior to C23?
In C23, the nullptr keyword got standardized. I would prefer to use nullptr instead of NULL prior to C23 too because it means that I could write code which compiles in:
C, prior to C23
C, since C23
C+...
3
votes
2
answers
310
views
Is there an equivalent of __attribute__((nonnull)) in C23?
Several compiler vendors have implemented a non standard extension __attribute__((nonnull)) to specify that a pointer must not be a null pointer.
C99 introduced a new syntax to specify that a function ...
3
votes
1
answer
242
views
Can strlen be [[unsequenced]]?
The wording of the C23 working draft on unsequenced functions is unclear to me. Among other properties, unsequenced functions have to be independent:
(6) An object X is observed by a function call
(...
10
votes
2
answers
827
views
What are the [[reproducible]] and [[unsequenced]] attributes in C23, and when should I use them?
C23 introduced the attributes [[reproducible]] and [[unsequenced]].
What is the motivation behind them?
How are they defined, and what effect do they have on a function?
What kind of functions should ...
3
votes
3
answers
310
views
What is the difference between C++11's constexpr and C23's [[reproducible]]?
C++11 added the constexpr keyword which can be added to functions to specify constant behavior.
C23 added what seems to be identical functionality in the form of the [[reproducible]] tag.
How are ...
5
votes
1
answer
1k
views
What is the difference between float, _Float32, _Float32x, and _Float32_t?
C23 introduced a number of floating point types, including but not limited to:
_Float32
_Float32x
_Float32_t
I am unsure of the differences, such as:
Are they keywords, or are they type aliases, or ...
3
votes
1
answer
187
views
What is the alignment requirement of malloc(1)
I have heard that a successful call to malloc() returns a pointer suitably aligned for any type. Yet it seems useless and wasteful to require malloc(1) to return a pointer aligned for a larger value ...
0
votes
1
answer
85
views
How to avoid mentioning a function pointer's arguments inside the definition of a function that takes it as argument?
I'm writing this C code that involves passing around a lot of function pointers, and sometimes writing all the arguments that a function pointer takes when defining a function that takes it as an ...
2
votes
1
answer
269
views
What Exactly is the Proposed C23 `_Either` Type?
C23 proposal n3003 and n2366 mention a proposed _Either type in passing on the first page and seventh, respectively, and I have not been able to find any other references to it thus far. As far as I ...
11
votes
3
answers
2k
views
C23 auto vs C++11 auto
The C23 standard apparently has introduced using "auto" keyword for auto type deduction, see here, just like in C++11. However, there seems to be some differences.
According to here, https://...