0
0
CConceptBeginner · 3 min read

_Generic in C: Type Generic Programming Explained

_Generic in C is a keyword introduced in C11 that allows you to select expressions based on the type of a given value at compile time. It works like a simple type-based switch, enabling type-generic programming without macros or function overloading.
⚙️

How It Works

_Generic works like a type-based decision maker in your code. Imagine you have a box, and depending on what type of item you put inside, you want to do something different. _Generic checks the type of the expression you give it and picks the matching option you defined.

It looks at the type of the expression at compile time (before the program runs) and chooses the corresponding code or value. This is similar to choosing a tool from a toolbox based on the job you need to do, but the choice is made automatically by the compiler.

This feature helps write cleaner code when you want to perform different actions for different types without writing many separate functions or using complicated macros.

💻

Example

This example shows how _Generic selects a string describing the type of a variable.

c
#include <stdio.h>

#define type_name(x) _Generic((x), 
    int: "int",
    float: "float",
    double: "double",
    char *: "pointer to char",
    default: "other type")

int main() {
    int a = 5;
    float b = 3.14f;
    double c = 2.718;
    char *d = "hello";

    printf("a is %s\n", type_name(a));
    printf("b is %s\n", type_name(b));
    printf("c is %s\n", type_name(c));
    printf("d is %s\n", type_name(d));
    printf("42 is %s\n", type_name(42));

    return 0;
}
Output
a is int b is float c is double d is pointer to char 42 is int
🎯

When to Use

Use _Generic when you want to write functions or macros that behave differently depending on the type of their arguments. This is useful for creating type-generic code, like printing different types, performing math operations, or handling different data types in a single interface.

For example, if you want a single macro to print any type of variable with the correct format, _Generic lets you choose the right format string automatically. It helps avoid writing many versions of the same function for each type.

It is especially helpful in libraries or code that must work with multiple data types cleanly and safely without runtime overhead.

Key Points

  • _Generic is a compile-time feature introduced in C11.
  • It selects expressions based on the type of a given value.
  • Helps write type-generic code without macros or function overloading.
  • Works like a switch statement for types.
  • Improves code clarity and safety for multi-type operations.

Key Takeaways

_Generic enables type-based selection at compile time in C.
It helps write cleaner, type-generic code without complex macros.
Use it to handle different types in a single interface safely.
It acts like a switch statement but for data types.
Introduced in C11, it improves code clarity and maintainability.