Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Generic mechanism C11 standard     - MySQL replication master and slave structure built (Database)

- MySQL EXPLAIN SQL output description (Database)

- CentOS6.7 text installation system (Linux)

- Linux input and output redirection (Linux)

- Linux initialization init system - UpStart (Linux)

- 256 with rich colors decorate your terminal (Linux)

- Tomcat installation under Linux (Server)

- Oracle 11g manually create a database (Database)

- Graphics of Java Tools (Programming)

- Linux Network Programming - raw socket Example: sending a UDP packet (Programming)

- Configuring a Linux operating system security management services (Linux)

- CentOS 5.8 (64) Python 2.7.5 installation error resolved (Linux)

- Linux usage in echo (Linux)

- Understand ASP.NET 5 running the command: DNVM, DNX, and DNU (Server)

- Job achieve automation in Ubuntu 14.04 - Using Cron (Linux)

- Linux system firewall defense network attacks (Linux)

- Java implementation heap sort (large root heap) (Programming)

- Oracle to read and modify the data block process (Database)

- Awk include binding capacity larger than the specified size of all files directory (Linux)

- JavaScript function part (Programming)

  Generic mechanism C11 standard
  Add Date : 2018-11-21      
  Apple LLVM4.0 has supported the C11 standard key features - generics mechanism. Although the C11 in a generic mechanism than C ++ seem to be a lot of simple, but when doing library is still very useful.

Here we look at the C11 standard generic expression.

C11 Generics mechanisms are introduced by the keyword _Generic, its syntax is:

_Generic (Assignment-expression, generic-assoc-list)



    generic-assoc-list, generic-association

    type-name: assignment-expression

    default: assignment-expression

C code examples are given below:

#define GENERAL_ABS (x) _Generic ((x), int: abs, float: fabsf, double: fabs) (x)

static void GenericTest (void)


    printf ( "int abs:% d \ n", GENERAL_ABS (-12));

    printf ( "float abs:% f \ n", GENERAL_ABS (-12.04f));

    printf ( "double abs:% f \ n", GENERAL_ABS (-13.09876));

    int a = 10;

    int b = 0, c = 0;


    _Generic (A + 0.1f, int: b, float: c, default: b) ++;

    printf ( "b =% d, c =% d \ n", b, c);


    _Generic (A + = 1.1f, int: b, float: c, default: b) ++;

    printf ( "a =% d, b =% d, c =% d \ n", a, b, c);


It should be noted here, _Generic in the assignment-expression just get it done and not the type to run the calculation. That is, the compiler just get the type of the expression at compile time, without any other instructions. This with sizeof (), typeof (), as well as in C ++ typeid () and decltype () the same.

In addition, there must be assignment-expression with the same type of generic-association generic-association-list, otherwise the compiler will complain. Of course, if it contains default processing in generic-association-list, then the compiler can be carried out smoothly. As shown in the following code:

struct MyStruct {int a, b;} s;

_Generic ( "Hello", const char *: puts ( "OK!")); // ERROR "Hello" to char [6] type!

_Generic ( "Hello", char [6]: "! OK" puts ()); // OK

_Generic ((Const char *) "Hello", const char *: puts ( "OK!")); // OK

_Generic (S, int: puts ( "OK!")); // ERROR

_Generic (S, struct MyStruct: puts ( "OK!")); // OK

_Generic (S, int: puts ( "! Yep"), default: puts ( "Others")); // OK

It should be noted that, _Generic expression, expression statement are not met for a match will also be the type of compiler, to confirm its effectiveness. Therefore, the type does not satisfy the matching expression like sizeof (), typeof (), as only the compiler, not calculation. For example, the following statement is all wrong:

const int g = 50;
typeof (g = 10) t = 200; // ERROR
int f = sizeof (g = 100); // ERROR
f = _Generic (g, int: sizeof (g), char: (g = 100, g), default: g + 10); // ERROR

The last line of code, even if the char: This type is not matched, but the compiler will still complain.
- Ubuntu15 core CLR (Server)
- Getting the Linux shell variable test (Programming)
- Installing software on Ubuntu: apt-get and dpkg difference (Linux)
- Oracle through the alarm log view and inspect the main library, physical and snapshot standby database (Database)
- dmidecode command Detailed (Hardware information) (Linux)
- Android LayoutInflater source parsing (Programming)
- About MongoDB query method according to fuzzy field (Database)
- Linux System Tutorial: How to browse the Linux command line, weather forecast (Linux)
- Gentoo: existing preserved libs problem solving (Linux)
- Learning MySQL data types (Database)
- Setting the RedHat9 Intrusion Detection System (Linux)
- DataGuard the MRP can not start to analyze and solve problems (Database)
- Use Bosh deploy CloudFoundry problems encountered on OpenStack (Server)
- Install Oracle 11g illustrations and dependent libraries under SUSE11 (Database)
- OpenWrt modify flash size (Linux)
- The Samba service does not have permission to access (Server)
- Use matplotlib scientific drawing in Linux (Linux)
- Implement Oracle dynamic registration of non-standard port 1521 (Database)
- Installation of Ubuntu Make under Ubuntu 15.10 (Linux)
- IOS distributed management using GitHub project development (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.