How to fix warning with %ms in scanf

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?

3 answers

  • answered 2017-11-14 23:28 TheAschr

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

  • answered 2017-11-15 02:06 Keith Thompson

    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).

  • answered 2017-11-15 02:11 iBug

    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.