-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_example.c
More file actions
42 lines (35 loc) · 981 Bytes
/
static_example.c
File metadata and controls
42 lines (35 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* Functions that illustrate the use of the static storage class specifier.*/
#include <stdio.h>
// Function prototypes
static void count_stays_zero();
void count_increases();
void print_num(int to_print);
// Here `static` limits the use of this variable to only this file.
static int num = 0;
void main()
{
for (int i = 0; i < 3; i++)
{
count_stays_zero();
count_increases();
}
print_num(num);
}
// When in front of a function, `static` prevents use of the function outside this file.
static void count_stays_zero()
{
int zero_count = 0;
printf("The zero count is: %d \n", zero_count);
zero_count += 1;
num += 3;
}
void count_increases()
{
static int increasing_count = 0; // `static` prevents var from going out of scope when function is completed.
printf("The increasing count is: %d \n", increasing_count);
increasing_count += 1;
}
void print_num(int to_print)
{
printf("The number is: %d\n", to_print);
}