I have a question about global variable in header file.
I learned that when using a preprocessor, the contents of the header are imported into the cpp file.
I think that when I write the code as below, the header file is called once and the global variable is declared once, so why does the LNK2005(multiple definition) error occur?
// main.c
#include "student.h"
int main() {
cout << a << endl;
return 0;
}
// student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <iostream>
using namespace std;
int a = 33;
#else
#endif
// student.c
#include "student.h"
Using 'extern' solves the problem, but errors do not occur even if i declare it as a 'const'.
const int a=33;
Does the 'const' also have a function of sharing global variables?
I'm curious about the principle of internal function