Reputation:
I was wondering whether there is a way to tell the compiler (I'm on gcc version 4.1.2 20080704 (Red Hat 4.1.2-46) or icc 11.1) to throw a warning whenever a long-to-int implicit conversion takes place. For example, compiling the file test.c
which contains the code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
int n = atol(argv[1]);
printf("int: %d\n", n);
long int N = atol(argv[1]);
printf("long: %ld\n", N);
return 0;
}
with:
gcc -Wall -Wconversion test.c -o test
does not produce any warnings. Running the resulting binary as
./test 12345678901
I get, as expected:
int: -539222987
long: 12345678901
as the number 12345678901 has overflown the int but not the long. I'd like the compiler to tell me whenever something like this might happen. The option -Wconversion unexpectedly (to me) does not do that.
Upvotes: 4
Views: 3289
Reputation: 106317
Check if your gcc version has -Wshorten-64-to-32
. Be prepared for a deluge of possibly spurious double -> float conversion warnings if you use floating-point in your code.
edit: I think shorten-64-to-32
may be an Apple extension that mainline never picked up, sadly. So you may be out of luck unless you upgrade to gcc-4.3 or higher.
Upvotes: 4
Reputation: 169763
The behaviour of -Wconversion
changed with GCC4.3 - update your compiler and try again (can't check if it really works myself as I'm on a 32bit system, but as the warning gets correctly emitted for atoll
, it should)...
Upvotes: 0