Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax and Sample Usage of _Generic in C11

Tags:

c

generics

c11

I heard C11 added generics. I've googled a bit, looked at some articles, understood there's a new keyword ( _Generic ) and all. But I can't seem to grasp it all.

Is it something like the generics in C# or templates in C++? Can anyone give me a brief explanation of the C11 definition of generics, its syntax and a simple sample usage example?

like image 474
ApprenticeHacker Avatar asked Mar 21 '12 12:03

ApprenticeHacker


People also ask

What is _generic in C?

_Generic keyword in C is used to define MACRO for different data types. This new keyword was added to the C programming language in C11 standard release. the _Generic keyword is used to help the programmer use the MACRO in a more efficient way. this keyword translate the MACRO based on the type of the variable.

What is generics C++?

Generics in C++ Generics is the idea to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes and interfaces. For example, classes like an array, map, etc, which can be used using generics very efficiently. We can use them for any type.


1 Answers

The best example I have seen inspired the following (runnable) example, which unlocks all sorts of freaky possibilities for cracked-out introspection...

#include <stdio.h> #include <stddef.h> #include <stdint.h>  #define typename(x) _Generic((x),        /* Get the name of a type */             \                                                                                   \         _Bool: "_Bool",                  unsigned char: "unsigned char",          \          char: "char",                     signed char: "signed char",            \     short int: "short int",         unsigned short int: "unsigned short int",     \           int: "int",                     unsigned int: "unsigned int",           \      long int: "long int",           unsigned long int: "unsigned long int",      \ long long int: "long long int", unsigned long long int: "unsigned long long int", \         float: "float",                         double: "double",                 \   long double: "long double",                   char *: "pointer to char",        \        void *: "pointer to void",                int *: "pointer to int",         \       default: "other")  #define fmt "%20s is '%s'\n" int main() {    size_t s; ptrdiff_t p; intmax_t i; int ai[3] = {0}; return printf( fmt fmt fmt fmt fmt fmt fmt fmt,       "size_t", typename(s),               "ptrdiff_t", typename(p),         "intmax_t", typename(i),      "character constant", typename('0'),  "0x7FFFFFFF", typename(0x7FFFFFFF),     "0xFFFFFFFF", typename(0xFFFFFFFF), "0x7FFFFFFFU", typename(0x7FFFFFFFU),  "array of int", typename(ai)); } 
                 ╔═══════════════╗  ═════════════════╣ Amazeballs... ╠═════════════════════════════════════                  ╚═══════════════╝              size_t is 'unsigned long int'          ptrdiff_t is 'long int'           intmax_t is 'long int' character constant is 'int'         0x7FFFFFFF is 'int'         0xFFFFFFFF is 'unsigned int'        0x7FFFFFFFU is 'unsigned int'       array of int is 'other' 
like image 144
Alex Gray Avatar answered Sep 27 '22 21:09

Alex Gray