Short Answer
#define internal static will replace every word called
internal back to
static, and this happens
before the compiler starts running. It has nothing to do with defining types or anything.
The typical use for #define is to use it for an unchanging value:
my_game.c
| // Headers...
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
int main()
{
// Use SCREEN_WIDTH and SCREEN_HEIGHT as many times as you need to
}
|
Instead of directly using the values 800 and 600 all the time. It saves typing if you want the width and height to be different in the future.
Long Answer
Consider this file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | #include <stdio.h>
int scores[5];
static void print_high_scores()
{
for (int i = 0; i < 5; ++i)
{
printf("Score #%d: %d\n", i+1, scores[i]);
}
}
int main()
{
// Manually populate high scores
scores[0] = 100;
scores[1] = 80;
scores[2] = 70;
scores[3] = 50;
scores[4] = 10;
print_high_scores();
return 0;
}
|
Notice how
scores is in global scope. If I have another file in my project, it can access it by typing in:
extern int scores[5];
However, if you only want
scores to be accessible in the original file (so only
main() and
print_high_scores() use it, for example), then you can add the
static keyword:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | #include <stdio.h>
static int scores[5]; // Now accessible in this file only
static void print_high_scores()
{
for (int i = 0; i < 5; ++i)
{
printf("Score #%d: %d\n", i+1, scores[i]);
}
}
int main()
{
// Manually populate high scores
scores[0] = 100;
scores[1] = 80;
scores[2] = 70;
scores[3] = 50;
scores[4] = 10;
print_high_scores();
return 0;
}
|
Notice how
print_high_scores() was already static, meaning it is also something restricted to file scope (i.e. no other file will know about it). Now,
static can also be used in another context, and to avoid confusion, we can do the following:
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 | #include <stdio.h>
#define internal static
internal int scores[5];
internal void print_high_scores()
{
for (int i = 0; i < 5; ++i)
{
printf("Score #%d: %d\n", i+1, scores[i]);
}
}
int main()
{
// Manually populate high scores
scores[0] = 100;
scores[1] = 80;
scores[2] = 70;
scores[3] = 50;
scores[4] = 10;
print_high_scores();
return 0;
}
|
"internal" makes it more clear that
scores and
print_high_scores() are
internal to the file.
But in the end,
#define is an instruction for the preprocessor (a program that runs
before compiling your code) to replace all instances of the text "internal" back to "static". Therefore, the compiler will be seeing static all over again in the code, not internal. We only did this for clarity in reading the code.
Closing Statement
Now again, the preprocessor runs from the start every time it compiles a new file (or, more technically, a compilation unit)... so you need to write:
#define internal static
for every compilation unit that wants the word internal to be replaced back to static. See my previous post for more info.