Have a look at the following piece of code:
#include <stdio.h>
int main(void)
{
int a;
a = 2147483647;
printf("a + 1 = %d \t sizeof (a + 1) = %lu\n", a + 1, sizeof (a + 1));
printf("a + 1L = %ld \t sizeof (a + 1L) = %lu\n", a + 1L, sizeof (a + 1L));
a = -1;
printf("a + 1 = %d \t sizeof (a + 1) = %lu\n", a + 1, sizeof (a + 1));
printf("a + 1L = %ld \t sizeof (a + 1) = %lu\n", a + 1L, sizeof (a + 1L)); //why a + 1L does not yield long integer ?
return 0;
}
This results in the following output:
a + 1 = -2147483648 sizeof (a + 1) = 4
a + 1L = 2147483648 sizeof (a + 1L) = 8
a + 1 = 0 sizeof (a + 1) = 4
a + 1L = 0 sizeof (a + 1) = 8
Why does a + 1L in last line yield 0 instead of a long integer as 4294967296 ?
Because converting the
int-1 to along intresults in thelong intwith value -1, and-1 + 1 = 0.Converting
-1to another type would only result in4294967295if the target type is an unsigned 32-bit type (usually,unsigned intis such, generally,uint32_t, if provided). But then, adding 1 to the value would wrap to 0.Thus to obtain
4294967296, you would need an intermediate cast,so that
-1is first converted to theuint32_twith value4294967295, and that is then converted tolong.