Page 187 - C-Language
P. 187
Chapter 26: Generic selection
Syntax
• _Generic ( assignment-expression , generic-assoc-list )
Parameters
Parameter Details
generic-assoc-list generic-association OR generic-assoc-list , generic-association
generic- type-name : assignment-expression OR default : assignment-
association expression
Remarks
1. All type qualifiers will be dropped during the evaluation of _Generic primary expression.
2. _Generic primary expression is evaluated at translation phase 7. So phases like string
concatenation have been finished before its evaluation.
Examples
Check whether a variable is of a certain qualified type
#include <stdio.h>
#define is_const_int(x) _Generic((&x), \
const int *: "a const int", \
int *: "a non-const int", \
default: "of other type")
int main(void)
{
const int i = 1;
int j = 1;
double k = 1.0;
printf("i is %s\n", is_const_int(i));
printf("j is %s\n", is_const_int(j));
printf("k is %s\n", is_const_int(k));
}
Output:
i is a const int
j is a non-const int
k is of other type
https://riptutorial.com/ 163

