2

The following snippet of code is completely valid in C++ (at least gets compiled):

my_file.cxx:

static const int MY_CONST_ONE = 1; static const int MY_CONST_TWO = MY_CONST_ONE; 

On the other hand, compilation of exactly the same code in C fails with the error message (http://ideone.com/erBkm9):

my_file.c:2:1: error: initializer element is not constant 

my_file.c:

static const int MY_CONST_ONE = 1; static const int MY_CONST_TWO = MY_CONST_ONE; 

What is the reason? Is it something compiler-specific or some known C vs C++ difference?

3
  • Your compiler already did the work. Post it's result, so we don't have to do the work again. Commented Mar 13, 2013 at 16:04
  • 1
    @AlokSave, this question is a lot simpler and easier to understand than the one you linked. I'd recommend keeping it. Commented Mar 13, 2013 at 16:07
  • @MarkRansom: But it is a duplicate. The marked duplicate answers exactly what is being asked here and more. What does simpler and easier to understand have to do with it? If at all you can go ahead do the reverse marking of duplicates, but I don't think it would do any justice to the content in the other Q. Commented Mar 13, 2013 at 16:09

1 Answer 1

7

Basically, const variables in C are not considered compile-time constants. Places where compile-time constants are needed can thus not get their values from const variables.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It makes perfect sense considering the compilation error that I get.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.