Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [16]
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 #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 #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
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:
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:
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.