A program with variables

Back in Xcode, you are going to create another project. First, close the AGoodStart project so that you do not accidentally type new code into the old project.

Now create a new project (FileNewProject...). This project will be a C Command Line Tool named Turkey.

In the project navigator, find this project’s main.c file and open it. Edit main.c so that it matches the following code.

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​/​/​ ​D​e​c​l​a​r​e​ ​t​h​e​ ​v​a​r​i​a​b​l​e​ ​c​a​l​l​e​d​ ​'​w​e​i​g​h​t​'​ ​o​f​ ​t​y​p​e​ ​f​l​o​a​t​
 ​ ​ ​ ​f​l​o​a​t​ ​w​e​i​g​h​t​;​

 ​ ​ ​ ​/​/​ ​S​t​o​r​e​ ​a​ ​n​u​m​b​e​r​ ​i​n​ ​t​h​a​t​ ​v​a​r​i​a​b​l​e​
 ​ ​ ​ ​w​e​i​g​h​t​ ​=​ ​1​4​.​2​;​

 ​ ​ ​ ​/​/​ ​L​o​g​ ​i​t​ ​t​o​ ​t​h​e​ ​u​s​e​r​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​h​e​ ​t​u​r​k​e​y​ ​w​e​i​g​h​s​ ​%​f​.​​n​"​,​ ​w​e​i​g​h​t​)​;​

 ​ ​ ​ ​/​/​ ​D​e​c​l​a​r​e​ ​a​n​o​t​h​e​r​ ​v​a​r​i​a​b​l​e​ ​o​f​ ​t​y​p​e​ ​f​l​o​a​t​
 ​ ​ ​ ​f​l​o​a​t​ ​c​o​o​k​i​n​g​T​i​m​e​;​

 ​ ​ ​ ​/​/​ ​C​a​l​c​u​l​a​t​e​ ​t​h​e​ ​c​o​o​k​i​n​g​ ​t​i​m​e​ ​a​n​d​ ​s​t​o​r​e​ ​i​t​ ​i​n​ ​t​h​e​ ​v​a​r​i​a​b​l​e​
 ​ ​ ​ ​/​/​ ​I​n​ ​t​h​i​s​ ​c​a​s​e​,​ ​'​*​'​ ​m​e​a​n​s​ ​'​m​u​l​t​i​p​l​i​e​d​ ​b​y​'​
 ​ ​ ​ ​c​o​o​k​i​n​g​T​i​m​e​ ​=​ ​1​5​.​0​ ​+​ ​1​5​.​0​ ​*​ ​w​e​i​g​h​t​;​

 ​ ​ ​ ​/​/​ ​L​o​g​ ​t​h​a​t​ ​t​o​ ​t​h​e​ ​u​s​e​r​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​C​o​o​k​ ​i​t​ ​f​o​r​ ​%​f​ ​m​i​n​u​t​e​s​.​​n​"​,​ ​c​o​o​k​i​n​g​T​i​m​e​)​;​

 ​ ​ ​ ​/​/​ ​E​n​d​ ​t​h​i​s​ ​f​u​n​c​t​i​o​n​ ​a​n​d​ ​i​n​d​i​c​a​t​e​ ​s​u​c​c​e​s​s​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

(Wondering about the that keeps turning up in your code? You will learn what it does in Chapter 6.)

Build and run the program. You can either click the Run button at the top left of the Xcode window or use the keyboard shortcut Command-R. Your output in the console should look like this:

T​h​e​ ​t​u​r​k​e​y​ ​w​e​i​g​h​s​ ​1​4​.​2​0​0​0​0​0​.​
C​o​o​k​ ​i​t​ ​f​o​r​ ​2​2​8​.​0​0​0​0​0​0​ ​m​i​n​u​t​e​s​.​

Back in your code, let’s review what you have done. In the line of code that looks like this:

 ​ ​ ​ ​f​l​o​a​t​ ​w​e​i​g​h​t​;​

we say that you are declaring the variable weight to be of type float.

In the next line, your variable gets a value:

 ​ ​ ​ ​w​e​i​g​h​t​ ​=​ ​1​4​.​2​;​

You are copying data into that variable. We say that you are assigning a value of 14.2 to that variable.

In modern C, you can declare a variable and assign it an initial value in one line, like this:

 ​ ​ ​ ​f​l​o​a​t​ ​w​e​i​g​h​t​ ​=​ ​1​4​.​2​;​

Here is another assignment:

 ​ ​ ​ ​c​o​o​k​i​n​g​T​i​m​e​ ​=​ ​1​5​.​0​ ​+​ ​1​5​.​0​ ​*​ ​w​e​i​g​h​t​;​

The stuff on the righthand side of the = is an expression. An expression is something that gets evaluated and results in some value. Actually, every assignment has an expression on the righthand side of the =.

For example, in this line:

 ​ ​ ​ ​w​e​i​g​h​t​ ​=​ ​1​4​.​2​;​

the expression is just 14.2.

An expression can have multiple steps. For example, when evaluating the expression 15.0 + 15.0 * weight, the computer first multiplies weight by 15.0 and then adds that result to 15.0. Why does the multiplication come first? We say that multiplication has precedence over addition.

To change the order in which operations are normally executed, you use parentheses:

 ​ ​ ​ ​c​o​o​k​i​n​g​T​i​m​e​ ​=​ ​(​1​5​.​0​ ​+​ ​1​5​.​0​)​ ​*​ ​w​e​i​g​h​t​;​

Now the expression in the parentheses is evaluated first, so the computer first does the addition and then multiplies weight by 30.0.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset