Online Book Reader

Home Category

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

By Root 453 0
Chapter 8.

Apple has created two integer types that are 32-bit on 32-bit platforms and 64-bit on 64-bit platforms:

NSInteger g;

NSUInteger h;

In much of Apple’s code, you will see these types used. They are, for all intents and purposes, the same as long and unsigned long.

Tokens for displaying integers

Create a new project: a C Command Line Tool called Numbers. In main.c, create an integer and print it out in base-10 (as a decimal number) using printf():

#include

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

{

int x = 255;

printf("x is %d.\n", x);

return 0;

}

You should see something like

x is 255.

As we’ve seen, %d prints an integer as a decimal number. What other tokens work? You can print the integer in base-8 (octal) or base-16 (hexadecimal). Add a couple of lines to the program:

#include

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

{

int x = 255;

printf("x is %d.\n", x);

printf("In octal, x is %o.\n", x);

printf("In hexadecimal, x is %x.\n", x);

return 0;

}

When you run it, you should see something like:

x is 255.

In octal, x is 377.

In hexadecimal, x is ff.

(We’ll return to hexadecimal numbers in Chapter 33.)

What if the integer has lots of bits? You slip an l (for long) or an ll (for long long) between the % and the format character. Change your program to use a long instead of an int:

#include

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

{

long x = 255;

printf("x is %ld.\n", x);

printf("In octal, x is %lo.\n", x);

printf("In hexadecimal, x is %lx.\n", x);

return 0;

}

If you are printing an unsigned decimal number, you should use %u:

#include

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

{

unsigned long x = 255;

printf("x is %lu.\n", x);

// Octal and hex already assumed the number was unsigned

printf("In octal, x is %lo.\n", x);

printf("In hexadecimal, x is %lx.\n", x);

return 0;

}

Integer operations

The arithmetic operators +, -, and * work as you would expect. They also have the precedence rules that you would expect: * is evaluated before + or -. In main.c, replace the previous code with a calculation:

#include

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

{

printf("3 * 3 + 5 * 2 = %d\n", 3 * 3 + 5 * 2);

return 0;

}

You should see

3 * 3 + 5 * 2 = 19


Integer division

Most beginning C programmers are surprised by how integer division works. Try it:

#include

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

{

printf("3 * 3 + 5 * 2 = %d\n", 3 * 3 + 5 * 2);

printf("11 / 3 = %d\n", 11 / 3);

return 0;

}

You’ll get 11 / 3 = 3.666667, right? Nope. You get 11 / 3 is 3. When you divide one integer by another, you always get a third integer. The system rounds off toward zero. (So, -11 / 3 is -3)

This actually makes sense if you think “11 divided by 3 is 3 with a remainder of 2.” And it turns out that the remainder is often quite valuable. The modulus operator (%) is like /, but it returns the remainder instead of the quotient:

#include

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

{

printf("3 * 3 + 5 * 2 = %d\n", 3 * 3 + 5 * 2);

printf("11 / 3 = %d remainder of %d \n", 11 / 3, 11 % 3);

return 0;

}

What if you want to get 3.666667? You convert the int to a float using the cast operator. The cast operator is the type that you want placed in parentheses to the left of the variable you want converted. Cast your denominator as a float before you do the division:

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

{

printf("3 * 3 + 5 * 2 = %d\n", 3 * 3 + 5 * 2);

printf("11 / 3 = %d remainder of %d \n", 11 / 3, 11 % 3);

printf("11 / 3.0 = %f\n", 11 / (float)3);

return 0;

}

Now, floating point division will be done instead of integer division, and you’ll get 3.666667. Here’s the rule for integer vs. floating-point division: / is integer division only if both the numerator and denominator are integer types. If either is a floating-point number, floating-point division is done instead.


Operator shorthand

All the operators that

Return Main Page Previous Page Next Page

®Online Book Reader