-
Notifications
You must be signed in to change notification settings - Fork 7
/
ex6.tex
82 lines (64 loc) · 2.85 KB
/
ex6.tex
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
\chapter{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:
\begin{code}{ex6.c}
<< d['code/ex6.c|pyg|l'] >>
\end{code}
In this program we're declaring variables of different types
and then printing them with different \ident{printf} format
strings.
\section{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.
\begin{code}{ex6 output}
\begin{lstlisting}
<< d['code/ex6.out|dexy'] >>
\end{lstlisting}
\end{code}
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:
\begin{description}
\item[Integers] You declare Integers with the \ident{int} keyword, and
print them with \verb|%d|.
\item[Floating Point] Declared with \ident{float} or \ident{double} depending
on how big they need to be (double is bigger), and printed with
\verb|%f|.
\item[Character] Declared with \ident{char}, written with a \verb|'| (single-quote)
character around the char, and then printed with \verb|%c|.
\item[String (Array of Characters)] Declared with \verb|char name[]|,
written with \verb|"| characters, and printed with \verb|%s|.
\end{description}
You'll notice that C makes a distinction between single-quote for \ident{char}
and double-quote for \ident{char[]} or strings.
\begin{aside}{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.
\end{aside}
\section{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 \ident{initial} variable before the \ident{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:
\begin{code}{ex6 explosion}
\begin{lstlisting}
<< d['code/ex6.bad.out|dexy'] >>
\end{lstlisting}
\end{code}
Run this change under Valgrind too to see what it tells you about
the error "Invalid read of size 1".
\section{Extra Credit}
\begin{enumerate}
\item Come up with other ways to break this C code by changing the
\ident{printf}, then fix them.
\item Go search for "printf formats" and try using a few of the
more exotic ones.
\item Research how many different ways you can write a number. Try
octal, hexadecimal, and others you can find.
\item Try printing an empty string that's just \verb|""|.
\end{enumerate}