C Types

What does Static Typing Mean?

C is a statically typed language. This means that variables must be declared with a type and cannot change through the lifetime of the program. In a dynamicly typed language such as Ruby or Javascript, variables can hold data of any type over their lifetime. For example, in Javascript:

> var dyanmicVar;
undefined
> dynamicVar = "a string";
'a string'
> typeof dynamicVar;
'string'
> dynamicVar = 2;
2
> typeof dynamicVar;
'number'

Now lets try the same thing in C

NSString *staticVar = @"a string";
staticVar = 2;

This should generate a compilre error Implicit conversion of 'int' to 'NSString *' is disallowed with ARC. The first major difference here is that there is a program called the compiler that is attempting to enforce type correctness. While this can be irritating at first, static typing can also help catch errors in your programs. For example, in the Javascript program above, what if some part of the program was expecing dynamicVar to be a string. You wouldn't know there was an issue until actually executing the program. And then, you would have to back up to the location where dynamicVar was assigned a non-string.

Dynamic vs. static typing is one of those issues in programming language design that people feel strongly about. Ultimatley we don't have a choice since Objective-C was chosen for us, so lets learn to take advantage of the warnings and errors given by the compiler.

C Built-In Types

short, int, long, NSInteger
Represent integers. short holds smaller numbers than int, which is smaller than long. NSInteger is a type provided by Apple that is an integer of the ideal size for the current platform, be it iOS or OS X.
float, double, CGFloat
Decimal numbers, double is capable of storing higher precision numbers than float, but uses up double the storage space. CGFloat is the decimal type used by the iOS Core Graphics framework.
Pointer
Pointers are variables that "point" to other variables. In Objective-C, we never deal with objects directly, but instead pass around pointers to them. This is why when we declare an object, the variable that we store is a pointer to the object.
NSDate *today = [NSDate date];
Technically we say that "today is a pointer to an NSDate," however since objects are always stored as pointers, normally you will just say that "today is an NSDate."
struct
Structures are composite types. They allow us to group together data into a more complicated concept. For example I want a type Location that knows the name, latitude, and longitude of a location and the number of times I had visited that place.
typedef struct {
  float latitude;
  float longitude;
  int visits;
} Location;
    
Elements of a struct are accessed using dot notation.
Location theFactory;
theFactory.latitude = 42.963107; 
theFactory.longitude = -85.669407;
theFactory.visits = 42;
Structs are used frequently in iOS development. Try looking up NSRect in the XCode documentation. NSRects are used to represent the location of a user interface element
BOOL
Boolean values, takes two values, YES or NO.

That's all there is. With nothing but numbers, words, and collections of the two, we can describe most any concept you want.

Declaring Variables

Before we can use a variable to store data, it must be delcared with both a name and a type.

int anInteger;
float aDecimal;
NSString *aString; // "string" is programmer talk for text.

Unlike int and float which are built-in C types, NSString is actually a class specific to Objective-C. The variable aString will hold a pointer to an instance of NSString. If you are unfamiliar with words like class, object, and instance, don't worry too much now, we will cover them in detail in the next section. For now, just know that if you need a variable to hold some text, you delcare it of type NSString *.

Assigning Variables

Now that we have some variables, we should store data in them. A single equal sign = is used to store the value on the right side into the variable on the left side.

int anInteger;
float aDecimal;
NSString *aString; // "string" is programmer talk for text.
anInteger = 5;
aDecimal = 2.7182;
aString = @“The Deliverator belongs to an elite order, a hallowed subcategory.”;

This example both declares several variables, and then assigns values to them.

Combining Declaration and Assignment

It is possible to combine both the declaration and assignment of a variable into the same line.

int anInteger = 5;
float aDecimal = 2.7182;
NSString *aString = @“The Deliverator belongs to an elite order, a hallowed subcategory.”;

Exercises

  1. Declare and assign a variable of types int, float, and NSString*. Confirm that your program will run. While we can't see any output yet, if it runs you are probably doing it right.
  2. Create variables of different types and try assigning them to one another. What kind of compiler messages do you get?