I have a bit confusion about the variable definition and declaration using "extern" keyword. Assuming I want a variable 'timer' can be used in multiple c files. Then I can:
On c1.h
int timer;
Then on c1.c
#include "c1.h"
void timer_increase() {
timer++
}
Then on c2.c
#include "c1.h"
void print_timer() {
printf("%d", timer);
}
However, when I using extern variable:
On c1.h
extern int timer;
Then on c1.c
#include "c1.h"
int timer;
void timer_increase() {
timer++
}
Then on c2.c
#include "c1.h"
void print_timer() {
printf("%d", timer);
}
Both scripts work fine and I cannot see any reason that I have to used extern to declare a variable. Can anyone gives me any hints?