Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

11
  • int main = 0; should not compile live: godbolt.org/z/uh3nm6 Commented Nov 29, 2019 at 9:28
  • 6
    "...the name main in the global namespace is reserved for functions...": source: en.cppreference.com/w/cpp/language/main_function Commented Nov 29, 2019 at 9:35
  • 10
    If you're using the name main for a variable, you're doing it wrong. It's undefined behaviour. Don't waste your time investigating why it compiles on certain compiles and not on others and why it segfaults Commented Nov 29, 2019 at 9:38
  • 1
    @ManthanTilva if you really want to find out what happens, look at the generated assembly output. No code is actually generated and the code that will be executed is whatever junk is in memory. Commented Nov 29, 2019 at 9:42
  • 3
    Undefined behavior is behavior that is undefined. It's like tossing down a bottle from a skyscraper, then afterwards go down on the street to see what damage it caused, if any. During some circumstances it was crash into cars, in some cases it will hit a pedestrian, in other cases nobody gets hurts. Who cares why, stop throwing bottles. Just because the owner of the skyscraper has not banned you from using the elevator, it doesn't mean that they take any responsibility for you throwing bottles nor will they ensure that your bottles fly in some deterministic manner. The skyscraper is just fine. Commented Nov 29, 2019 at 10:12