You need to declare a global variable:
extern int SIZE_A=10; For some reason it displays a warning:
warning: 'SIZE_A' initialized and declared 'extern'
You need to declare a global variable:
extern int SIZE_A=10; For some reason it displays a warning:
warning: 'SIZE_A' initialized and declared 'extern'
This variable is not constant at all :), but this is not the problem.
Just a normal practice is this: in one file, a global variable is declared and defined, such as
int SIZE_A = 10; and in others (for example, using the mechanism of included header files), it is only declared that somewhere in another file (ie, extern ) there is such a variable
extern int SIZE_A; Otherwise, you risk specifying different values of this variable in different files - which is what they warn you about. In this case, options are possible, depending on the compiler, when extern with a value will create a variable in the object file — then there will be a collision during linking — two variables with the same name; which one is considered correct? or when extern will lead only to a reference to a variable in another file - but then the value in the extern declaration will be completely ignored - it simply will not be written anywhere :)
extern declaration. Which of them is more correct - let the constitutional court decide :) - HarryIn c, the declaration of the object identifier in the file's scope, which contains the initializer, is an external definition of the identifier.
If there is no initializer, and the storage class specifier is static or missing, then this is so-called. tentative definition . Within a translation unit there may be several preliminary definitions for a single identifier.
I will give an example from the Standard C11 (6.9.2 / 4):
int i1 = 1; //определение, внешняя компоновка static int i2 = 2; //определение, внутренняя компоновка extern int i3 = 3; //определение, внешняя компоновка int i4; //предварительное определение, внешняя компоновка static int i5; //предварительное определение, внутренняя компоновка int i1; //предварительное определение, ссылается на предыдущий int i2; //ошибка, не совпадает компоновка int i3; //предварительное определение, ссылается на предыдущий int i4; //предварительное определение, ссылается на предыдущий int i5; //ошибка, не совпадает компоновка extern int i1; //ссылается на предыдущий с внешней компоновкой extern int i2; //ссылается на предыдущий с внутренней компоновкой extern int i3; //ссылается на предыдущий с внешней компоновкой extern int i4; //ссылается на предыдущий с внешней компоновкой extern int i5; //ссылается на предыдущий с внутренней компоновкой Those. In your case (variable available in different translation units) the presence of an explicit extern optional. A warning is displayed so that there is no false feeling (due to the presence of extern ) that this record is just an announcement. Although in fact this definition (due to the presence of an initializer).
To consolidate the fact of constancy, one should additionally add const . As a result, we get:
const int SIZE_A = 10; But after all in the message it is written, black in English: the initialization of the EXTERNAL variable. If you yourself have declared that this variable is NOT yours (that is, it is declared elsewhere and by someone else), then what is your right to initialize it?! Can you initialize someone else's wife? :-)
Source: https://ru.stackoverflow.com/questions/602538/
All Articles