aboutsummaryrefslogtreecommitdiffstats
path: root/doc/info/chap/integer-types.texinfo
blob: cbc4b4597af735d293bf539cf49e6bd6d28f7bf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
@node Integer types
@chapter Integer types


@menu
* Builtin integer types::                     Integer types provided by the compiler.
* Portable integer types::                    Integer types provided by the standard library.
@end menu



@node Builtin integer types
@section Builtin integer types

@cpindex Integer types
@cpindex Types, integer
@hfindex limits.h
The C programming languages, provides several
integer types. All of these have limited
portability, and are intrinsic (they are
provided by the compiler, not the standard library.)
All constants named in this section of are
made available by including the header file
@file{<limits.h>}.

@table @code
@item char
@tpindex char
@lvindex CHAR_BIT
@lvindex CHAR_MIN
@lvindex CHAR_MAX
The smallest addressable unit of the machine
that can contain a basic character set.
It is either signed or unsigned, depending
on the implementation. It contains @code{CHAR_BIT}
bits. @sc{POSIX} requires that @code{CHAR_BIT == 8},
however the C standard only specifies that
@code{CHAR_BIT >= 8}. The range of this type
is [@code{CHAR_MIN}, @code{CHAR_MAX}].

@item signed char
@tpindex signed char
@lvindex SCHAR_MIN
@lvindex SCHAR_MAX
@iftex
Signed version of @code{char}. It is guaranteed
to have a range of either
[@math{-2}@sup{@code{CHAR_BIT}@math{~-~1}}@math{~+~1},
@math{2}@sup{@code{CHAR_BIT}@math{~-~1}}@math{~-~1}]
or
[@math{-2}@sup{@code{CHAR_BIT}@math{~-~1}},
@math{2}@sup{@code{CHAR_BIT}@math{~-~1}}@math{~-~1}].
The exact range is [@code{SCHAR_MIN}, @code{SCHAR_MAX}].
@end iftex
@ifnottex
Signed version of @code{char}. It is
guaranteed to have a range of at least either
[-2^(@code{CHAR_BIT} - 1) + 1, 2^(@code{CHAR_BIT} - 1) - 1] or
[-2^(@code{CHAR_BIT} - 1), 2^(@code{CHAR_BIT} - 1) - 1].
The exact range is [@code{SCHAR_MIN}, @code{SCHAR_MAX}].
@end ifnottex

@item unsigned char
@tpindex unsigned char
@lvindex UCHAR_MIN
@lvindex UCHAR_MAX
@iftex
Unsigned version of @code{char}. It is guaranteed
to have a range of exactly
[0, @math{2}@sup{@code{CHAR_BIT}}@math{~-~1}],
or equivalently [0, @code{UCHAR_MAX}].
@end iftex
@ifnottex
Unsigned version of @code{char}. It is guaranteed
to have a range of exactly
[0, 2^@code{CHAR_BIT} -1],
or equivalently [0, @code{UCHAR_MAX}].
@end ifnottex

@item short int
@itemx short
@itemx signed short int
@itemx signed short
@tpindex short int
@tpindex signed short int
@lvindex SHORT_MIN
@lvindex SHORT_MAX
Signed integer type of at least 16 bits. Its range is
guaranteed contain at least [@math{-32767}, @math{32767}].
The exact range [@code{SHORT_MIN}, @code{SHORT_MAX}].

@item unsigned short int
@itemx unsigned short
@tpindex unsigned short int
@lvindex USHORT_MAX
Unsigned integer type of at least 16 bits. Its range is
guaranteed contain at least [0, @math{65535}].
The exact range [0, @code{USHORT_MAX}].

@item signed int
@itemx int
@tpindex int
@tpindex signed int
@lvindex INT_MIN
@lvindex INT_MAX
Signed integer type of at least 16 bits. Its range is
guaranteed contain at least [@math{-32767}, @math{32767}].
The exact range [@code{INT_MIN}, @code{INT_MAX}].

@item unsigned int
@tpindex unsigned int
@lvindex UINT_MAX
Unsigned integer type of at least 16 bits. Its range is
guaranteed contain at least [0, @math{65535}].
The exact range [0, @code{UINT_MAX}].

@item signed
@tpindex signed
Alias for @code{signed int} which is used when the
valid value range is the least guaranteed range. Thus
The range may exceed [@math{-32767}, @math{32767}],
but values outside that range [[@math{-32767},
@math{32767}]] should not be used.

@item unsigned
@tpindex unsigned
Alias for @code{unsigned int} which is used when the
valid value range is the least guaranteed range. Thus
The range may exceed [0, @math{65535}], but values
outside that range [[0, @math{65535}]] should not be used.

@item long int
@itemx long
@itemx signed long int
@itemx signed long
@tpindex signed long int
@tpindex long int
@lvindex LONG_MIN
@lvindex LONG_MAX
Signed integer type of at least 32 bits. Its range is
guaranteed contain at least [@math{-2147483647},
@math{2147483647}]. The exact range [@code{LONG_MIN},
@code{LONG_MAX}].

@item unsigned long int
@itemx unsigned long
@tpindex unsigned long int
@lvindex ULONG_MAX
Unsigned integer type of at least 32 bits. Its range is
guaranteed contain at least [0, @math{4294967295}].
The exact range [0, @code{ULONG_MAX}].

@item long long int
@itemx long long
@itemx signed long long int
@itemx signed long long
@tpindex long long int
@tpindex signed long long int
@lvindex LLONG_MIN
@lvindex LLONG_MAX
Signed integer type of at least 64 bits. Its range is
guaranteed contain at least [@math{-9223372036854775807},
@math{9223372036854775807}]. This type was added in C99.
The exact range [@code{LLONG_MIN}, @code{LLONG_MAX}].

@item unsigned long long int
@itemx unsigned long long
@tpindex unsigned long long int
@lvindex ULLONG_MAX
Unsigned integer type of at least 64 bits. Its range is
guaranteed contain at least [0, @math{18446744073709551615}].
This type was added in C99. The exact range [0,
@code{ULLONG_MAX}].
@end table



@node Portable integer types
@section Portable integer types

@cpindex Integer types
@cpindex Types, integer
@hfindex stdint.h
@hfindex inttypes.h
@sc{ISO}@tie{}C99 added integers types of increased
portability. These, and constants describing their
properties, are made available by including either
of the header files @file{<stdint.h>} or
@file{<inttypes.h>}. @file{<inttypes.h>} includes
@file{<stdint.h>}. All integer types have limited
portability, but they --- with exceptions ---
are available even if @code{_PORTABLE_SOURCE} or
@code{_LIBRARY_HEADER} is defined.

@table @code
@item int@i{N}_t
@tpindex int@i{N}_t
@lvindex INT@i{N}_MIN
@lvindex INT@i{N}_MAX
Signed integer types of exaclty @i{N} bits.
They have do not have any padding bits, and
use two's complement. They are only defined
if there are matching integer types. Thus,
if the compile do not use two's complement,
none will defined.

Their range are [@code{INT@i{N}_MIN},
@code{INT@i{N}_MAX}], where
@code{INT@i{N}_MIN == -@code{INT@i{N}_MAX} - 1},
and @code{INT@i{N}_MAX == (1 << (@i{N} - 1)) - 1}.
Numerical literals, without type suffixes, can
be @sc{CPP}-safely casted to their type with
the macros @code{INT@i{N}_C(value)}.

@code{int@i{N}_t}, where @i{N} is 8, 16, 32,
or 64, must be defined if possible.

These types are standard library-independent,
but compiler-dependent and machine-dependent.

@item uint@i{N}_t
@tpindex uint@i{N}_t
@lvindex UINT@i{N}_MAX
Unsigned integer types of exaclty @i{N} bits.
They have do not have any padding bits. They
are only defined if there are matching integer
types.

Their range are [0, @code{UINT@i{N}_MAX}], where
@code{UINT@i{N}_MAX == (1 << @i{N}) - 1}.
Numerical literals, without type suffixes, can
be @sc{CPP}-safely casted to their type with
the macros @code{UINT@i{N}_C(value)}.

@code{int@i{N}_t}, where @i{N} is 8, 16, 32,
or 64, must be defined if possible.

These types are standard library-independent
and compiler-independent, but machine-dependent.

@item int_least@i{N}_t
@tpindex int_least@i{N}_t
@lvindex INT_LEAST@i{N}_MIN
@lvindex INT_LEAST@i{N}_MAX
Signed integer types with ranges of at least
[@code{INT@i{N}_MIN}, @code{INT@i{N}_MAX}].
The types are as narrow as possible, they
do not however need to use two's complement.
Numerical literals, without type suffixes, can
be @sc{CPP}-safely casted to their type with
the macros @code{INT_LEAST@i{N}_C(value)}.

Types where @i{N} is 8, 16, 32, or 64 are
guaranteed to be defined.

These types are standard library-independent,
but compiler-dependent and machine-dependent.

The exact ranges of these types are
[@code{INT_LEAST@i{N}_MIN}, @code{INT_LEAST@i{N}_MAX}].

@item uint_least@i{N}_t
@tpindex uint_least@i{N}_t
@lvindex UINT_LEAST@i{N}_MAX
Unsigned integer types with ranges of at least
[0, @code{UINT@i{N}_MAX}]. The types are
as narrow as possible. Numerical literals,
without type suffixes, can be @sc{CPP}-safely
casted to their type with the macros
@code{UINT_LEAST@i{N}_C(value)}.

Types where @i{N} is 8, 16, 32, or 64 are
guaranteed to be defined.

These types are standard library-independent
and compiler-independent, but machine-dependent.

The exact ranges of these types are
[0, @code{UINT_LEAST@i{N}_MAX}].

@item int_fast@i{N}_t
@cpindex Fast integers
@cpindex Integers, fast
@tpindex int_fast@i{N}_t
@lvindex INT_FAST@i{N}_MIN
@lvindex INT_FAST@i{N}_MAX
Signed integer types with ranges of at least
[@code{INT@i{N}_MIN}, @code{INT@i{N}_MAX}].
They do not need to use two's complement.
Numerical literals, without type suffixes, can
be @sc{CPP}-safely casted to their type with
the macros @code{INT_FAST@i{N}_C(value)}.

Types where @i{N} is 8, 16, 32, or 64 are
guaranteed to be defined.

The underlaying types of these types are selected,
by the C standard library, to the types that
are thought to be the fastest type to operate
over in most use-cases.
For example, @code{int_fast16_t} can actually
be a @code{int64_t} (@code{long long int}).

These types are standard library-dependent,
compiler-dependent and machine-dependent.

The exact ranges of these types are
[@code{INT_FAST@i{N}_MIN}, @code{INT_FAST@i{N}_MAX}].

@item uint_fast@i{N}_t
@cpindex Fast integers
@cpindex Integers, fast
@tpindex uint_fast@i{N}_t
@lvindex UINT_FAST@i{N}_MAX
Signed integer types with ranges of at least
[0, @code{UINT@i{N}_MAX}]. Numerical literals,
without type suffixes, can be @sc{CPP}-safely
casted to their type with the macros
@code{UINT_FAST@i{N}_C(value)}.

Types where @i{N} is 8, 16, 32, or 64 are
guaranteed to be defined.

The underlaying types of these types are selected,
by the C standard library, to the types that
are thought to be the fastest type to operate
over in most use-cases.
For example, @code{uint_fast16_t} can actually
be a @code{uint64_t} (@code{unsigned long long int}).

These types are standard library-dependent and
machine-dependent, but compiler-independent.

The exact ranges of these types are
[0, @code{UINT_FAST@i{N}_MAX}].

@item intmax_t
@tpindex intmax_t
@lvindex INTMAX_MIN
@lvindex INTMAX_MAX
Widest natively available signed integer type.
If the compiler provides wider integers than the
machine, the widest integer provided by the machine
(not the compiler) is used.

Because @code{long long int} has at least 64 bits,
this type has at least 64 bits too.

This type is standard library-independent and
compiler-independent, but machine-dependent.

The range if this type is [@code{INTMAX_MIN},
@code{INTMAX_MAX}].

@item uintmax_t
@tpindex uintmax_t
@lvindex UINTMAX_MAX
Widest natively available unsigned integer type.
If the compiler provides wider integers than the
machine, the widest integer provided by the machine
(not the compiler) is used.

Because @code{long long int} has at least 64 bits,
this type has at least 64 bits too.

This type is standard library-independent and
compiler-independent, but machine-dependent.

The range if this type is [0, @code{UINTMAX_MAX}].

@item intptr_t
@cpindex Pointers
@tpindex intptr_t
@lvindex INTPTR_MIN
@lvindex INTPTR_MAX
Signed integer type cast to or from
@code{void*} without loss of precision. It can
point to any address.

This type is standard library-independent and
compiler-independent, but machine-dependent.

The range if this type is [@code{INTPTR_MIN},
@code{INTMAX_MAX}].

@item uintptr_t
@cpindex Pointers
@tpindex uintptr_t
@lvindex UINTPTR_MAX
Unsigned integer type cast to or from
@code{void*} without loss of precision. It can
point to any address.

This type is standard library-independent and
compiler-independent, but machine-dependent.

The range if this type is [0, @code{UINTPTR_MAX}].

@end table

Types where @i{N} is neither 8, 16, 32, nor 64,
are not available if @code{_PORTABLE_SOURCE} or
@code{_LIBRARY_HEADER} is defined. @command{slibc},
currently, does not define any integer types where
@i{N} is neither 8, 16, 32, nor 64.