Help please.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int n+17;

void fun()
{
   printf ("n = %d\n", n);
}

void main ()
{
   int n=4;
   printf ("n = %d\n", n);
   fun();
}


My question is what is the scope of either "int" variable?


Another quesion is are those variable stored in static, stack, heap?
I think it's stack but not sure.

And finally what would be the output? I'm little confused. 
My question is what is the scope of either "int" variable?

The first is in the global scope, the second is local to main().

Another quesion is are those variable stored in static, stack, heap?

You're talking about different things, stack and heap usually refer to memory layout, static usually refers to linkage or storage duration. The global variable is usually allocated on the heap and usually has static storage duration. The local variable is usually allocated on the stack and has automatic storage duration.

And finally what would be the output? I'm little confused.

The printf() in main should print 4. The printf() in the function will print an undefined value because you didn't properly initialize the variable.

Topic archived. No new replies allowed.