top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the order of initialization of global/static variables in C/C++?

+3 votes
764 views
What is the order of initialization of global/static variables in C/C++?
posted Apr 30, 2014 by Jagan Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
One option is the init_priority attribute documented at
http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html

1 Answer

+1 vote

AFAIK C/C++ language does not define the order in which global/static variables (in different files) are initialized. However there may be an impact because of the compiler behavior.

Comments are welcome?

answer Apr 30, 2014 by Kiran
Agree with Kiran.
Similar Questions
0 votes

Which is the best practice for variable declaration within the function ? Or is it vary from one language to other one ?

0 votes

I have a recursive function as below,

void func()
{
       if(counter>10)
              return;
       func();
}

Here, I need to come out of the function when counter reaches specific number(10 as per example).
Now, the condition is, I can't take this counter as global or static or can't pass this counter as parameter.

Any suggestion to solve this given above terms.

...