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

Chapter 21: Contending with Variables in Functions 271

Global variables are declared outside of any function. It’s typically done right before the main() function.

Everything you know about creating a variable, other than being declared outside a function, applies to creating global variables: You must specify the type of variable (int, char, and float, for example), the variable’s name, and the semicolon.

You can also declare a group of global variables at one time:

int score,tanks,ammo;

And, you can preassign values to global variables, if you want:

char prompt[]=”What?”;

An example of a global variable in a real, live program

For your pleasure, please refer again to the BOMBER.C source code. This final modification adds code that keeps a running total of the number of people you kill with the bombs. That total is kept in the global variable deaths, defined right up front. Here’s the final source code, with specific changes noted just afterward:

#include <stdio.h>

 

#define COUNT 20000000

/* 20,000,000 */

void dropBomb(void);

/* prototype */

void delay(void);

 

int deaths;

/* global variable */

int main()

 

{

 

char x;

 

deaths=0;

for(;;)

{

printf(“Press ~ then Enter to quit\n”); printf(“Press Enter to drop the bomb:”); x=getchar();

fflush(stdin); /* clear input buffer */ if(x==’~’)

{

break;

}

272 Part IV: C Level

dropBomb();

printf(“%d people killed!\n”,deaths);

}

return(0);

}

 

void dropBomb()

 

{

 

int x;

 

for(x=20;x>1;x--)

 

{

 

puts(“

*”);

delay();

 

}

puts(“ BOOM!”); deaths+=1500;

}

void delay()

{

long int x;

for(x=0;x<COUNT;x++)

;

}

Bring up the source code for BOMBER.C in your editor. Make the necessary changes so that your code matches what’s shown here. Here are the details:

First, the global variable deaths is declared right before the main() function:

int deaths;

/* global variable */

Second, the main() function is rewritten to contain an endless loop (refer to the preceding code block). The loop allows you to drop bombs over and over; pressing the ~ (tilde) key ends the loop.

Third, the global variable deaths is incremented inside the dropBomb() func­ tion by a simple addition to that function:

deaths+=1500;

Double-check your source code! Then, save BOMBER.C to disk one more time (though you don’t quite finish it until you read Chapter 22).

Compile and run the program.

Chapter 21: Contending with Variables in Functions 273

The program effectively keeps a running tally of the dead by using the global deaths variable. The variable is manipulated (incremented) in the dropBomb() function and is displayed in the main() function. Both functions share that global variable.

See how the global variable is declared right up front? The #includes usually come first, and then the prototyping stuff, and then the global variables.

No, this game doesn’t challenge you.

For more information about the endless for loop, see Chapter 25.

The fflush() function is used to clear extra characters that getchar() might read. Refer to Chapter 13 for the details.

In Unix-like operating systems, be sure to use fpurge(stdin) in Line 20 rather than fflush(stdin). Refer to Chapter 13.

274 Part IV: C Level