A program with variables

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

Now create a new project (FileNewNew Project...). 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​;​

 ​ ​ ​ ​/​/​ ​P​u​t​ ​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​;​
}​

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. Then click the A program with variables button to get to the log navigator. Select the item at the top labeled Debug Turkey to show your output. It 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​.​

Now click the A program with variables button to return to the project navigator. Then select main.c so that you can see your code again. Let’s review what you’ve done here.

In your 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 right-hand 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 right-hand side of the =.

For example, in this line:

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

the expression is just 14.2.

Variables are the building blocks of any program. This is just an introduction to the world of variables. You’ll learn more about how variables work and how to use them as we continue.

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

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