gnu::format(3attr) — Linux manual page

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

gnu::format(3attr)                                     gnu::format(3attr)

NAME         top

       gnu::format - specify the style of format string

SYNOPSIS         top

       [[gnu::format(style, fmt-idx, first-idx)]]

DESCRIPTION         top

       This attribute can be applied to a function.  It specifies that a
       function parameter is a format string, and specifies the style of
       the format string.  This allows checking the syntax of the format
       string, as well as the types of the variadic arguments.  The style
       can be one of the following.

       printf
       scanf
       strftime
       strfmon

       fmt-idx is a 1-based index that specifies the position of the
       format string within the parameter list.

       first-idx is a 1-based index that specifies the position of the
       first argument that corresponds to the format string.  If the
       first argument is part of a va_list argument, it should be
       specified as 0.

VERSIONS         top

   GNU syntax
       __attribute__((format(style, fmt-idx, first-idx)))

   Styles
       On some targets, other styles are additionally supported.

       MinGW
       Microsoft Windows
              ms_printf
              ms_scanf
              ms_strftime

              These correspond to the formats supported by the msvcrt.dll
              library.

              GCC-only.

       Solaris
              cmn_err

       Darwin
              CFString

       OpenBSD
              kprintf
              syslog
              syslog Clang-only.

       FreeBSD
              freebsd_kprintf
                     Clang-only.

       In some languages, other styles are additionally supported.

       Objective-C
              NSString

   Non-variadic functions
       Clang accepts the attribute on non-variadic functions as an
       extension.

STANDARDS         top

       GNU.

HISTORY         top

       gcc, g++, clang 2.8, clang++ 2.8.

EXAMPLES         top

           [[gnu::format(printf, 3, 0)]]
           int
           vstprintf(int size;
               char buf[restrict size], int size,
               const char *restrict fmt, va_list args)
           {
                int len;

                if (size == 0) {
                     errno = EOVERFLOW;
                     return -1;
                }

                len = vsnprintf(buf, size, fmt, args);
                if (len >= size) {
                     errno = E2BIG;
                     return -1;
                }

                return len;
           }

           [[gnu::format(printf, 3, 4)]]
           int
           stprintf(int size;
               char buf[restrict size], int size,
               const char *restrict fmt, ...)
           {
                int len;
                va_list args;

                va_start(args, fmt);
                len = vstprintf(buf, size, fmt, args);
                va_end(args);

                return len;
           }

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.15.tar.gz
       fetched from
       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on
       2025-08-11.  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.15            2025-07-19             gnu::format(3attr)