Hello I'm viewing an example of external linkage of function and variable in C.
This is an example that produces random variables.
- random.c
#define SEED 20
#define MULT 3124
#define INC 2345
#define MOD 5436
unsigned int call_count = 0;
static unsigned seed = SEED;
unsigned random_i(void) {
seed = (MULT * seed + INC) % MOD;
call_count++;
return seed;
}
double random_f(void) {
seed = (MULT * seed + INC) % MOD;
call_count++;
return seed / (double)MOD;
}
- and this is main.c
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable:4996)
unsigned random_i(void); // I wonder why this prototype has no "extern" specifier
double random_f(void); // I wonder why this prototype has no "extern" specifier
extern unsigned call_count;
int main(void) {
register int i;
for (i = 0; i < 10; i++)
printf("%d ", random_i());
printf("\n");
for (i = 0; i < 10; i++)
printf("%lf ", random_f());
printf("\nCall count : %d\n", call_count);
return 0;
}
in this short program, I wonder why those of two function prototype has no "extern" specifier AND why this code is compiled without an error.
Because what I know is that when I use variables or functions that is in other source code, I have to do it with extern
specifier for example extern int a=10;
.
Please let me know.
Thanks.