Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why compiler restricts global variable always to be initialized by constant value? [duplicate]

Tags:

c

Let me exemplify this,

int a = 100;
int b = a;

int main(int argc, char **argv, char ** env)
{
  printf("The value of b=%d\r\n",b);

  return 0;
}

Now, I get the compilation error as expected.

[joshis1@localhost global_var]$ gcc global_var.c -o global_var.out
global_var.c:4:1: error: initializer element is not constant
 int b = a;
 ^

What I want to learn here is why do I get the error? why compiler restricts this operation. I understand that initialized global variables are stored in Data segments. The compiler could have first resolved the value of a,and then could have assigned the same value to b. Why it lacks this feature? Is it complex for compiler to do? Is there any rationale behind this functionality or just a pitfall of C?

like image 840
dexterous Avatar asked Oct 02 '13 05:10

dexterous


People also ask

Are global variables initialized at compile time?

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code.

Do global variables need to be initialized?

In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.

What are two reasons why you should not use global variables?

Global variables can be altered by any part of the code, making it difficult to remember or reason about every possible use. A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code.

Why global variables are not stored in Stack?

Stack and heap are used to store variables during the execution of the program and it also get destroyed. Global data structures or global variables are not consumed by stack or heap. They basically allocated in a fixed memory block, which remains unchanged.


3 Answers

The official documentation, taken from line 1644, 6.7.8 Initialization, says:

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

Why the rule exists is a more difficult question - perhaps as you suggest it is difficult for the compiler to do. In C++ such an expression is valid, but global initialiser may invoke constructors, etc, whereas for C, to keep things compact, globals are evaluated at the compile phase. int b = a; is evaluable at compile time, but what about int b = a + c;? int b = pow(a, 2);? Where would you stop? C decides that not allowing you to start is the best solution.

like image 171
Ken Y-N Avatar answered Nov 06 '22 16:11

Ken Y-N


From your comment:

...how can I force compiler to make this work?

Well you can't make the compiler accept what you have but you can accomplish your goal by defining the value you want to assign to both variables.

#define INITIAL_VALUE_FOR_A 100

int a = INITIAL_VALUE_FOR_A;
int b = INITIAL_VALUE_FOR_A;

Now if you need to change the initial value, you only need to change it in one place;

like image 42
Jim Rhodes Avatar answered Nov 06 '22 16:11

Jim Rhodes


C is portable to very simple, small machines. Evaluating expressions that aren't constant requires runtime code, in a function. In embedded programming you might not want any functions (or code) that you did not explicitly program.

Your compiler probably will evaluate the initializer as a language extension, if configured with different options. If that fails, you could try C++ (even just the C-like subset) or another language that does more things you like :v) .

like image 1
Potatoswatter Avatar answered Nov 06 '22 14:11

Potatoswatter