Datasheet
13
Chapter 1: Computer Programming Exposed!
Ignore the first line; it’s not important now. It just provides your program with
some information it can use. I’ll explain exactly what that line means over the
next few chapters.
char* theSandwich = “I hate peanut butter and jelly”;
theSandwich is what is known as a variable. The best way to think of it for
now is as a bucket that holds some kind of data (I get more precise in Chapter
4). char* tells you what kind of variable it is; in this case, theSandwich is
a bunch of characters (text) known as a string (while technically a string is
more than that, for now that description is good enough for our purposes). I
hate peanut butter and jelly is the data that the variable contains.
printf (theSandwich);
printf is an instruction that tells the computer to display (this is called an
operation) whatever data is in the theSandwich bucket.
You can also safely ignore the last two lines for the time being.
return 0;
}
Figure 1-2 shows the similarities between the program and the recipe for
making a sandwich.
Figure 1-2:
A computer
program
can be com-
pared to a
peanut but-
ter and jelly
sandwich
recipe.
Recipe: Peanut Butter and Jelly Sandwich
Ingredients
Peanut butter
Jelly
2 slices of bread
Directions
Place the two slices of bread close to each other
Spread peanut butter on one slice of bread
Spread jelly on the other slice of bread
Put one slice of bread on top of the other
1.
2.
3.
4.
int main(int argc, char*argv[]) {
char* theSandwich =
“I hate peanut butter and jelly”;
printf (theSandwich);
return 0;
}
Variables
Data
Operation –
print the data
Instructions
You can think of the following ingredients as variables that represent the
data. For example, peanut butter is the name you give to pureed peanuts
(and whatever else is in peanut butter), jelly the name you give to some
fruit that’s been processed and put in a jar, and so on.
05_522752-ch01.indd 1305_522752-ch01.indd 13 8/27/09 9:45:02 PM8/27/09 9:45:02 PM