liveBook · Manning

Integer

As explained in Modern C

signed int is a C signed integer type capable of representing both negative and nonnegative integer values.

A minus sign is an operation

On a platform where signed int ranges from −32768 to 32767, the decimal constant 32768 does not fit in int. It has type signed long, so the expression -32768 also has type signed long—even though −32768 is an int value on that platform. section 5.3

TAKEAWAY: An integer value and its type are different things. C's signed integer types represent negative and nonnegative values; signed int may be written int or signed. Signed short, signed long, and signed long long are distinct types with different ranks and potentially different ranges. A type's precision determines its permitted range. section 5.2

Before arithmetic, narrow integer types are promoted to a wider type—on the platforms described here, to signed int without changing their value. Thus an expression's arithmetic type need not be the narrow type of a value used in it. section 5.2

Decimal integer constants use the first signed type that fits. The leading minus in -1 is negation, not part of the constant. Hexadecimal and octal constants follow different type-selection rules and can be unsigned: under the stated 32-bit assumption, 0x80000000 is unsigned, as is the result of negating it. Spelling and value alone do not settle an expression's type. section 5.3

Table 5.3. Examples for constants and their types. This is under the supposition that signed and unsigned have the commonly used representation with 32 bits (view table figure)

Constant x

Value

Type

Value of –x

2147483647 +2147483647 signed –2147483647
2147483648 +2147483648 signed long –2147483648
4294967295 +4294967295 signed long –4294967295
0x7FFFFFFF +2147483647 signed –2147483647
0x80000000 +2147483648 unsigned +2147483648
0xFFFFFFFF +4294967295 unsigned +1
1 +1 signed –1
1U +1 unsigned +4294967295

Compare constants with their types and negated values; the table explicitly assumes commonly used 32-bit signed and unsigned representations. section 5.3

A signed type has a sign bit, while its positive values have the same representation as those of the corresponding unsigned type. The C rules described here allow sign-and-magnitude, ones’ complement, and two’s complement representations. Two’s complement is common on modern platforms, but that observation is not a reason to assume every implementation uses it. Arithmetic overflow is a separate question from how the sign is represented. section 5.7.5

How Modern C covers it

How deep it goes
Explains type ranks, promotions, literal typing, and sign representations; raises arithmetic overflow as a separate implementation question.
Examples
Compares decimal and hexadecimal constants and works through a 16-bit int range where -32768 has type signed long.
What it assumes
Builds the type-class distinction from basic C keywords, then uses arithmetic expressions and literals to develop the rules.
What you can do after
Evaluate/understand the type of integer constants and why negation or promotion may give an expression an unexpected type.
How it connects
Connects signed integers to unsigned integers, integer promotion, numeric literals, precision, and sign representation.

Also explained in

Compare how 15 books explain integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.