I'm new to C, just a question on global variable, below is the code that uses the header file as recommended:
//test.h
extern int globalVariable;
//test.c
#include "test.h"
int globalVariable = 2020;
//main.c
#include <stdio.h>
#include "test.h"
int main()
{
printf("Value is %d", globalVariable);
}
so it works well and the print output is 2020.
But if I change the main.c as:
#include <stdio.h>
#include "test.h"
int globalVariable;
int main()
{
printf("Value is %d", globalVariable);
}
it still compile and the output is still 2020. Below is my question:
Q1-When I add int globalVariable; in main.c, isn't that it re-initialize the globalVariable to 0 becuase int globalVariable; is the same as int globalVariable = 0;. So why the output is still 2020 rather than 0?
Q2-globalVariable is already defined in test.c, and I re-define(reinitialize) it to 0, should it be a compile error since C doesn't allow multiple definition, so why the program still compile?