static_assert - Online Linux Manual PageSection : 3
Updated : 2023-01-26
Source : Linux man-pages 6.03
NAMEstatic_assert, _Static_assert − fail compilation if assertion is false
LIBRARYStandard C library (libc)
SYNOPSIS#include <assert.h>void static_assert(scalar constant-expression, const char *msg);/* Since C23: */
void static_assert(scalar constant-expression);
DESCRIPTIONThis macro is similar to assert(3), but it works at compile time, generating a compilation error (with an optional message) when the input is false (i.e., compares equal to zero). If the input is nonzero, no code is emitted. msg must be a string literal. Since C23, this argument is optional. There's a keyword, _Static_assert(), that behaves identically, and can be used without including <assert.h>.
RETURN VALUENo value is returned.
VERSIONSIn C11, the second argument (msg) was mandatory; since C23, it can be omitted.
STANDARDSC11 and later.
EXAMPLESstatic_assert() can't be used in some places, like for example at global scope. For that, a macro must_be() can be written in terms of static_assert(). The following program uses the macro to get the size of an array safely. #include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* This macro behaves like static_assert(), failing to
* compile if its argument is not true. However, it always
* returns 0, which allows using it everywhere an expression
* can be used.
*/
#define must_be(e) \
( \
0 * (int) sizeof( \
struct { \
static_assert(e); \
int ISO_C_forbids_a_struct_with_no_members; \
} \
) \
)
#define is_same_type(a, b) \
__builtin_types_compatible_p(typeof(a), typeof(b))
#define is_array(arr) (!is_same_type((arr), &*(arr)))
#define must_be_array(arr) must_be(is_array(arr))
#define sizeof_array(arr) (sizeof(arr) + must_be_array(arr))
#define nitems(arr) (sizeof((arr)) / sizeof((arr)[0]) \
+ must_be_array(arr))
int foo[10];
int8_t bar[sizeof_array(foo)];
int
main(void)
{
for (size_t i = 0; i < nitems(foo); i++) {
foo[i] = i;
}
memcpy(bar, foo, sizeof_array(bar));
for (size_t i = 0; i < nitems(bar); i++) {
printf("%d,", bar[i]);
}
exit(EXIT_SUCCESS);
}
SEE ALSOassert(3) 0
Johanes Gumabo
Data Size : 10,730 byte
man-static_assert.3Build : 2024-12-05, 20:55 :
Visitor Screen : x
Visitor Counter ( page / site ) : 3 / 179,765
Visitor ID : :
Visitor IP : 3.144.116.34 :
Visitor Provider : AMAZON-02 :
Provider Position ( lat x lon ) : 39.962500 x -83.006100 : x
Provider Accuracy Radius ( km ) : 1000 :
Provider City : Columbus :
Provider Province : Ohio , : ,
Provider Country : United States :
Provider Continent : North America :
Visitor Recorder : Version :
Visitor Recorder : Library :
Online Linux Manual Page : Version : Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page : Library : lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page : Library : lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base : Version : Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base : Library : lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36
Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.