6

When compiling this code sample

#include <stdio.h>
#include <stdlib.h>

int myfunc()
{
    printf("Constructor\n");
    return 1;
}

static const int dummy = myfunc();

int main()
{
    printf("main\n");
    return 0;
}

it works when compiled as C++, but not as C using the same compiler (MingW gcc). I get an initializer element is not constant in C mode.

So apparently there are differences regarding static intialization. Is there a reason why this is apparently allowed for C++ but not for C? Is this because otherwise you wouldn't be able to have global objects with constructor functions?

Nick
  • 1,417
  • 1
  • 14
  • 21
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • 2
    C ought to be as simple as possible. Non-static initialization requires the presence of startup/constructor routines that fire before `main()` is called. Apparently, the C++ standards committee could put up with that requirement, but the C guys didn't like it (and I can understand that). –  Jan 11 '14 at 10:35
  • SO should be limited to technical questions, not opinion based speculation why this or that feature exists in a language and not another. Voting to close: *Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.* – Jens Gustedt Jan 11 '14 at 10:38
  • 2
    @JensGustedt, why would this be opinion based? I don't thin kthat the standard is changed based on preferences. I even gave an example why I would think it is neccessary to allow this in C++, so I would expect that the answer is a technical reason. – Devolus Jan 11 '14 at 10:40
  • 2
    please refer to this post http://stackoverflow.com/questions/5921920/difference-between-initialization-of-static-variables-in-c-and-c – Sajad Karuthedath Jan 11 '14 at 10:47

1 Answers1

2

C++ compiler generates an additional 'Start' function, where all "global function calls" are executed before the PC (program counter) is set to the address of 'main'.

A "global function call" is any function-call which is performed in order to initialize a global object (including implicit function calls, i.e., constructors).

C compiler does not generate such 'Start' function, and the PC is set to 'main' as soon as the OS loads the executable and runs the process.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • Do you have any reference to back this up? – Shoe Jan 11 '14 at 10:57
  • I can say from my personal experience that a bug in a C++ static ctor will blow up my system before main() gets called:) I have debugged my way through the crt that calls the static ctors, and it surely happens before main. – Martin James Jan 11 '14 at 11:17
  • 1
    Fun fact: C programs also never start from `main` (`main` returns a return code, but something still must call `exit` to signal that code to the operating system). – Griwes Jan 11 '14 at 11:28
  • This answer is clearly (at least partially) wrong. Main is not the process entry point, because there us a CRT startup involved, which will prepare the runtime environment before main is called. `main` is, from the compilers point of view, just another function. The CRT startup is, among other things, responsible for initializing static variables. – Devolus Jan 11 '14 at 11:49