13

Here is my code , I want to print 15 and 12 but due to instance member hiding the local value of a is getting printed twice.

#include <stdio.h>                                  
int a = 12;             
int main()          
{           
    int a = 15;             
    printf("Inside a's main local a = : %d\n",a);                  
    printf("In a global a = %d\n",a);            
    return 0;           
}

Why and is there any way to print it in c ? ... BTW I know it in c++.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • It's #include in first line of code – Omkant Aug 29 '12 at 18:05
  • @tuğrulbüyükışık: That's nonsense. – jamesdlin Aug 29 '12 at 19:27
  • @ tuğrul büyükışık ..... That's not working how does my program know about the filename of that code itself... It will give compilation error. It's not correct – Omkant Aug 30 '12 at 05:35
  • @gsamaras: I don't think it's appropriate to edit someone else's code for such a small cosmetic change (adding spaces around `=`), when the original code was perfectly correct. I won't revert the edit because that wouldn't be helpful either. – Keith Thompson Jul 19 '16 at 01:44
  • @KeithThompson you are right, sorry! I was dragged here by a new (wrong) answer. My goal was to fully edit that question and then wanted to abandoned the edit, but instead of hitting the cancel button, I tried to do that manually and forgot that it was that way. Really sorry. – gsamaras Jul 19 '16 at 02:03
  • @gsamaras: Ok, stuff happens. IMHO it looks slightly better with the spaces. Not a big deal either way. – Keith Thompson Jul 19 '16 at 02:05

4 Answers4

26

Use the extern specifier in a new compound statement.

This way:

#include <stdio.h>      

int a = 12;             

int main(void)          
{           
    int a = 15;             
    printf("Inside a's main local a = : %d\n", a);

    {
        extern int a;
        printf("In a global a = %d\n", a);
    }

    return 0; 
}
ouah
  • 142,963
  • 15
  • 272
  • 331
3

I know this doesn't directly answer your question, but the best way to do this is to change the name of the local variable so it doesn't conflict with the name of the global variable.

If you have control of the code inside the function (i.e., you can add an extern declaration to make the global variable visible), then you can just as easily change the name of the variable.

It's impossible to tell what name would be better. In practice, the variables will undoubtedly have more descriptive names than a. The way they're used should give you some guidance about good names for them.

If they actually serve the same purpose, they probably don't both need to exist. You might remove variable that's local to main(), or, perhaps better, remove the global and pass the local (or its address) to other functions that need to access it.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

I think i found my answer in a way... it works

#include <stdio.h>

int a = 5;


int main()
{
    int a=10;
    if(1)
    {
        extern int a;
        printf("global: %d\n", a);
    }
    printf("local: %d\n", a);
    return 0;
}
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Omkant
  • 9,018
  • 8
  • 39
  • 59
-1

add :: for global ambit

#include <stdio.h>                                  
int a=12;             
int main()          
{           
  int a=15;             
  printf("Inside a's main local a = : %d\n",a);                  
  printf("In a global a = %d\n",::a);            
  return 0;           
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305