Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with printf("%llx")?

I have this piece of code that is challenging all my knowledge of C. Here I have :

int main(void){
    unsigned long long int massage ;

    scanf("%llX", &massage); //input: 0x1234567890abcdef
    printf("%llX", massage);
    return 0;
}

On my "64bit - Corei5 - Fedora - GCC" it prints out exactly what I fed it. but on my buddy's system (32bit, MS XP, MinGW) it prints 90ABCDEF. I don't understand why. does anyone know?

BTW: sizeof(unsigned long long int) on his system is 8.

like image 668
Untitled Avatar asked Dec 28 '12 15:12

Untitled


People also ask

Is printf insecure?

Yes it's unsafe, but not for the reason you suggested.

What is %f in printf in C?

The f in printf stands for formatted, its used for printing with formatted output.

What is LX in printf?

%p and %lx prints the address in hexadecimal while %ld prints it in decimal.


1 Answers

The issue is a discrepancy between what the compiler believes (as reflected in sizeof: sizeof(unsigned long long int) is evaluated at compile-time) and what the run-time library believes (as reflected in printf: the printf function is called at run-time, so that's when its format-specifiers take effect).

According to "C99" in the MinGW documentation:

GCC does not include a C runtime library. This is supplied by the platform. The MinGW port of GCC uses Microsoft's original (old) Visual C runtime, MSVCRT, which was targeted by Microsoft Visual Studio 6 (released in 1998).

[…]

Because MinGW relies on MSVCRT, it has many of the same limitations and quirks with compatibility as Visual Studio 6. You should assume that MinGW applications cannot rely on C99 behaviour, only on C89. For example, the newer format characters in printf like %a and %ll are not supported, although there exists a workaround for %ll.

(The workaround that it mentions is to use I64 instead of ll: so, %I64X. Annoyingly, at least on my system, GCC will issue a warning when it sees that in a literal format-string, because it assumes it'll have a better run-time library.)

like image 147
ruakh Avatar answered Sep 17 '22 15:09

ruakh