forked from mwilliams/lcthw-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn-c-the-hard-waych7.txt
167 lines (134 loc) · 5.63 KB
/
learn-c-the-hard-waych7.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
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 7
Exercise 6: Types Of Variables
You should be getting a grasp of how a simple C program is structured,
so let's do the next simplest thing which is making some variables of
different types:
__________________________________________________________________
Source 20: ex6.c
1 #include <stdio.h>
2
3 int main(int argc, char *argv[])
4 {
5 int distance = 100;
6 float power = 2.345f;
7 double super_power = 56789.4532;
8 char initial = 'A';
9 char first_name[] = "Zed";
10 char last_name[] = "Shaw";
11
12 printf("You are %d miles away.\n", distance);
13 printf("You have %f levels of power.\n", power);
14 printf("You have %f awesome super powers.\n", super_power);
15 printf("I have an initial %c.\n", initial);
16 printf("I have a first name %s.\n", first_name);
17 printf("I have a last name %s.\n", last_name);
18 printf("My whole name is %s %c. %s.\n",
19 first_name, initial, last_name);
20
21 return 0;
22 }
__________________________________________________________________
In this program we're declaring variables of different types and then
printing them with different printf format strings.
7.1 What You Should See
Your output should look like mine, and you can start to see how the
format strings for C are similar to Python and other languages. They've
been around for a long time.
__________________________________________________________________
Source 21: ex6 output
1$ make ex6
2cc -Wall -g ex6.c -o ex6
3$ ./ex6
4You are 100 miles away.
5You have 2.345000 levels of power.
6You have 56789.453200 awesome super powers.
7I have an initial A.
8I have a first name Zed.
9I have a last name Shaw.
10My whole name is Zed A. Shaw.
11$
__________________________________________________________________
What you can see is we have a set of "types", which are ways of telling
the C compiler what each variable should represent, and then format
strings to match different types. Here's the breakdown of how they
match up:
Integers
You declare Integers with the int keyword, and print them with
%d.
Floating Point
Declared with float or double depending on how big they need to
be (double is bigger), and printed with %f.
Character
Declared with char, written with a ' (single-quote) character
around the char, and then printed with %c.
String (Array of Characters)
Declared with char name[], written with " characters, and
printed with %s.
You'll notice that C makes a distinction between single-quote for char
and double-quote for char[] or strings.
__________________________________________________________________
Note 4: C Type Short-Hand For English
When talking about C types, I will typically write in English char[]
instead of the whole char SOMENAME[]. This is not valid C code, just a
simpler way to talk about types when writing English.
__________________________________________________________________
7.2 How To Break It
You can easily break this program by passing the wrong thing to the
printf statements. For example, if you take the line that prints my
name, but put the initial variable before the first_name in the
arguments, you'll get a bug. Make that change and the compiler will
yell at you, then when you run it you might get a "Segmentation fault"
like I did:
__________________________________________________________________
Source 22: ex6 explosion
1$ make ex6
2cc -Wall -g ex6.c -o ex6
3ex6.c: In function 'main':
4ex6.c:19: warning: format '%s' expects type 'char *', but argument 2 h
as type 'int'
5ex6.c:19: warning: format '%c' expects type 'int', but argument 3 has
type 'char *'
6$ ./ex6
7You are 100 miles away.
8You have 2.345000 levels of power.
9You have 56789.453125 awesome super powers.
10I have an initial A.
11I have a first name Zed.
12I have a last name Shaw.
13Segmentation fault
14$
__________________________________________________________________
Run this change under Valgrind too to see what it tells you about the
error "Invalid read of size 1".
7.3 Extra Credit
1. Come up with other ways to break this C code by changing the
printf, then fix them.
2. Go search for "printf formats" and try using a few of the more
exotic ones.
3. Research how many different ways you can write a number. Try octal,
hexadecimal, and others you can find.
4. Try printing an empty string that's just "".
[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.