Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [16]

By Root 460 0
to how 0 and 1 work in if statements; because 1 is true and 0 is false, it’s natural to think of 1 as success and 0 as failure. So think of main() as returning an error report. In that case, 0 is good news! Success is a lack of errors.

To make this clearer, some programmers use the constants EXIT_SUCCESS and EXIT_FAILURE, which are just aliases for 0 and 1 respectively. These constants are defined in the header file stdlib.h:

#include

#include

float fahrenheitFromCelsius(float cel)

{

float fahr = cel * 1.8 + 32.0;

printf("%f Celsius is %f Fahrenheit\n", cel, fahr);

return fahr;

}

int main(int argc, const char * argv[])

{

float freezeInC = 0;

float freezeInF = fahrenheitFromCelsius(freezeInC);

printf("Water freezes at %f degrees Fahrenheit\n", freezeInF);

return EXIT_SUCCESS;

}

In this book, we will generally use 0 instead of EXIT_SUCCESS.

Global and static variables


In this chapter, we talked about local variables that only exist while a function is running. There are also variables that can be accessed from any function at any time. We call these global variables. To make a variable global, you declare it outside of a particular function. For example, you could add a lastTemperature variable that holds the temperature that was converted from Celsius. Add a global variable to the program:

#include

#include

// Declare a global variable

float lastTemperature;

float fahrenheitFromCelsius(float cel)

{

lastTemperature = cel;

float fahr = cel * 1.8 + 32.0;

printf("%f Celsius is %f Fahrenheit\n", cel, fahr);

return fahr;

}

int main(int argc, const char * argv[])

{

float freezeInC = 0;

float freezeInF = fahrenheitFromCelsius(freezeInC);

printf("Water freezes at %f degrees Fahrenheit\n", freezeInF);

printf("The last temperature converted was %f\n", lastTemperature);

return EXIT_SUCCESS;

}

Any complex program will involve dozens of files containing different functions. Global variables are available to the code in every one of those files. Sometimes sharing a variable between different files is what you want. But, as you can imagine, having a variable that can be accessed by multiple functions can also lead to great confusion. To deal with this, we have static variables. A static variable is like a global variable in that it is declared outside of any function. However, a static variable is only accessible from the code in the file where it was declared. So you get the non-local, “exists outside of any function” benefit while avoiding the “you touched my variable!” issue.

You can change your global variable to a static variable, but because you have only one file, main.c, it will have no effect whatsoever.

// Declare a static variable

static float lastTemperature;

Both static and global variables can be given an initial value when they are created:

// Initialize lastTemperature to 50 degrees

static float lastTemperature = 50.0;

If you don’t give them an initial value, they are automatically initialized to zero.

In this chapter, you have learned about functions. When we get to Objective-C in Part III, you will hear the word method – a method is very, very similar to a function.

Challenge


The interior angles of a triangle must add up to 180 degrees. Create a new C Command Line Tool named Triangle. In main.c, write a function that takes the first two angles and returns the third. Here’s what it will look like when you call it:

#include

// Add your new function here

int main(int argc, const char * argv[])

{

float angleA = 30.0;

float angleB = 60.0;

float angleC = remainingAngle(angleA, angleB);

printf("The third angle is %.2f\n", angleC);

return 0;

}

The output should be:

The third angle is 90.00

6

Numbers


We’ve used numbers to measure and display temperature, weight, and how long to cook a turkey. Now let’s take a closer look at how numbers work in C programming. On a computer, numbers come in two flavors: integers and floating-point numbers. You have already used both.

Return Main Page Previous Page Next Page

®Online Book Reader