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' 

    3 answers 3

    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 :)

    • and if a value is assigned in two, but not in the third, what value will be in the third? - Grundy
    • @Grundy UB :) Only a normal compiler will simply not allow this to be compiled. For example, VC ++ categorically refused to link. - Harry
    • interesting, but in principle can be any value? :) - Grundy
    • @Grundy You know, I treat it like this ... There is a constitution, there are a lot of laws that theoretically should not contradict the constitution, but usually nobody looks at it, but there is a real prose of life ... And if you try to live by the constitution - very soon, such a life will become a torture :) The second phrase - “when I arrive in a country, I don’t ask what kind of laws are there. I ask if they are implemented”. Probably, I am wrong, but I try 1. avoid dark corners, and 2. know how my instrument behaves in certain cases. So what will be in your situation? I do not know. - Harry
    • @Grundy I know that VC ++ will not allow this to be compiled at all, but OWatcom will, but it will prevent (warning) the value in the extern declaration. Which of them is more correct - let the constitutional court decide :) - Harry

    In 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? :-)