What is the output?

I want to double check myself, not 100% sure. Any help is appreciated, thanks in advance.

int g;
void afun(int x)
{
int y=0;
static int z=0;
x++; y++; z++; g++;
cout << x << ' ' << y << ' ' << z << ' ' << g << endl;
}
void main( )
{
int a=7;
afun(a);
g++;
afun(a);
cout << a << ' ' << g << endl;
}
Main should return an int but anyways why can't you compile it yourself? Or are you just not willing to compile it yourself? Or you could alternatively follow the code

Also keep in mind globals are 0 initaliEd and statics have a lifetime of globals but in this case local to a function
Last edited on
What is the output?
error: '::main' must return 'int'

After fixing that, here is expected output:
8 1 1 1
8 1 2 3
7 3
If you cannot compile it yourself, here is some sites:
http://ideone.com/
http://coliru.stacked-crooked.com/
http://melpon.org/wandbox/
http://cpp.sh/
Last edited on
Output of g is undefined. You don't initialize it.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Output of g is undefined. You don't initialize it.
Global variables are zero-initialized:
Standard wrote:
3.6.2 Initialization of non-local variables [basic.start.init]  
2 Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.
Last edited on
Topic archived. No new replies allowed.