0

I have a custom implementation of printf which I use in my school projects. Wishing to have the same warnings that printf, I use __attribute__((format (printf ...)). That works fine, but using -Wall -Wextra -Werror -std=c11 -pedantic-errors, I get the error ISO C does not support %n$ operand number formats on gcc 7.2.0. (My implementation support that format). On clang it does not shout a warning if I use std=c11, but does with std=c99.

Is that format part of the c11 specification (as clang behavior let me think) or only the Singe Unix Specification (that is what my man page says) ? If so, how can I use it ? In this page, I cannot find it as an option to std.

I would rather not disable any warnings, since they are quite useful and catches a lot. But is there a way to allow that format in format strings, or specify only for my function ?

Thanks a lot.

P.S: I use a custom implemenation of printf because in my school projects we must only use specific allowed functions (basically, system api like malloc, free, read etc), or the one we have done ourselves.

EDIT : for reference, the format %n$, with n being string of digit for a non-zero number, allow to access the argument at that index (starting at one) in the list of variable arguments given to printf. (And can be used for the conversion itself or for the precision or field widht with the * operand).

VannTen
  • 441
  • 2
  • 12
  • 3
    That format is indeed a POSIX extension, and not in the C specification (neither in C99 nor in C11). – Some programmer dude Jan 16 '18 at 16:05
  • 2
    "Is that format part of the c11 specification" --> No. – chux - Reinstate Monica Jan 16 '18 at 16:07
  • 1
    Alternative: Make a new function called `VT_printf()` that in turn calls `vprintf()` Call `VT_printf()` when using `"%n$"`. IOWs, do not call a standard C library function directly with an invalid parameter. – chux - Reinstate Monica Jan 16 '18 at 16:12
  • @Someprogrammerdude Is there a way to tell the compilator that I can use that POSIX extension ? @chux I don't see how that will solve my problem ? You mean not performing the checks on `VT_printf` with attribute ? But then spell errors in my format strings will not be catched anymore. – VannTen Jan 16 '18 at 16:17
  • Yea pedantic errors will complain about it always :/ – Antti Haapala -- Слава Україні Jan 16 '18 at 16:21
  • "But then spell errors in my format strings will not be catched anymore. " Yet that is your goal. You do not want "%n$", which is a error for standard `printf()`, to be labeled an error. – chux - Reinstate Monica Jan 16 '18 at 16:34
  • It’s a minor oversight in Clang that it doesn’t report the POSIX notation as non-C-standard. You can’t have your cake and eat it. Either you want standard C only and messages for Jon-standard C, or you don’t. You can have only one or the other. Maybe put the POSIX-permitted code into a separate file from the strictly standard C code and compile the different files with different options? – Jonathan Leffler Jan 16 '18 at 16:55
  • @JonathanLeffler That's quite a good idea. Is there a way to tell GCC (and clang, btw) to be as strict as `pedantic-errors`, but for POSIX C ? (warn about features that are not part of it ?) – VannTen Jan 16 '18 at 17:00
  • No flag that I know of for “POSIX extensions”, but POSIX only defines extensions to the libraries and not to the core C standard. The formatted I/O functions are one of the few places where there is a discrepancy that the compilers detect. I can’t immediately think of any others. – Jonathan Leffler Jan 16 '18 at 17:12
  • @JonathanLeffler well, it *does* define extensions to the C standard, including higher limits, and castability of `void *` to function pointers be defined and so forth... but... – Antti Haapala -- Слава Україні Jan 16 '18 at 18:06

1 Answers1

0

You could use gnu_printf instead of printf in the attribute to model a behavior that comes closer to POSIX' printf.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177