The static storage, not added or requested dynamically

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Read input and write output

To show: The static storage, not added or requested dynamically

 

 

/* static storage program example */

#include <stdio.h>

#define MAXNUM 3

 

void sum_up(void);

 

int main(void)

{

int count;

 

printf("\n*****static storage*****\n");

printf("Key in 3 numbers to be summed ");

for(count = 0; count < MAXNUM; count++)

sum_up();

printf("\n*****COMPLETED*****\n");

return 0;

}

 

void sum_up(void)

{

/* At compile time, sum is initialized to 0 */

static int sum = 0;

int num;

 

printf("\nEnter a number: ");

/* scanf("%d", &num); */

scanf_s("%d", &num, 1);

sum += num;

printf("\nThe current total is: %d\n", sum);

}

 

Output example:

 

*****static storage*****

Key in 3 numbers to be summed

Enter a number: 23

The current total is: 23

Enter a number: 45

The current total is: 68

Enter a number: 234

The current total is: 302

*****COMPLETED*****

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index