_Generic(3) — Linux manual page

NAME | SYNOPSIS | DESCRIPTION | STANDARDS | HISTORY | EXAMPLES | COLOPHON

_Generic(3)             Library Functions Manual             _Generic(3)

NAME         top

       _Generic - type-generic selection

SYNOPSIS         top

       _Generic(expression, type1: e1, ... /*, default: e */);

DESCRIPTION         top

       _Generic() evaluates the path of code under the type selector
       that is compatible with the type of the controlling expression,
       or default: if no type is compatible.

       expression is not evaluated.

       This is especially useful for writing type-generic macros, that
       will behave differently depending on the type of the argument.

STANDARDS         top

       C11.

HISTORY         top

       C11.

EXAMPLES         top

       The following program demonstrates how to write a replacement for
       the standard imaxabs(3) function, which being a function can't
       really provide what it promises: seamlessly upgrading to the
       widest available type.

              #include <stdint.h>
              #include <stdio.h>
              #include <stdlib.h>

              #define my_imaxabs  _Generic(INTMAX_C(0),  \
                  long:           labs,                  \
                  long long:      llabs                  \
              /*  long long long: lllabs */              \
              )

              int
              main(void)
              {
                  off_t  a;

                  a = -42;
                  printf("imaxabs(%jd) == %jd\n", (intmax_t) a, my_imaxabs(a));
                  printf("&imaxabs == %p\n", &my_imaxabs);
                  printf("&labs    == %p\n", &labs);
                  printf("&llabs   == %p\n", &llabs);

                  exit(EXIT_SUCCESS);
              }

COLOPHON         top

       This page is part of the man-pages (Linux kernel and C library
       user-space interface documentation) project.  Information about
       the project can be found at 
       ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report
       for this manual page, see
       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.
       This page was obtained from the tarball man-pages-6.9.1.tar.gz
       fetched from
       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on
       2024-06-26.  If you discover any rendering problems in this HTML
       version of the page, or you believe there is a better or more up-
       to-date source for the page, or you have corrections or
       improvements to the information in this COLOPHON (which is not
       part of the original manual page), send a mail to
       man-pages@man7.org

Linux man-pages 6.9.1          2024-06-15                    _Generic(3)