2

I am getting ERROR when I am running this program at the line

static int b = a; //error : initializer element is not constant

Can not understand why?

 #include <stdio.h>
   // #include <setjmp.h>
    int main()
    {
    int a = 5;
    static int b = a;

    return 0;
    }
3
  • 6
    The language definition says that initialisers for static variables have to be constant expressions. Commented Jun 22, 2012 at 11:04
  • 2
    @EdHeal: // comments have been standard C for the past 13 years, and they were widely supported before then. Even MSVC supports them. Commented Jun 22, 2012 at 11:15
  • @DietrichEpp - Shows you the last time I programed in C! Commented Jun 22, 2012 at 11:23

4 Answers 4

3

Apart from the other reasons stated in other answers here, please see the below statement in the Standard.

The C Standard says this in Point-4 (Section 6.7.8 Initialization):

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

Additionally, as to what is a constant expression, it says in Section 6.6 Constant Expressions as below:

A constant expression can be evaluated during translation rather than runtime, and
accordingly may be used in any place that a constant may be.
Sign up to request clarification or add additional context in comments.

Comments

2

In C (unlike C++), the initializer for any object with static storage duration - including function statics - must be constant expressions. In your example a is not a constant expression so the initialization is not valid.

C99 6.7.8 / 4:

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

Comments

1

Static variable is always global in the sense it is not on any thread's stack, and it is not important if its declaration is inside a function or not.

So the initialization of the global variable b is performed during program start-up, before any function (including main) gets called, i.e. no a exists at that time, because a is local variable which gets its memory place on stack after the function (here main) is called.

Hence you really cannot expect the compiler to accept it.

2 Comments

Static variable is always global??Are you sure?
It is in the sense there is always only one instance of it, even if you call the function where it is defined million times concurrently. The all function calls share it. It is not in the sense that (when defined in a function), it can not be used from elsewhere (unless the function provides pointer to it.)
0

Following from Als's answer ...

// This is really crappy code but demonstrates the problem another way ....
#include <stdio.h>
int main(int argc, char *argv[])
{
static int b = argc ; // how can the compiler know 
                      // what to assign at compile time?

return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.