4

After running this code:

#include <stdio.h>
int x;
int main(void)
{
    printf("%d\n",x);
    return 0;
}
int x=5; 

I expected the output should be 0. Because of the sequence control structure of the program int x; should be executed first and then 0 is printed and finally int x=5; should be executed. But it is giving the output 5.

How is the program accesses 5 for the x in printf?

haccks
  • 104,019
  • 25
  • 176
  • 264

3 Answers3

7

The first acts as a forward declaration, and the later acts as the actual definition.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Isn't the statements get executed sequentially and it should print `0` (as global variables are set to `0`)? Edited the question. – haccks Jan 17 '14 at 13:35
  • 1
    @haccks All initialization of global or static variables gets done well before `main` is called. In fact, in a case like yours the compiler may even set the value already during compilation, so it's already set when the executable is loading into memory. – Some programmer dude Jan 17 '14 at 13:48
  • @haccks By the way, this is what the "data" segment in object and executable files are for, to store initialized data like your variable. Uninitialized data (like you would have if you didn't initialize the variable `x` in your case) would end up in the "bss" segment, which is zeroed before `main` is called. – Some programmer dude Jan 17 '14 at 13:56
  • That mean after compilation, `5` get set to `x` already, right? – haccks Jan 17 '14 at 13:58
  • 1
    @haccks Yes that's correct. It's set already in the executable file and the value is loaded when the program is loaded. Also, declarations and definitions are not really statements that have to executed in any special order. In fact, complex initializations can take place in any order the compiler wants it to (it's implementation dependent). – Some programmer dude Jan 17 '14 at 14:04
  • Thanks. Got it. (I guessed that before but not sure about that :) ). – haccks Jan 17 '14 at 14:11
3

Variable default values declared outside of functions get set before main ever runs. So what you are seeing is the correct behavior. Same goes for variables declared in other source files.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
3

Global variables are initialised before main() runs. That means that it's entirely possible for a function to access something which appears after it in the file, as long as it's visible (i.e. forward declared).

With that said, you shouldn't really have multiple declarations for the same variable in one file. It could lead to confusion (mainly for the programmer) about what and where it's actually initialised.

EDIT: To clarify, functions/variables in global scope are not executed like a sequence of statements inside a function. The location of the function's declaration/definition has absolutely no bearing on when it gets called in relation to any other code. It only determines what parts of the surrounding scope are visible to it. In your case, this means main() does not get called in between your two int lines. It gets called by the runtime when it's finished all other initialisation.

Peter Bloomfield
  • 5,578
  • 26
  • 37