vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

c_integer_constant.c (1487B)


      1 // C integer constants
      2 
      3 // Source: https://en.cppreference.com/w/c/language/integer_constant
      4 
      5 #include <inttypes.h>
      6 #include <stdio.h>
      7 
      8 int main(void)
      9 {
     10     printf("123 = %d\n", 123);
     11     printf("0123 = %d\n", 0123);
     12     printf("0x123 = %d\n", 0x123);
     13     printf("12345678901234567890ull = %llu\n", 12345678901234567890ull);
     14     // the type is a 64-bit type (unsigned long long or possibly unsigned long)
     15     // even without a long suffix
     16     printf("12345678901234567890u = %"PRIu64"\n", 12345678901234567890u );
     17 
     18     // printf("%lld\n", -9223372036854775808); // Error:
     19         // the value 9223372036854775808 cannot fit in signed long long, which
     20         // is the biggest type allowed for unsuffixed decimal integer constant
     21 
     22     printf("%llu\n", -9223372036854775808ull );
     23     // unary minus applied to unsigned value subtracts it from 2^64,
     24     // this gives unsigned 9223372036854775808
     25 
     26     printf("%lld\n", -9223372036854775807ll - 1);
     27     // correct way to form signed value -9223372036854775808
     28 }
     29 
     30 // The following variables are initialized to the same value:
     31 
     32 int d = 42;
     33 int o = 052;
     34 long o29 = 0O4'750l; // C29
     35 int x = 0x2a;
     36 int X = 0X2A;
     37 int b = 0b101010; // C23
     38 
     39 // The following variables are also initialized to the same value:
     40 
     41 unsigned long long l1 = 18446744073709550592ull; // C99
     42 unsigned long long l2 = 18'446'744'073'709'550'592llu; // C23
     43 unsigned long long l3 = 1844'6744'0737'0955'0592uLL; // C23
     44 unsigned long long l4 = 184467'440737'0'95505'92LLU; // C23
     45