forked from mwilliams/lcthw-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn-c-the-hard-waych22.txt
476 lines (327 loc) · 11.5 KB
/
learn-c-the-hard-waych22.txt
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
Learn C The Hard Way A Learn Code The Hard Way Book
* Book
* Comments
* Video Courses
* Related Books
[next] [prev] [prev-tail] [tail] [up]
Chapter 22
Exercise 21: Advanced Data Types And Flow Control
This exercise will be a complete compendium of the available C data
types and flow control structures you can use. It will work as a
reference to complete your knowledge, and won't have any code for you
to enter. I'll have you memorize some of the information by creating
flash cards so you can get the important concepts solid in your mind.
For this exercise to be useful, you should spend at least a week
hammering the content and filling out all the element I have missing
here. You'll be writing out what each one means, and then writing a
program to confirm what you've researched.
22.1 Available Data Types
int
Stores a regular integer, defaulting to 32 bits in size.
double
Holds a large floating point number.
float
Holds a smaller floating point number.
char
Holds a single 1 byte character.
void
Indicates "no type" and used to say a function returns nothing,
or a pointer has no type as in void *thing.
enum
Enumerated types, work as integers, convert to integers, but
give you symbolic names for sets. Some compilers will warn you
when you don't cover all elements of an enum in
switch-statements.
22.1.1 Type Modifiers
unsigned
Changes the type so that it does not have negative numbers,
giving you a larger upper bound but nothing lower than 0.
signed
Gives you negative and positive numbers, but halves your upper
bound in exchange for the same lower bound negative.
long
Uses a larger storage for the type so that it can hold bigger
numbers, usually doubling the current size.
short
Uses smaller storage for the type so it stores less, but takes
half the space.
22.1.2 Type Qualifiers
const
Indicates the variable won't change after being initialized.
volatile
Indicates that all bets are off, and the compiler should leave
this alone and try not to do any fancy optimizations to it. You
usually only need this if you're doing really weird stuff to
your variables.
register
Forces the compiler to keep this variable in a register, and the
compiler can just ignore you. These days compilers are better at
figuring out where to put variables, so only use this if you
actually can measure it improving the speed.
22.1.3 Type Conversion
C uses a sort of "stepped type promotion" mechanism, where it looks at
two operands on either side of an expression, and promotes the smaller
side to match the larger side before doing the operation. If one side
of an expression is on this list, then the other side is converted to
that type before the operation is done, and this goes in this order:
1. long double
2. double
3. float
4. int (but only char and short int);
5. long
If you find yourself trying to figure out how your conversions are
working in an expression, then don't leave it to the compiler. Use
explicit casting operations to make it exactly what you want. For
example, if you have:
long + char - int * double
Rather than trying to figure out if it will be converted to double
correctly, just use casts:
(double)long - (double)char - (double)int * double
Putting the type you want in parenthesis before the variable name is
how you force it into the type you really need. The important thing
though is always promote up, not down. Don't cast long into char unless
you know what you're doing.
22.1.4 Type Sizes
The stdint.h defines both a set of typdefs for exact sized integer
types, as well as a set of macros for the sizes of all the types. This
is easier to work with than the older limits.h since it is consistent.
The types defined are:
int8_t
8 bit signed integer.
uint8_t
8 bit unsigned integer.
int16_t
16 bit signed integer.
uint16_t
16 bit unsigned integer.
int32_t
32 bit signed integer.
uint32_t
32 bit unsigned integer.
int64_t
64 bit signed integer.
uint64_t
64 bit unsigned integer.
The pattern here is of the form (u)int(BITS)_t where a u is put in
front to indicate "unsigned", then BITS is a number for the number of
bits. This pattern is then repeated for macros that return the maximum
values of these types:
INTN_MAX
Maximum positive number of the signed integer of bits N.
INTN_MIN
Minimum negative number of signed integer of bits N.
UINTN_MAX
Maximum positive number of unsigned integer of bits N. Since
it's unsigned the minimum is 0 and can't have a negative value.
There are also macros in stdint.h for sizes of the size_t type,
integers large enough to hold pointers, and other handy size defining
macros. Compilers have to at least have these, and then they can allow
other larger types.
Here is a full list should be in stdint.h:
int_leastN_t
holds at least N bits.
uint_leastN_t
holds at least N bits unsigned.
INT_LEASTN_MAX
max value of the matching leastN type.
INT_LEASTN_MIN
min value of the matching leastN type.
UINT_LEASTN_MAX
unsigned maximum of the matching N type.
int_fastN_t
similar to int_leastN_t but asking for the "fastest" with at
least that precision.
uint_fastN_t
unsigned fastest least integer.
INT_FASTN_MAX
max value of the matching fastestN type.
INT_FASTN_MIN
min value of the matching fastestN type.
UINT_FASTN_MAX
unsigned max value of the matching fastestN type.
intptr_t
a signed integer large enough to hold a pointer.
uintptr_t
an unsigned integer large enough to hold a pointer.
INTPTR_MAX
max value of a intptr_t.
INTPTR_MIN
min value of a intptr_t.
UINTPTR_MAX
unsigned max value of a uintptr_t.
intmax_t
biggest number possible on that system.
uintmax_t
biggest unsigned number possible.
INTMAX_MAX
largest value for the biggest signed number.
INTMAX_MIN
smallest value for the biggest signed number.
UINTMAX_MAX
largest value for the biggest unsigned number.
PTRDIFF_MIN
minimum value of ptrdiff_t.
PTRDIFF_MAX
maximum value of ptrdiff_t.
SIZE_MAX
maximum of a size_t.
22.2 Available Operators
This is a comprehensive list of all the operators you have in the C
language. In this list, I'm indicating the following:
(binary)
The operator has a left and right: X + Y.
(unary)
The operator is on its own: -X.
(prefix)
The operator comes before the variable: ++X.
(postfix)
Usually the same as the (prefix) version, but placing it after
gives it a different meaning: X++.
(ternary)
There's only one of these, so it's actually called the ternary
but it means "three operands": X ? Y : Z.
22.2.1 Math Operators
These are your basic math operations, plus I put () in with these since
it calls a function and is close to a "math" operation.
()
Function call.
* (binary)
multiply.
/
divide.
+ (binary)
addition.
+ (unary)
positive number.
++ (postfix)
read, then increment.
++ (prefix)
increment, then read.
-- (postfix)
read, then decrement.
-- (prefix)
decrement, then read.
- (binary)
subtract.
- (unary)
negative number.
22.2.2 Data Operators
These are used to access data in different ways and forms.
->
struct pointer access.
.
struct value access.
[] Array index.
sizeof
size of a type or variable.
& (unary)
Address of.
* (unary)
Value of.
22.2.3 Logic Operators
These handle testing equality and inequality of variables.
!=
does not equal.
<
less than.
<=
less than or equal.
==
equal (not assignment).
>
greater than.
>=
greater than or equal.
22.2.4 Bit Operators
These are more advanced and for shifting and modifying the raw bits in
integers.
& (binary)
Bitwise and.
<<
Shift left.
>>
Shift right.
^
bitwise xor (exclusive or).
|
bitwise or.
~
compliment (flips all the bits).
22.2.5 Boolean Operators
Used in truth testing. Study the ternary operator carefully, it is very
handy.
!
not.
&&
and.
||
or.
?:
Ternary truth test, read X ? Y : Z as "if X then Y else Z".
22.2.6 Assignment Operators
Compound assignment operators that assign a value, and/or perform an
operation at the same time. Most of the above operations can also be
combined into a compound assignment operator.
=
assign.
%=
modulus assign.
&=
bitwise and assign.
*=
multiply assign.
+=
plus assign.
-=
minus assign.
/=
divide assign.
<<=
shift left, assign.
>>=
shift right, assign.
^=
bitwise xor, assign.
|=
bitwise or, assign.
22.3 Available Control Structures
There's a few control structures you haven't encountered yet:
do-while
do { ... } while(X); First does the code in the block, then
tests the X expression before exiting.
break
Put this in a loop, and it breaks out ending it early.
continue
Stops the body of a loop and jumps to the test so it can
continue.
goto
Jumps to a spot in the code where you've placed a label:, and
you've been using this in the dbg.h macros to go to the error:
label.
22.3.1 Extra Credit
1. Read stdint.h or a description of it and write out all the possible
available size identifiers.
2. Go through each item here and write out what it does in code.
Research it so you know you got it right by looking it up online.
3. Get this information solid as well by making flash cards and
spending 15 minutes a day memorizing it.
4. Create a program that prints out examples of each type and confirm
that your research is right.
[next] [prev] [prev-tail] [front] [up]
__________________________________________________________________
Please enable JavaScript to view the comments powered by Disqus.
Take An Online Video Course
You can sign up for a video course at:
http://www.udemy.com/learn-c-the-hard-way/
This course is currently being built at the same time that the book is
being built, but if you sign up now then you get early access to both
the videos and PDF of the book.
Related Books
You might want to check out these other books in the series:
1. Learn Ruby The Hard Way
2. Learn Regex The Hard Way
3. Learn SQL The Hard Way
4. Learn C The Hard Way
5. Learn Python The Hard Way
I'll be referencing other books shortly.
Copyright 2011 Zed A. Shaw. All Rights Reserved.