I am trying this code on GNU's C++ compiler and am unable to understand its behaviour:
#include <stdio.h>;
int main()
{
int num1 = 1000000000;
long num2 = 1000000000;
long long num3;
//num3 = 100000000000;
long long num4 = ~0;
printf("%u %u %u", sizeof(num1), sizeof(num2), sizeof(num3));
printf("%d %ld %lld %llu", num1, num2, num3, num4);
return 0;
}
When I uncomment the commented line, the code doesn't compile and is giving an error:
error: integer constant is too large for long type
But, if the code is compiled as it is and is executed, it produces values much larger than 10000000000.
Why?
<stdint.h>
and useuint64_t
. To display a 64 bit value,printf( "%" PRIu64 "\n", val);
– Varietal<stdint.h>
included,uint64_t a = 0xffffffffffffff; printf( "%" PRIu64 "\n",a ); : error: expected ‘)’ before ‘PRIu64’ printf( "%" PRIu64 "\n",a ); :: warning: spurious trailing ‘%’ in format [-Wformat=] printf( "%" PRIu64 "\n",a );
– Changeable#define __STDC_FORMAT_MACROS 1
See #14536056 – Varietal