I have read the C11 standard and it contains an interesting feature of the C language and that is the use of _Generic
...
Due to its context, I imagine that it is used to make generic expressions in the language, but I don't quite understand it.
This is the code I've tried:
#include <stdio.h>
#define ObtenerTipo(x) \
_Generic((x), \
char : "char", \
short : "short", \
int : "int", \
long : "long", \
long long : "long long", \
default : "desconocido" \
)
int main(void) {
return printf("Tipo: %s\n", ObtenerTipo(0xDEADBEEF0);
}
What it throws on my computer is:
long
How does it work _Generic
in C language?
Introduction
_Generic
is a new keyword of the C language, introduced in its latest standard (C11) whose objective is to give a behavior based on the type of data stored by the expression it evaluates:Note that the square brackets (
[]
) mean that there can be more than one, see example below.Example:
Its behavior is similar to that of a
switch
, the main difference is that it_Generic
evaluates the types, let's take the macro that you have previously put as an example:_Generic
is responsible for locating the data type that resides in(x)
, so if it is an operation, it will probably be replaced by the casedefault
or it will generate errors in the final code, that is, if we write the following expression:It will jump directly to
default
, this is because_Generic
it does not evaluate expressions, only the types of the first argument passed, and will later remove the call to change it to its value:It will be replaced by
int
orlong
, maybelong long
in either case, this depends on the architecture of your computer.Limitations, or rather, drawbacks?
Like any good practice and application of a language feature, we know that it is not good to abuse them, for 'X' or 'Y' reason,
_Generic
it is not the exception.char
orshort
are automatically evaluated toint
_Generic("hola", ...)
will be evaluated tochar *
._Generic(+0.00, ...)
will be evaluateddouble
directly._Generic
(Not really limitation/drawback, but purists get used to it fast) .The main limitations of the keyword
_Generic
are based on types, however, it is fully applicable to any type, in fact, most of these limitations can be easily worked around by casting the operand passed as an argument.Use cases
In practically every language there are generic objects, however, the C language is not so graceful, but we have been blessed with
_Generic
to give it a little more seasoning and that is to be able to execute various functions based on the data type of the argument passed to the function , see the case oftgmath.h
:Imagine we have the following function:
It's really tedious to have to know which function to call in a certain case, well, the macro solves it like this:
And so you just have to call the macro:
In summary...
The keyword
_Generic
has quite a few uses and is one of the best features introduced in the C11 language standard, it definitely helps make code more readable and allows for ease between various types, but like any language feature, it's not good to overuse. her.Cheers :)