liveBook · Manning

Unsigned integer

As explained in Modern C

An unsigned integer type is a C integer type whose representable values are non-negative.

Consider an expression such as `a + 5`, where `a` has type `unsigned`. The `+` symbol is an operator, and `a` and `5` are its operands. An unsigned integer represents numbers that cannot be negative. section 4.0

C provides several unsigned integer types: `unsigned char`, `unsigned short`, `unsigned int` (also written `unsigned`), `unsigned long`, and `unsigned long long`. Their precision determines the valid range of values. For an n-bit unsigned representation, the largest value follows the pattern 2ⁿ − 1; examples include 65,535, 4,294,967,295, and 18,446,744,073,709,551,615. section 4.0 section 5.2

Narrow integer types do not participate directly in arithmetic. Before an arithmetic expression is considered, they are promoted to `signed int` while preserving their value. The wider unsigned types remain unpromoted. section 5.2

Printing an unsigned value

Use the `%u` conversion for decimal output of an unsigned integer. Use `%x` when the intended meaning is a bit pattern. The conversion and modifier must match the argument type; an inappropriate format specifier or modifier makes the behavior undefined. section 8.3.4

Table 4.1. Value operators The Form column gives the syntactic form of the operation, where @ represents the operator and a and possibly b denote values that serve as operands. For arithmetic and bit operations, the type of the result is a type that reconciles the types of a and b. For some of the operators, the Nick column gives an alternative form of the operator, or lists a combination of operators that has special meaning. Most of the operators and terms will be discussed later. (view table figure)
     

type restriction

 

Operator

Nick

Form

a

b

Result

 
    a Narrow   Wide Promotion
+ -   a@b Pointer Integer Pointer Arithmetic
+ - * /   a@b Arithmetic Arithmetic Arithmetic Arithmetic
+ -   @a Arithmetic   Arithmetic Arithmetic
%   a@b Integer Integer Integer Arithmetic
~ compl @a Integer   Integer Bit
& bitand a@b Integer Integer Integer Bit
| bitor          
^ xor          
<< >>   a@b Integer Positive Integer Bit
== < > <= >=   a@b Scalar Scalar 0,1 Comparison
!= not_eq a@b Scalar Scalar 0,1 Comparison
  !!a a Scalar   0,1 Logic
!a not @a Scalar   0,1 Logic
&& || and or a@b Scalar Scalar 0,1 Logic
.   a@m struct   Value Member
*   @a Pointer   Object Reference
[]   a[b] Pointer Integer Object Member
->   a@m struct Pointer   Object Member
()   a(b ...) Function pointer   Value Call
sizeof   @ a None   size_t Size, ICE
_Alignof alignof @(a) None   size_t Alignment, ICE

Use the value-operator table to relate arithmetic syntax, operands, promotions, and result types. section 4.0

How Modern C covers it

How deep it goes
Defines the type family, ranges, promotions, operators, and output formats, but defers most operator rules to reference tables.
Examples
Uses `a + 5`, representative maximum values, `SIZE_MAX`, and `%u` or `%x` output formats.
What you can do after
Apply unsigned types in arithmetic and choose matching `printf` conversions for decimal values or bit patterns.
How it connects
Connects unsigned integers with signed integers, integer promotions, arithmetic expressions, size_t, operators, and formatted output.

Also explained in

Compare how 9 books explain unsigned 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.