Online Book Reader

Home Category

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

By Root 462 0
you’ve seen so far yield a new result. So, for example, to increase x by 1, you would use the + operator and then assign the result back into x:

int x = 5;

x = x + 1; // x is now 6

C programmers do these sorts of operations so often that operators were created that change the value of the variable without an assignment. For example, you can increase the value held in x by 1 with the increment operator (++):

int x = 5;

x++; // x is now 6

There is also a decrement operator (--) that decreases the value by 1:

int x = 5;

x--; // x is now 4

What if you want to increase x by 5 instead of just 1? You could use addition and assignment:

int x = 5;

x = x + 5; // x is 10

But there is a shorthand for this, too:

int x = 5;

x += 5; // x is 10

You can think of the second line as “assign x the value of x + 5.” In addition to +=, there is also -=, *=, /=, and %=.

To get the absolute value of an int, you use a function instead of an operator. The function is abs(). If you want the absolute value of a long, use labs(). Both functions are declared in stdlib.h:

#include

#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);

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

printf("The absolute value of -5 is %d\n", abs(-5));

return 0;

}

Floating-point numbers


If you need a number with a decimal point, like 3.2, you use a floating-point number. Most programmers think of a floating-point number as a mantissa multiplied by 10 to an integer exponent. For example, 345.32 is thought of as 3.4532 x 102. And this is essentially how they are stored: a 32-bit floating number has 8 bits dedicated to holding the exponent (a signed integer) and 23 bits dedicated to holding the mantissa with the remaining 1 bit used to hold the sign.

Like integers, floating-point numbers come in several sizes. Unlike integers, floating-point numbers are always signed:

float g; // 32-bits

double h; // 64-bits

long double i; // 128-bits

Tokens for displaying floating-point numbers

printf() can also display floating point numbers, most commonly using the tokens %f and %e. In main.c, replace the integer-related code:

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

{

double y = 12345.6789;

printf("y is %f\n", y);

printf("y is %e\n", y);

return 0;

}

When you build and run it, you should see:

y is 12345.678900

y is 1.234568e+04

So %f uses normal decimal notation, and %e uses scientific notation.

Notice that %f is currently showing 6 digits after the decimal point. This is often a bit much. Limit it to two digits by modifying the token:

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

{

double y = 12345.6789;

printf("y is %.2f\n", y);

printf("y is %.2e\n", y);

return 0;

}

When you run it, you should see:

y is 12345.68

y is 1.23e+04

Functions for floating-point numbers

The operators +, -, *, and / do exactly what you would expect. If you will be doing a lot of math, you will need the math library. To see what’s in the math library, open the Terminal application on your Mac and type man math. You will get a great summary of everything in the math library: trigonometry, rounding, exponentiation, square and cube root, etc.

If you use any of these math functions in your code, be sure to include the math library header at the top that file:

#include

One warning: all of the trig-related functions are done in radians, not degrees!

Challenge


Use the math library! Add code to main.c that displays the sine of 1 radian. Show the number rounded to three decimal points.

7

Loops


In Xcode, create yet another new project: a C Command Line Tool named Coolness.

The first program I ever wrote printed the words, “Aaron is Cool”. (I was 10 at the time.) Write that program now:

#include

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

{

printf("Aaron is Cool\n");

return 0;

}

Build and run the program.

Let’s suppose for a moment that

Return Main Page Previous Page Next Page

®Online Book Reader