Mohd Shahril
Mohd Shahril

Reputation: 2327

Getting weird result by using %I64u inside Mingw-w64

This is my code :

Note : \n inside scanf is my way to prevent trailing newline problem. That isn't best solution but i'm using it too much and currently it becoming my habit. :-)

...

int main()
{
    unsigned long long int input[2], calc_square;

    while(scanf("\n%I64u %I64u", input[0], input[1]) == 2)
    {
        printf("%I64u %I64u\n", input[0], input[1]);

        ...

My expected input and program result is :

Input :

89 89

For output, instead of printing back 89, it show this output :

I64u I64u

I'm using g++ (GCC) 4.9.1 from MSYS2 package. Noted that g++ because there are some portion of my code currently using C++ STL.


Edited : I changed my code by using standard %llu instead of %I64u, and here is my expected input and program result :

Input

89 89

For output, it's kind a weird result :

25769968512 2337536

Upvotes: 0

Views: 4386

Answers (2)

Ray Donnelly
Ray Donnelly

Reputation: 4086

I suspect you have been using MSYS2's GCC which isn't a native Windows compiler, and doesn't support the MS-specific %I64 format modifiers (MSYS2's GCC is very much like Cygwin's GCC).

If you wanted to use MinGW-w64 GCC, you should have launched mingw64_shell.bat or mingw32_shell.bat and have the appropriate toolchain installed:

pacman -S mingw-w64-i686-toolchain

or

pacman -S mingw-w64-x86_64-toolchain

With that done, you can safely use either modifier on any Windows version dating back to Windows XP SP3 provided you pass -D__USE_MINGW_ANSI_STDIO=1.

FWIW, I avoid using the MS-specific modifiers and always pass -D__USE_MINGW_ANSI_STDIO=1

Finally, annoyingly, your sample doesn't work when launched from the MSYS2 shell due to mintty not being a proper Windows console; you need to run it from cmd.exe

Upvotes: 0

Adam Rosenfield
Adam Rosenfield

Reputation: 400274

This code is wrong:

while(scanf("\n%I64u %I64u", input[0], input[1]) == 2)

input[0] and input[1] each have type unsigned long long, but they are required to have type unsigned long long * (pointer to unsigned long long) for scanf operations. I'm unsure if MinGW supports checking printf and scanf format specifiers, but ordinary GCC is capable of detecting these kinds of errors at compile time as long as you enable the proper warnings. I highly recommend always compiling with as high of a warning level as you possibly can, such as -Wall -Wextra -Werror -pedantic in the most extreme case.

You need to pass in the address of these variables:

while(scanf("\n%I64u %I64u", &input[0], &input[1]) == 2)
//                           ^          ^
//                           |          |

Upvotes: 1

Related Questions