-
Notifications
You must be signed in to change notification settings - Fork 7
/
ex3.tex
108 lines (83 loc) · 4.24 KB
/
ex3.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
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
\chapter{Exercise 3: Formatted Printing}
Keep that \file{Makefile} around since it'll help you spot errors
and we'll be adding to it when we need to automate more things.
Many programming languages use the C way of formatting output, so let's try it:
\begin{code}{ex3.c}
<< d['code/ex3.c|pyg|l'] >>
\end{code}
Once you have that, do the usual \shell{make ex3} to build it and run it.
Make sure you \emph{fix all warnings}.
This exercise has a whole lot going on in a small amount of code so
let's break it down:
\begin{enumerate}
\item First you're including another "header file" called \file{stdio.h}. This tells the compiler that you're going to use the "standard Input/Output functions". One of those is \ident{printf}.
\item Then you're using a variable named \ident{age} and setting it to 10.
\item Next you're using a variable \ident{height} and setting it to 72.
\item Then you use the \ident{printf} function to print the age and height
of the tallest 10 year old on the planet.
\item In the \ident{printf} you'll notice you're passing in a string, and
it's a format string like in many other languages.
\item After this format string, you put the variables that should be
"replaced" into the format string by \ident{printf}.
\end{enumerate}
The result of doing this is you are handing \ident{printf} some variables
and it is constructing a new string then printing that new string to the
terminal.
\section{What You Should See}
When you do the whole build you should see something like this:
\begin{Terminal}{Building and running ex3.c}
\begin{lstlisting}
<< d['code/ex3.out|dexy'] >>
\end{lstlisting}
\end{Terminal}
Pretty soon I'm going to stop telling you to run \file{make} and what
the build looks like, so please make sure you're getting this right
and that it's working.
\section{External Research}
In the \emph{Extra Credit} section of each exercise I may have you go
find information on your own and figure things out. This is an important
part of being a self-sufficient programmer. If you constantly run to
ask someone a question before trying to figure it out first then you
never learn to solve problems independently. This leads to you never
building confidence in your skills and always needing someone else
around to do your work.
The way you break this habit is to \emph{force} yourself to try to answer
your own questions first, and to confirm that your answer is right. You
do this by trying to break things, experimenting with your possible answer,
and doing your own research.
For this exercise I want you to go online and find out \emph{all} of the
\ident{printf} escape codes and format sequences. Escape codes are
\verb|\n| or \verb|\t| that let you print a newline or tab (respectively).
Format sequences are the \verb|%s| or \verb|%d| that let you print a
string or a integer. Find all of the ones available, how you can
modify them, and what kind of "precisions" and widths you can do.
From now on, these kinds of tasks will be in the Extra Credit and you
should do them.
\section{How To Break It}
Try a few of these ways to break this program, which may or may
not cause it to crash on your computer:
\begin{enumerate}
\item Take the \ident{age} variable out of the first \ident{printf} call
then recompile. You should get a couple of warnings.
\item Run this new program and it will either crash, or print out a really
crazy age.
\item Put the \ident{printf} back the way it was, and then don't set \ident{age}
to an initial value by changing that line to \verb|int age;| then
rebuild and run again.
\end{enumerate}
\begin{Terminal}{Breaking ex3.c}
\begin{lstlisting}
<< d['code/ex3.bad.out|dexy'] >>
\end{lstlisting}
\end{Terminal}
\section{Extra Credit}
\begin{enumerate}
\item Find as many other ways to break \file{ex3.c} as you can.
\item Run \verb|man 3 printf| and read about the other '\%' format
characters you can use. These should look familiar if you used
them in other languages (\ident{printf} is where they come from).
\item Add \file{ex3} to your \file{Makefile}'s \ident{all} list. Use this
to \verb|make clean all| and build all your exercises so far.
\item Add \file{ex3} to your \file{Makefile}'s \ident{clean} list as well.
Now use \verb|make clean| will remove it when you need to.
\end{enumerate}