0

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
Mohd Shahril
  • 2,257
  • 5
  • 25
  • 30
  • From the `printf` example it should be clear that g++ does not support the `I64` modifier. – Raymond Chen Oct 22 '14 at 02:46
  • possible duplicate of [Printf long long int in C with GCC?](http://stackoverflow.com/questions/13590735/printf-long-long-int-in-c-with-gcc) – Raymond Chen Oct 22 '14 at 02:48
  • @RaymondChen, I already take a look at that, but i'm became confuse with I64u output, so because of that I created this new question. Thank you for clear this thing up for me. :-) – Mohd Shahril Oct 22 '14 at 02:50
  • Using non-standard specifiers causes undefined behaviour; to solve this use `%llu`. – M.M Oct 22 '14 at 02:52
  • @MattMcNabb, this is my result for using `%llu`, input is both 89, and output is `25769968512 2337536` – Mohd Shahril Oct 22 '14 at 02:55
  • post a MCVE plus input, and change your code to check the result of `scanf` – M.M Oct 22 '14 at 02:55
  • @MattMcNabb, I don't clear insight regarding MCVE plus input, for your second request, I has already edited it. – Mohd Shahril Oct 22 '14 at 03:05
  • sorry I don't know what you mean but I can't guess what you have put in "..." or how you are compiling your program. – M.M Oct 22 '14 at 03:06
  • @MattMcNabb, sorry if I not clearing this thing up for you, here is my complete code, http://pastebin.com/cJyeaNB9, and here is how i'm compiling my code, `g++ code.cpp -o code.exe` – Mohd Shahril Oct 22 '14 at 03:08
  • @MohdShahril take out all the stuff not relevant to the problem, and use `-std=c++11` in the compilation, and check the return value of scanf. C++03 didn't have `long long`. – M.M Oct 22 '14 at 03:15
  • BTW in the pastebin code, `arr[i]` accesses out of bounds of the vector when `i == 1` ; vectors index from `0` – M.M Oct 22 '14 at 03:19
  • @MattMcNabb, using `-std=c++11` didnt solve this problem, BTW, for out of bound, it's actually coding mistake. BTW, I'm now planning to change all my code into C, and thanks for helping me. :-) – Mohd Shahril Oct 22 '14 at 03:25

2 Answers2

1

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)
//                           ^          ^
//                           |          |
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • Thank you very much!! I didn't noticed this much earlier, BTW, for G++, I'm using `%llu` instead of `%64u` and it's working for me. :-D – Mohd Shahril Oct 22 '14 at 03:34
0

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

Ray Donnelly
  • 3,920
  • 1
  • 19
  • 20