# public williamyangcn /lcthw-cn forked from zedz/lcthw-cn

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

file 79 lines (62 sloc) 3.258 kb
 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 \chapter{Exercise 7: More Variables, Some Math}Let's get familiar with more things you can do with variables by declaring various \ident{ints}, \ident{floats}, \ident{chars}, and \ident{doubles}. We'll then use these in various math expressionsso you get introduced to C's basic math.\begin{code}{ex7.c}<< d['code/ex7.c|pyg|l'] >>\end{code}Here's what's going on in this little bit of nonsense:\begin{description}\item[ex7.c:1-4] The usual start of a C program.\item[ex7.c:5-6] Declare an \ident{int} and \ident{double} for some fake bug data.\item[ex7.c:8-9] Print out those two, so nothing new here.\item[ex7.c:11] Declare a huge number using a new type \ident{long} for storing    big numbers.\item[ex7.c:12-13] Print out that number using \verb|%ld| which adds a modifier    to the usual \verb|%d|. Adding 'l' (the letter ell) means "print this    as a long decimal".\item[ex7.c:15-17] Just more math and printing.\item[ex7.c:19-21] Craft up a depiction of your bug rate compared to the bugs in    the universe, which is a completely inaccurate calculation. It's so small    though that we have to use \verb|%e| to print it in scientific notation.\item[ex7.c:24] Make a character, with a special syntax \verb|'\0'| which creates    a 'nul byte' character. This is effectively the number 0.\item[ex7.c:25] Multiply bugs by this character, which produces 0 for how much you    should care. This demonstrates an ugly hack you find sometimes.\item[ex7.c:26-27] Print that out, and notice I've got a \verb|%%| (two percent        chars) so I can print a '\%' (percent) character.\item[ex7.c:28-30] The end of the \ident{main} function.\end{description}This bit of source is entirely just an exercise, and demonstrates howsome math works. At the end, it also demonstrates something yousee in C, but not in many other languages. To C, a "character" is just aninteger. It's a really small integer, but that's all it is. This meansyou can do math on them, and a lot of software does just that, for goodor bad.This last bit is your first glance at how C gives you direct access to the machine. We'll be exploring that more in later exercises.\section{What You Should See}As usual, here's what you should see for the output:\begin{code}{ex7 output}\begin{lstlisting}<< d['code/ex7.out'] >>\end{lstlisting}\end{code}\section{How To Break It}Again, go through this and try breaking the \ident{printf} by passing inthe wrong arguments. See what happens when you try to print out that\ident{nul\_byte} variable too with \verb|%s| vs. \verb|%c|. When youbreak it, run it under \program{Valgrind} to see what it says about yourbreaking attempts.\section{Extra Credit}\begin{enumerate}\item Make the number you assign to \ident{universe\_of\_defects} various     sizes until you get a warning from the compiler.\item What do these really huge numbers actually print out?\item Change \ident{long} to \ident{unsigned long} and try to find     the number that makes that one too big.\item Go search online to find out what \ident{unsigned} does.\item Try to explain to yourself (before I do in the next exercise)    why you can multiply a \ident{char} and an \ident{int}.\end{enumerate}
Something went wrong with that request. Please try again.