If I write "long i = 1;" instead of "long i = 1l;", will the 1 be recognized as int and then implicitly converted to long?
Edit: Thank you all. I see there's no type conversion. Is this also the case with the suffix u (like 10u)? Then what's the use of those l and u?
The type of the constant 1 is int, so technically a type conversion will be done but it'll be done at compile time and nothing is lost.
However, consider the more interesting example of:
int main(void)
{
long long i = -2147483648;
long long j = -2147483647 - 1;
printf( " i is %lld, j is %lld\n", i, j);
return(0);
}
I get the following results from various compilers:
MSCV 9 (Version 15.00.21022.08):
i is 2147483648, j is -2147483648
GCC (3.4.5):
i is -2147483648, j is 0
Comeau (4.3.10.1):
i is 2147483648, j is -2147483648
Digital Mars:
i is -2147483648, j is -2147483648
I'm not sure yet how to account for the differences. It could be one or more of:
long long" support is C99, but some of these compilers might be compiling for C90 with "long long" as an extension)FWIW, the behavior of MSVC and Comeau is what I expected - which is something that many might still find surprising. The logic (in my mind) for the first operation is:
-2147483648 gets tokenized as '-' and 21474836482147483648 is an unsigned int (since it can't fit into an int - I believe this is different in C99) -' operator results in 2147483648 again due to unsigned arithmetic rulesThe logic for the second operation is:
-2147483647 gets tokenized as '-' and 21474836472147483647 is a signed int1 results in -2147483648 since there's no problem representing that numberIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With