Sunday, April 26, 2020

Using static or extern as global variable in C++

Good practice using extern variable:
Declare the extern variable in an .h file
extern int outputMonth;

Define and initialize it outside of a function (like before main function)
int outputMonth = 50;

Static variable is internal linkage (extern use external linkage) where each cpp file has its own copy of value. So it is not recommended to use it as a global variable.

No comments:

Post a Comment