Why can I compile this in 32-bit, but not 64-bit?

user@user:~/langs/c$ cat 3264int.c 
#include <stdio.h>
int main(){
        long z; 
    printf("Long int size is %d bytes long!\n", sizeof(z)); 

    return 0;
}
user@user:~/langs/c$ cat 3264int.c ^C
user@user:~/langs/c$ gcc -m32 -o 32int 3264int.c 
user@user:~/langs/c$ gcc -m64 -o 64int 3264int.c 
3264int.c: In function โ€˜mainโ€™:
3264int.c:4: warning: format โ€˜%dโ€™ expects type โ€˜intโ€™, but argument 2 has type โ€˜long unsigned intโ€™ cat 3264int.c

I tried changing the type for zto int, and it still has not compiled.

+3
source share
2 answers

This is a warning, not an error. You get an executable file. However, if you are trying to compile with -pedanticor -Werror, then you will not. If this micro offer is what you are working with, then what you need to do is change your format specifier to %ld. On your platform size_t, which will be returned sizeof, maybe 8 bytes on 64-bit, but 4 bytes on 32-bit. %dcan display a 32-bit integer, but not a 64-bit integer.

+5
source

sizeof size_t. 32- 32- int (.. unsigned int long unsigned int, , , 32- 32- ), 64- unsigned int, 64-. % d int ints 32- 32-, 64- .

:

sizeof - unsigned int , long, "% u" "% lu" printf.

:

% zu, size_t.

+4

Source: https://habr.com/ru/post/1791910/


All Articles