24

I'm trying to get long long from the console using standard IO function scanf. I started with %lld:

scanf("%lld", &rule);

That throws:

error: unknown conversion type character 'l' in format [-Werror=format=]

I've found more workarounds, but they too throw errors:

 scanf("%I64d", &rule);
   ->error: ISO C does not support the 'I64' ms_scanf length modifier [-Werror=format=]
 scanf("%"SCNd64"", &rule);
   ->error: expected ')' before 'SCNd64'

Am I doing something wrong? Is there an another trick?

I'm compiling on very recent version of MinGw GCC with these flags: -pedantic -Wall -Werror -std=c99 -g -D HOME=1

7
  • Try using something like -std=gnu99 instead of c99.
    – hobbs
    Commented May 18, 2014 at 2:55
  • If rule is int64_t instead, try scanf("%" SCNd64, &rule); Be sure to include inttypes.h and stdint.h. See this for more info. It should be supported in C99.
    – WhozCraig
    Commented May 18, 2014 at 2:58
  • I deleted an answer I had posted; what you are doing complies and executes fine for me with the same gcc options on Linux, so I'm thinking it's something to do with MingW.
    – James King
    Commented May 18, 2014 at 3:41
  • Your implementation is not C99-compliant. Are you sure that -std=c99 is being passed when the first error message is generated?
    – M.M
    Commented May 18, 2014 at 4:35
  • 2
    -std=gnu99 yields the same errors. Commented May 18, 2014 at 11:35

3 Answers 3

13

Just wanted to add this snippet too:

MinGW-w64 - for 32 and 64 bit Windows / [Mingw-w64-public] -Wformat and %llu

the issue is that formatter-width specifier %ll isn't supported for all msvcrt-DLL versions, therefore gcc warns about its use. The variant for specifying 64-bit integer-scalar-width in formatter for msvcrt in a backward-compatible way is by using %I64.

Use %I64u on Windows, or just use inttypes.h PRIuMAX.

If you must use %llu, define __USE_MINGW_ANSI_STDIO macro before including stdio.h. Be aware that if you do this, MS type %I64* format will no longer work.

6

for SCNd64 and similar, you'd have to use

#include <inttypes.h>

but all of this is only supposed to work if your compiler supports C99. Your first error message is a strong indication that it doesn't, or that you didn't give the right commandline switches.

6
  • 4
    It doesn't: MinGW re-uses as many software components from Windows as possible, and this includes the C runtime. MinGW's GCC is a chimera of a C99 compiler with a non-C99 standard library. Commented May 18, 2014 at 4:48
  • Unfortunatelly, after including inttypes.h, I'm getting the same errors as before. How can it be possible for a compiler being unable to do such a trivial thing? The command line switches must be kept - the same switches are being used on evaluation server (I'm doing a programming homework). Commented May 18, 2014 at 11:34
  • As Pascal says, your platform includes components from Windows that aren't C99 compliant. On the evaluation server, if this isn't on a Windows system, you might have more luck. Commented May 18, 2014 at 18:54
  • Even though we get warning for lld, I observed that correct value is printed (MinGW. In below case, the result printed is 6442450950 unsigned long long var6=2147483650*3; printf("var6 =%lld\n",var6);
    – Rajesh
    Commented Jan 17, 2018 at 15:30
  • 1
    @rajesh, that's because the libc, not the compiler, does the printing. The libc does the job as the implementation says. The compiler additionally check whether a <standard>-compliant library would correctly handle formatting strings.
    – PypeBros
    Commented Apr 13, 2018 at 11:33
0

Certain compilers, especially Windows-based ones, do not have the lond long data type as a predefined data type. In these cases, what you should do is use the keyword %" PRId64 "\n" which is a specification for a high precision long long data type.
To use this function it is necessary to include the #include <inttypes.h> library #include <inttypes.h> In your example it would look like this:

scanf("%" SCNd64, &rule);

Not the answer you're looking for? Browse other questions tagged or ask your own question.