2

I need to do something like this:

scanf("%ms", ...)

and compile it with

g++ -Wall -pedantic -std=c++11 

also with

gcc -Wall -pedantic -std=c99

And it prints warning: ISO C does not support the 'm' scanf flag. How can i fix it?

AnorLondo
  • 37
  • 1
  • 7
  • Do you want to work with C++ or C? In C++, you should just use `std::string`. – Ry- Nov 14 '17 at 23:26
  • 1
    cf. https://stackoverflow.com/questions/38685724/difference-between-ms-and-s-scanf – PhillipD Nov 14 '17 at 23:29
  • 1
    This is explained in detail under the **"NOTES"** section of `man 3 scanf`. Both `a` and `m` for allocate are non-standard extensions. They require a *pointer to pointer to char*, not *pointer to char* as the variable argument. – David C. Rankin Nov 14 '17 at 23:34
  • The only way to fix that is to work on the ISO C standard committee and get the modifier made a part of standard C (and that still wouldn't make it valid in C99 or C11 — that particular opportunity has flown, unless you've got a time machine handy). Be aware that not all POSIX systems support it; specifically, macOS up to and including High Sierra 10.13 does not support it, for example. It's a very useful feature where it is supported; it isn't supported everywhere. So, you have to design a workaround for systems where it is not available even if you want to use it where it is available. – Jonathan Leffler Nov 14 '17 at 23:47
  • What constitutes 'fixing' your problem? Not changing the call to `scanf()` but suppressing the warning? Changing the code so the warning isn't generated because a different function is called? Are you planning to work only with GCC or do you have to work with other compilers too? – Jonathan Leffler Nov 15 '17 at 00:05
  • What about rolling your own ms like function? – chux - Reinstate Monica Nov 15 '17 at 03:00
  • Well, how can you solve that? don't use the `--pedantic` option. But as this is something you state in your question you cannot do, there's no solution to the problem _I want to force my nonstandard program to pass as standard_. – Luis Colorado Nov 15 '17 at 10:40

3 Answers3

2

Answer: Either drop -pedantic, or give up %ms. They don't come together.

AFAIK, there's nothing like %ms format specifier in ISO C standards. It's added as an extension by POSIX.

The command line argument -pedantic tells GCC to strictly follow the ISO C standards, so it won't accept %ms with that option.

If your program is targeting POSIX systems, it'd be fine if you drop -pedantic. Else if you want to make your code portable, don't use %ms in printf/scanf.

iBug
  • 35,554
  • 7
  • 89
  • 134
1

The %ms format is specified by POSIX, not by ISO C.

By using -Wall -pedantic -std=c99, you've requested diagnostics for code that violates the C99 standard, as yours does (its behavior is undefined).

If you want your code to be portable to ISO C99 (or C11) implementations, don't use %ms.

If you need to use %ms (making your code less portable, but it should work with any POSIX-conforming implementation), don't use those options. Dropping the -pedantic option seems to be sufficient to inhibit the warning. Compilers other than gcc are likely to have different options to achieve this (though clang copies gcc fairly closely).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

The %ms notation is a POSIX extension in scanf() compared with the standard C version of scanf()

TheAschr
  • 899
  • 2
  • 6
  • 19
  • 2
    While the statement is accurate (as far as it goes), I don't think it answers the question. – Jonathan Leffler Nov 15 '17 at 00:05
  • 1
    The POSIX [`scanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html) specification shows this is an extension: it includes the line "• [CX] ⌦ An optional assignment-allocation character '`m`'. ⌫" and also has an explanation of what it does (also marked with the '[CX]' — C extension — notation). – Jonathan Leffler Nov 15 '17 at 00:06