Output long long numbers in Mingw / Windows

    The long long type appeared in the c99 standard. In 32-bit architectures, the size of this type is 64 bits. The standard introduces a format string for printf-like functions for this type:% lld (% llu for unsigned).

    But as often happens, writing simply from directories does not always work. Faced with the need to recompile your code using MinGW / GCC, (which would seem to ensure compatibility and portability of the code between GNU / Linux and windows platforms), I found that the behavior of this type was significantly different from that described: the number was displayed as 32-bit.

    It turned out that MinGW uses calls to windows functions. Well, windows, as you know, takes care of backward compatibility with older versions: when the new standard appeared, they did not bring the functions in line with it, but continued to use the non-standard format string for this type:% I64d. Here is the relevant page in msd describing this - no% lld.

    But despite this, in Visual Studio 2005% lld is handled fine, but in MinGW, which uses the windows function, it doesn't.

    In short, voodoo programming
    Result:
    It’s good that if you use standard C ++ classes instead of C-functions, then there are no such problems. The solution for outputting a number into the string in the form 0000001234 (10 characters, right-justification, filling empty positions with zeros) looks like this: result: str == “0000001234”;

    stringstream stream;
    string str;
    long long number = 1234LL;

    stream.flags(ios::right);
    stream.fill( '0' );
    stream.width( 10 );

    stream << number;
    stream >> str;



    Also popular now: