Why's initializing a global variable with return value of a function failing at declaration,but works fine at file scope?

后端 未结 5 2118
名媛妹妹
名媛妹妹 2020-12-19 04:51

An 80k reputation contributor R.. told me on SO that we can\'t initialize global variables with the return value of a function as that\'s not c

相关标签:
5条回答
  • 2020-12-19 05:13

    The reason behind it is that in order to determine a value produced by a function one needs to execute code, and that there is no code execution done in C when initializing static and global variables.

    Compiler and linker work together to prepare a byte image of the global memory segment: the compiler provides the values, and the linker performs their final layout. At runtime, the image of the segment is loaded in memory as is, without further modifications. This happens before any code gets executed, so no function calls can be made.

    Note that this does not mean that it is not possible for some technical reason, only that C designers decided against doing it. For example, C++ compiler generates a code segment that calls constructors of global objects, which gets executed before the control is passed to main().

    0 讨论(0)
  • 2020-12-19 05:16

    You can maybe think of it like this: when main() starts, all global variables must already have their initializer values. And they can't, as you've been told, get those by calling functions since main() is really where execution starts, in a C program.

    0 讨论(0)
  • 2020-12-19 05:22

    The second version doesn't have an initializer for gvar. gvar is declared and defined at global scope without an initializer. It has static storage duration, so it is initialized with zero.

    The assignment in main is just that: an assignment, not an initialization.

    0 讨论(0)
  • 2020-12-19 05:22

    we could not call any function from outer of the function.Not like shell script.function only allow to called from inside of function body.

    In c first execution begins from main(), compiler don't know the function calling if that stands on outer of function it may taken as prototype if arg and return types provided.

    we can putting return value of function by calling from main or others function block, to the variable,the function called then (that global) variable modified.

    but we can use macro in global variable as needs. as:

    #define max() 12 
    int glob=max();
    main()
    {
    
    }
    
    0 讨论(0)
  • 2020-12-19 05:32

    In case 1, global variable is assigned with a variable while it is declared.

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

    Forming of data section, text section all happens during compilation.

    Global variables will be in data section(bss or initialized data section), so at compile time, foo() is not invoked right? and return value of foo() is not known during compilation.

    But second case, when the text section get executed, gvar is assigned with return value of foo(). It is valid.

    0 讨论(0)
提交回复
热议问题