Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [8]
char
A char is a one-byte integer that we usually treat as a character, like the letter 'a'.
pointers
A pointer holds a memory address. It is declared using the asterisk character. For example, a variable declared as int * can hold a memory address where an int is stored. It doesn’t hold the actual number’s value, but if you know the address of the int then you can easily get to its value. Pointers are very useful, and there will be more on pointers later. Much more.
struct
A struct (or structure) is a type made up of other types. You can also create new struct definitions. For example, imagine that you wanted a GeoLocation type that contains two float members: latitude and longitude. In this case, you would define a struct type.
These are the types that a C programmer uses every day. It is quite astonishing what complex ideas can be captured in these five simple ideas.
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 (File → New → New 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.
#include int main (int argc, const char * argv[]) { // Declare the variable called 'weight' of type float float weight; // Put a number in that variable weight = 14.2; // Log it to the user printf("The turkey weighs %f.\n", weight); // Declare another variable of type float float cookingTime; // Calculate the cooking time and store it in the variable // In this case, '*' means 'multiplied by' cookingTime = 15.0 + 15.0 * weight; // Log that to the user printf("Cook it for %f minutes.\n", cookingTime); // End this function and indicate success return 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 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: The turkey weighs 14.200000. Cook it for 228.000000 minutes. Now click the 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: float weight; we say that you are “declaring the variable weight to be of type float.” In the next line, your variable gets a value: weight = 14.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: float weight = 14.2; Here is another assignment: cookingTime = 15.0 + 15.0 * weight; 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: weight = 14.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. Challenge 4 if/else
Create a new C Command Line Tool named TwoFloats. In its main() function, declare two variables of type float and assign each of them a number with a decimal point, like 3.14 or 42.0. Declare another variable of type double and assign it the sum of the two floats. Print the result using printf(). Refer to the code in this chapter if you need to check your syntax.
An important idea in programming is taking different actions depending on circumstances. Have all the billing fields in the order