Advantages of _Generic

Following are some main advantages of _Generic in C:

  • It can be used to stimulate function overloading when used with macros.
  • Allows the functionality to execute the code based on the type of parameters.

_Generics Keyword in C

The _Generic keyword in C is used to implement a generic code that can execute statements based on the type of arguments provided. It can be implemented with C macros to imitate function overloading. The _Generic keyword was first introduced in C11 Standard.

Similar Reads

Syntax of _Generic in C

_Generic( (expression), type_1: statements, type_2: statements, . . default: statements )...

How to use _Generic in C?

We can use the _Generic keyword anywhere in our code where we need some generic code. The below example demonstrates how to use _Generic keyword:...

_Generic in Macros

...

Advantages of _Generic

A major drawback of Macro in C is that the arguments lack type checking, i.e. a macro can operate on different types of variables(like char, int, double,..) without type checking. We can use _Generic to include the statements for different argument types in such macros....

Disadvantages of _Generic

...