I found out by accident that in C/C++ I can initialize a variable with itself:
#include <stdio.h>
static const int foo = foo;
int main() {
int bar = bar;
printf("%i, %i", foo, bar);
}
Depending on compiler version I get different results:
Those just print "0, 0" on my machine:
- g++.exe (Rev1, Built by MSYS2 project) 5.3.0
- g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
- gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
This one gives an error:
- gcc.exe (Rev1, Built by MSYS2 project) 5.3.0
foo.c:2:28: error: initializer element is not constant static const int foo = foo; ^
Why does this work and what would the expected behavior be?