27

Is the integer constant's default type signed or unsigned? such as 0x80000000, how can I to decide to use it as a signed integer constant or unsigned integer constant without any suffix?

If it is a signed integer constant, how to explain following case?

printf("0x80000000>>3 : %x\n", 0x80000000>>3);

output:

0x80000000>>3 : 10000000

The below case can indicate my platform uses arithmetic bitwise shift, not logic bitwise shift:

int n = 0x80000000;

printf("n>>3: %x\n", n>>3);

output:

n>>3: f0000000
3
  • 1
    Usually, 0x80000000 is INT_MAX + 1, so it's unsigned. Hence logical shift in the first example. But when you assign it to an int, you invoke undefined behaviour, and typically the result is INT_MIN. Left shifting negative integers is implementation-defined, often arithmetic shift is used. The difference is that in the latter, you force it to a signed type. Jul 3, 2012 at 12:28
  • @DanielFischer INT_MAX + 1 is UB but int n = 0x80000000; is not UB but implementation-defined and the integer conversion in this case is ruled by 6.3.1.3p3 (in C99)
    – ouah
    Jul 3, 2012 at 18:54
  • @ouah The INT_MAX + 1 was meant as a mathematical expression, not C. It's correct, however, that converting that to int isn't undefined behaviour, but implementation-defined. My bad. Jul 3, 2012 at 18:57

2 Answers 2

28

C has different rules for decimal, octal and hexadecimal constants.

For decimal, it is the first type the value can fit in: int, long, long long

For hexadecimal, it is the first type the value can fit in: int, unsigned int, long, unsigned long, long long, unsigned long long

For example on a system with 32-bit int and unsigned int: 0x80000000 is unsigned int.

Note that for decimal constants, C90 had different rules (but rules didn't change for hexadecimal constants).

12
  • 1
    The only correct answer so far. (Nits: you didn't specify the rules for octal [see hex], and from C90, hexadecimal literals changed by the addition of the long long types). Jul 3, 2012 at 12:14
  • but then can't we expect a warning in int n = 0x80000000; Because it is assigning a uint32_t to a int32_t and somehow overflows ?
    – mbonnin
    Jul 3, 2012 at 12:29
  • Does this mean the statement "An integer constant like 1234 is an int." in [K&R2] is wrong?
    – Victor S
    Jul 3, 2012 at 12:37
  • 1
    @VictorS: No, it's correct - 1234 is within the guaranteed minimum range of int so it will always have type int.
    – caf
    Jul 3, 2012 at 13:12
  • 2
    @mbonnin some compilers can display a warning but the standard does not require a warning. There are implicit conversions between any arithmetic types to any arithmetic types.
    – ouah
    Jul 3, 2012 at 13:20
9

It is signed if it fits in a signed integer. To make it unsigned, append a u suffix, e.g. 1234u.

You can convert a signed value to unsigned by assigning it to an unsigned variable.

unsigned int i = 1234u; // no conversion needed
unsigned int i = 1234;  // signed value 1234 now converted to unsigned

For 0x80000000, it will be unsigned if ints are 32 bits on your platform, since it doesn't fit into a signed int.


Another thing to watch out for, though, is that the behaviour of right-shift is platform-dependent. On some platforms it's sign-preserving (arithmetic) and on some platforms it's a simple bitwise shift (logical).

5
  • Updated to answer your additional query. Jul 3, 2012 at 12:11
  • 0x80000000 is not signed but unsigned on 32-bit and 64-bit systems.
    – ouah
    Jul 3, 2012 at 12:11
  • It is signed if it fits in a signed integer I think this statement is wrong.
    – ouah
    Jul 3, 2012 at 12:15
  • 3
    A decimal integer constant is always signed. For hexadecimal constants, it can fit in a long but if it fit in an unsigned int it will be unsigned int. A signed integer is not a synonym of a signed int. The type long is also a signed integer type.
    – ouah
    Jul 3, 2012 at 12:20
  • What happens in conversion ?? Of signed to unsigned ?
    – Suraj Jain
    Dec 30, 2016 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.