When do we need to specify an integer type for numeric literals?

I came across a code as shown below:

#define SOME_VALUE 0xFEDCBA9876543210ULL

This one is SOME_VALUEassigned to some unsigned long longlater.

Questions

  • Do I need to have a postfix like ULLin this case?
  • What situation do we need to indicate the type of integer used?
  • In this case, C and C ++ behave differently?
+3
source share
5 answers

In C hex literal receives the first type int, unsigned int, long, unsigned long, long longor unsigned long long, which may be its value, if it has no suffix. I would not be surprised if C ++ has the same rules.

, , , , , ,

1 << 43;

, ( ) undefined, 1LL << 43;, , .

+7
  • , , , , .
  • , printf("%ld", SOME_VALUE); SOME_VALUE integer , .
+3

++ . , , :

#include <iostream>

void consumeInt(unsigned int x)
{
    std::cout << "UINT" << std::endl;
}

void consumeInt(int x)
{
    std::cout << "INT" << std::endl;
}

void consumeInt(unsigned long long x)
{
    std::cout << "ULL" << std::endl;
}

int main(int argc, const char * argv[])
{
    consumeInt(5);
    consumeInt(5U);
    consumeInt(5ULL);

    return 0;
}

:

INT
UINT
ULL

+2

- , int . , int, , , , int. , 0xFEDCBA9876543210ULL.

, . 1.2 - double, 1.2f - float.

0

, - ; C , .

Suffixes are important if you want to force an expression type, for example. for how it interacts in expressions. This may be necessary if you are going to perform an arithmetic operation that would overflow a smaller type (for example, 1ULL<<nor x*10LL), and make it unsigned is useful when you want the expression as a whole has unsigned semantics (for example, c-'0'<10Uor n%2U).

0
source

All Articles