0

I'm getting answer i = 2. But my question is what happened to i=0??? According to my understanding i=0 will be in DATA SEGMENT I=2 will be in STACK SEGMENT

#include <stdio.h>
int i = 0;
void main()
{
    int i = 2;
    printf("i value is %d\n",i);`
}
Shaac
  • 195
  • 14
  • 3
    `i == 2;` `::i == 0;` – MFH Oct 03 '13 at 09:28
  • You hid it. You hid `i` by using the same identifier for another variable. Why did you do that? – Daniel Daranas Oct 03 '13 at 09:29
  • 4
    Apart from your question, you should never use a `void main()` signature. Use `int main()` instead. – Shaac Oct 03 '13 at 09:29
  • 3
    @MFH and in standard C you would... wish you could do what you can in C++ ? – WhozCraig Oct 03 '13 at 09:30
  • When global variable and local variable have same name, then local variable got priority inside the block.When ever if you print inside the block where you declared local variable this results with local variable value and if you try using in another block where you did not declare the variable locally. That uses Global value. – Gangadhar Oct 03 '13 at 09:34
  • @WhozCraig: missed that one. Well in C you're screwed and have to be extra cautious not to shadow variables… Yet another thing I'll add onto my standard answers on why to avoid C… Well there is a hacky solution: http://stackoverflow.com/questions/13776588/accessing-global-variable-hidden-by-local – MFH Oct 03 '13 at 09:40

5 Answers5

6

Local variable i hides the global variable i. Hence, when you print it, it'll print the local variable.

If you want modify the global variable, you can use the idea mentioned here:

How can I access a shadowed global variable in C?

Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238
3

Shadowing. Your global variable i = 0 is shadowed by local i = 2.

Don't do it, avoid name collisions.

By the way, void main() {} is not standard C (assuming your program is running in a hosted environment, i.e. on top of an OS). Use int main(void) { return 0; } instead. Reference. Another one.

Community
  • 1
  • 1
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • 2
    Actually `int main()` is not standard C either. You should use `int main(void)` if you don't care about the arguments. – user694733 Oct 03 '13 at 09:56
0

You have declared a local variable with the same name as your global variable, hiding it by making i (when used within the very same scope where your local variable has been declared) refer to your local variable rather than the global one.

The most natural and logical solution for this is: Avoid doing it always when possible.

LihO
  • 41,190
  • 11
  • 99
  • 167
0

It is the variable scope working out here. If you have multiple variables of same type is declared, the operator available with the near most scope will get accessed.

The scope resolution will happen in the compile time. When the compiler search for any variable declaration when it is accessed in the code, it will first look in the nearest scope and then go up. The global, variable will be accessed last.

joe
  • 1,136
  • 9
  • 17
0

Suppose you have a global variable name i and you have two functions function1 and function2. In both functions, you printed the value of i. In function1 you have declared the value of i again. So in function1 you have a local variable i.

#include<stdio.h>
int i = 10;
    
void function1()
{
    int i = 20;
    printf("%d\n", i);
}
    
void function2()
{
    printf("%d", i);
}
    
int main()
{
    function1();
    function2();
    return 0;
}

The compiler will consider the local variable i in function1 and print 20. On the other hand in the function2, there is no local variable named i. So it will print the global variable i = 10.

Muhammad Mohsin Khan
  • 1,444
  • 7
  • 16
  • 23