In this tutorial, you will learn the concept of _Generic keyword with a programming example. It will also help you to write a program on how to check a variable is const qualified in C. So let’s first understand what _Generic keyword ( _Generic selection ) is.
_Generic keyword (since C11):
Using the _Generic keyword, we can select one expression at compile-time from the several expressions on the basis of the type of a controlling expression.
Syntax of _Generic keyword:
//Syntax of Generic selection _Generic ( assignment-expression , generic-assoc-list )
Where,
1.assignment-expression:
It is called the controlling expression. The controlling expression of a generic selection is not evaluated. For example,
_Generic(char Foo(), char: "character", default: "error");
It doesn’t result in a call at runtime to char Foo.
The type of the controlling expression is determined at compile-time. If controlling expression is compatible with the type name, then the result expression of the generic selection is the expression in that generic association. Otherwise, the result expression of the generic selection is the expression in the default generic association.
You must remember that controlling expression type must be compatible with one of the type-names in generic-assoc-list if the default association is not used. Otherwise, your code will not compile.
2.generic-assoc-list:
An association list is a comma-separated list of associations. The syntax of association is so simple which has shown below.
type-name : expression
where,
type-name: Any complete object type that isn’t variably modified (that is, not VLA or pointer to VLA).
expression: Any expression (except for the comma operator) of any type and value category.
So, the generic syntax of generic-assoc-list would be,
type-name : expression, type-name : expression, ...,default : expression
Here added, default: expression to avoid any compilation error if controlling expression is not matched with any type name.
⚡ None of the expressions from any other generic association of the generic selection is evaluated.
👍 Click to Directly Jump to Video Lecture (Hindi):
Some Constraints:
➤The generic-assoc-list can’t specify the same type more than once. For example, I have used int type-name twice which is wrong.
#define TYPE_NAME(X) _Generic((X), \ int: "int", \ int: "integer", \ double: "double", \ default: "unknown")
➤ A generic selection shall have no more than one default generic association. See the below example, when we use the macro we will get the compile-time error.
#define TYPE_NAME(X) _Generic((X), \ int: "int", \ default: "unknown",\ default: "unknown")
➤ If a generic selection doesn’t have a default, the controlling expression must have only one compatible type name in the generic association list. See the following code,
#include <stdio.h> #define TYPE_NAME(X) _Generic((X), \ char: "character", \ int: "integer") int main() { //passing double in macro printf("%s\n", TYPE_NAME(3.5)); return 0; }
Output:
error: ‘_Generic’ selector of type ‘double’ is not compatible with any association #define TYPE_NAME(X) _Generic((X), \
➤The type name in a generic association shall specify a complete object type other than a variably modified type. For example, VLA int arr[n];
Example code to explain the use of _Generic keyword:
The following code shows how to write a macro to check the type of argument you pass to it. It produces “unknown type” if no entry in the assoc-list matches the controlling expression. You must remember your compiler must support std-c11.
#include <stdio.h> //check type of argument passed #define TYPE_NAME(X) _Generic((X), \ int: "int", \ char: "char", \ double: "double", \ default: "unknown") int main() { // Passing an integer value printf("Type name: %s\n", TYPE_NAME(27)); return 0; }
Output:
Type name: int
Video on _Generic keyword in C (Hindi):
Recommended Post
- C Programming Courses And Tutorials
- CPP Programming Courses And Tutorials.
- How to check a variable is const qualified in C?
- Difference between macro constant and const variable.
- typedef vs #define.
- You should know the volatile Qualifier.
- 100 embedded C Interview Questions.
- 100 C interview Questions.
- Important const qualifier interview Questions.
- Difference between const qualifier and volatile qualifier.
- Question-Related to const.
- Interview questions on bitwise operators in C.
- C format specifiers.