Why is it necessary to initialize a global variable with the return value of a function that does not work when declared, but works fine in the file area?

An 80k reputable contributor R .. told me that we cannot initialize global variables with the return value of the function, since this is not considered a constant, and global variables must be initialized by a constant. And true to my words, I get the following error for this program as expected - initializer element is not a constant . Here is the program:

 #include<stdio.h> int foo(); int gvar=foo(); //ERROR int main() { printf("%d",gvar); } int foo() { return 8; } 

But in this context, I just do not understand why the next modified version of the above program does not show errors at all and works fine. In this second program, I initialize the same global variable with return value the same function foo() . Can you tell me what is the strict technical reason for this change in results? Why does it initialize a global variable with the return value of the function in the declaration causing the error, but does the same initialization with the same return value work fine when executed inside the function?

 #include<stdio.h> int foo(); int gvar; int main() { gvar=foo(); printf("%d",gvar); } int foo() { return 8; } 

Output 8

+4
source share
5 answers

The reason for this is that in order to determine the value created by the function, you need to execute the code and that in C the code is not executed when initializing static and global variables.

The compiler and linker work together to produce a byte image of the global memory segment: the compiler provides the values, and the linker performs their final layout. At run time, the segment image is loaded into memory, as is, without further changes. This happens before any code is executed, so function calls cannot be executed.

Note that this does not mean that this is not possible for some technical reason, only the C developers decided not to. For example, the C ++ compiler generates a code segment that calls global object constructors, which are executed before the control is passed to main ().

+9
source

The second version does not have an initializer for gvar . gvar declared and defined in the global scope without an initializer. It has a static storage duration, so it is initialized to zero.

The purpose of main is this: purpose, not initialization.

+2
source

In case 1, a global variable is assigned to a variable, but declared.

But in the second case, a global variable is assigned ( which has already been declared) with the return value foo ().

The formation of the data section, the text section occurs at compile time.

Global variables will be in the data section (the bss or initialized data section), so is foo () not being called correctly at compile time? and the return value of foo () at compile time is unknown.

But the second case, when the text section is executed, gvar assigned the return value of foo (). It's really.

+1
source

You might think of it this way: when you run main() all global variables should already have their initializer values. And they cannot, as you were told, get them by calling functions, since main() actually starts execution in the C program.

0
source

we could not call any function from an external function. Not like a shell script. The function only allows calling from inside the body of the function.

At the beginning of the first start, it starts with main() , the compiler does not know the function that calls, if it is on the outside of the function, it can be accepted as a prototype if arguments and return types are provided.

we can put the return value of the function by calling from the main or other function block, in a variable, a function, then called (this global) changed.

but we can use the macro in a global variable as needed. as:

 #define max() 12 int glob=max(); main() { } 
0
source

Source: https://habr.com/ru/post/1478939/


All Articles