Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

320 Part IV: C Level

The absolute value of a number is its value without the minus sign.

Even more complex functions than this one exist. Wise men truly run from them.

The best source for discovering what these commands do and how they

work is to look in the C language library reference that came with your compiler. If you’re lucky, it’s in a book; otherwise, it’s in your compiler’s online help feature.

Most of the time, you can replace the variables in a math function’s paren­ theses with constant values. For example:

x=sqrt(1024);

Something Really Odd to End Your Day

It’s your old pal ++ again, and his sister, -- — the increment and decrement operators from days gone by. They’re favorites, to be sure, but they’re kind of puzzling in a way. For example, consider the following snippet of code that you may find lurking in someone else’s program:

b=a++;

The variable b equals the contents of variable a plus 1. Or does it?

Remember that a++ can be a statement by itself. As such, doesn’t it work out first? If so, does b equal a+1, or does b equal a and then a is incremented after it all? And will Cody realize that Miranda is cheating on him so that he’ll be free to marry Jessica? Hmmm. . . .

It’s a puzzle that has an answer — an important answer that you have to know if you don’t want your programs getting all goofy on you.

The perils of using a++

Here’s the rule about using ++ to increment a variable so that you know what happens before running the sample program:

The variable is incremented last when ++ appears after it.

So:

b=a++;

Chapter 25: Math Madness! 321

The compiler instantly slides the value of variable a into variable b when it sees this statement. Then, the value of a is incremented. To drive this point home, test out the INCODD.C program.

Type this program into your editor. Save it to disk as INCODD.C:

#include <stdio.h>

int main()

{

int a,b;

a=10;

b=0;

printf(“A=%d and B=%d before incrementing.\n”,a,b); b=a++;

printf(“A=%d and B=%d after incrementing.\n”,a,b); return(0);

}

Compile and run! Here’s what the output looks like:

A=10 and B=0 before incrementing.

A=11 and B=10 after incrementing.

The first line makes sense: The a and b variables were given those values right up front. The second line proves the ++ conundrum. First, b is assigned the value of a, which is 10; then a is incremented to 11.

Whenever you see a variable followed by ++ in a C statement, know that the variable’s value is incremented last, after any other math or assign­ ments (equal-sign things) elsewhere on that line.

A good way to remember this rule is that the ++ comes after the variable. It’s as though C sees the variable first, raids its contents, and then — oh, by the way — it increments its value.

This rule screws you up if you ever combine the ++ with something else inside an if comparison:

if(a=b++)

This technique is common — sticking the b++ inside the comparison rather than on a line before or afterward. You have to keep in mind, however, that b is incremented after the if command compares them. If the comparison is true, b is still incremented. Remember that when you notice that your program is acting funny.

322 Part IV: C Level

Oh, and the same thing applies to a --

The decrementing operator, --, also affects a variable’s value after any other math or equal-sign stuff is done in the program. To witness this effect, here’s the DECODD.C program:

#include <stdio.h>

int main()

{

int a,b;

a=10;

b=0;

printf(“A=%d and B=%d before decrementing.\n”,a,b); b=a--;

printf(“A=%d and B=%d after decrementing.\n”,a,b); return(0);

}

Save the file to disk as DECODD.C. Compile and run. Here’s the output, prov­ ing that decrementing happens after any math or assignments:

A=10 and B=0 before decrementing.

A=9 and B=10 after decrementing.

The value of b is equal to 10, which means that the a variable was decre­ mented last.

All this makes sense, and you should understand it by now — or at least be aware of what’s happening. If you can’t remember the rule, just keep your incrementing or decrementing on a line by itself, as shown in this example:

a++;

b=a;

Reflections on the strange ++a phenomenon

Load the program INCODD.C back into your editor. Change Line 10 to read

b=++a;

This line looks stranger than it is. You can type it like this to avoid thinking that =++ is some weird C command:

b = ++a;

Chapter 25: Math Madness! 323

The ++ is still the incrementing operator. It still increases the value of the a variable. Because it comes before the variable, however, the incrementing happens first. It reads “Increase me first, and then do whatever else comes next.”

Save the source code for INCODD.C to disk. Compile and run it. Here’s what you see:

A=10 and B=0 before incrementing.

A=11 and B=11 after incrementing.

It worked! The ++a operation came first.

Again, this is just something strange you should know about. Rarely have I seen the ++ operator appear before a variable, but it can be done. It may help iron out some screwy things if you notice trouble when you’re putting ++ after a variable name.

You can use ++ before or after a variable when it appears on a line by itself. There’s no difference between this:

a++;

and this:

++a;

a++ is known as post-incrementing. ++a is known as pre-incrementing.

Yes, this process also works with decrementing. You can change Line 10 in DECODD.C to this:

b=--a;

Save, compile, and run the program. Variable a is decremented first, and then its new value is given to variable b.

Most people use the word anxious when they really mean eager.

Don’t even bother with this:

++a++;

Your logical mind may ponder that this statement first increments the a variable and then increments it again. Wrong! The compiler thinks that you forgot something when you try to compile this type of statement; you get one of the dreadful Lvalue required errors. That message means that the compiler was expecting another variable or number in there somewhere. Sorry, but the “fortress effect” of the incrementing or decre­ menting operators just doesn’t work.

324 Part IV: C Level