20

I was going through the code at http://geeksforgeeks.org/?p=10302

#include<stdio.h>
int initializer(void)
{
    return 50;
}
 
int main()
{
    static int i = initializer();
    printf(" value of i = %d", i);
    getchar();
    return 0;
}

This code will not compile in C because static variables need to be initialised before main() starts. That is fine. But this code will compile just fine in a C++ compiler.

My question is why it compiles in a C++ compiler when static has the same usage in both languages. Of course compilers will be different for these languages but I am not able to pin point the exact reason. If it is specified in the standard, I would love to know that.

I searched for this question on SO , found these similar questions:

Braiam
  • 1
  • 11
  • 47
  • 78
Anon
  • 2,608
  • 6
  • 26
  • 38

3 Answers3

10

It compiles in C++ because C++ needs to support dynamic initialization anyway, or you couldn't have local static or non-local objects with non-trivial constructors.

So since C++ has this complexity anyway, supporting that initialization like you show isn't complicated to add anymore.

In C that would be a big matter because C doesn't have any other reason to support initialization done at program startup (apart from trivial zero initialization). In C, initial values of file-scope or local static objects can always statically be put into the executable image.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Thanks. I am looking something similar to this. So when we have --- static char *p = "lol" ; lol is in the read-only area of the data segment while p is in the read-write area. Now, when there is dynamic initilisation, how does the compiler know the value at run time ? I guess this might warrant another question on its own so just the gist would do . – Anon May 07 '11 at 15:39
  • @Anon I don't understand your question. What value should it know? – Johannes Schaub - litb May 07 '11 at 15:44
  • My bad. I was not clear enough. Basically I wanted to know what does compiler store as the value of static variables when they are dynamically initialised. After a bit of Googling, I was able to find the answer. Thanks for pointing me in the right direction and in a timely manner. (For those who want to know the answer - Compiler will give the default value first and then the value will be replaced by the actual value at run time. So this is one case where the value of the static variable won't be in the read-only part of data segment) – Anon May 07 '11 at 16:01
  • @Anon the value of it will be a null pointer in between program startup and the start of its dynamic initialiation. see http://stackoverflow.com/questions/3309042/what-does-main-return – Johannes Schaub - litb May 07 '11 at 16:04
  • @Anon C++ has nothing to say about segments, read-only or otherwise. That's a (not very interesting) detail of your specific implementation. –  May 07 '11 at 16:09
8

6.7.8/4 [C99]

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

In static int i = initializer(); the RHS is not a constant expression and so the code doesn't compile in C.

In C++ there is no such restriction and the code is well-formed in C++.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
3

Static variables in C need to be initialised with a value known at compile time. This requirement has been removed in C++, and you can initialise them with expressions evaluated at run-time.

The two languages differ in this, and many, many other respects. You can quite easily write C code which will be acceptable to a C++ compiler, but the reverse is not true.