This question already has answers here:
Is it possible to declare multiple static variables with same name in a single C file?
(6 answers)
Closed 8 years ago.
#include <stdio.h>
void func();
int main()
{
static int b = 20;
func();
}
void func()
{
static int b;
printf("%d", b);
}
Output: 0
Is a new memory location created when the second static variable is declared?
Yes it is. Each of your b variables is private to the function in which they are declared.
b in func and b in main are two different variables, they are not related, and their scope is inside each function that they are in.
when you declare a variable in your function
compiler first look the local variable in the current function
then if can not find it look for global variables
Yes new memory location is created for above two static variables.
For abov code, Each static variable defined in the function call has scope for the function itself.
Refer
Static and global variable in memory
Is a new memory location created when the second static variable is declared?
Yes.
Both the variable b in main and func are different although they have same name. static keyword only change the lifetime of a variable not its visibility.
"b" variables are private because they are used in the "function". Unique global varible can be used once but you can use same local variables unlimited times.
Example-
In this example I am declaring Global variable and Private variable
#include <iostream>
using namespace std;
int second() ;
int main()
{
char number;
cout << "Please input a value\n";
cin >> number;
cout << " You selected: " << number;
second(); // This should work in a C program, how do I do it in C++
system("PAUSE");
return 0;
}
int second();
{
char number;
cout << "Please select another value...";
cin >> number;
cout << "You selected: " << number;
}
Related
I had this assignment at school, wherein I had to find the output of the following C code, and also, to explain the output.
#include<stdio.h>
int i;
void fun1(void);
void fun2(void);
int main()
{
fun1();
fun2();
return 0;
}
void fun1(){
i=20;
printf("%d\t",i);
}
void fun2(){
int i=50;
printf("%d",i);
}
The output is 20 50
Because in fun1() the Global Variable 'i' is assigned to 20 and printed. And in fun2() the variable 'i' is a Local Variable, which is declared and initialized to 50, which is then printed.
I have this following question out of curiosity, how do I use the global variable 'i', in fun2()?
A simple solution would be to simply change the name and avoid the whole thing. But my curiosity is due to Java, where there is a keyword "this" to access class variable instead of a local variable.
so is there any way to do that in C?
The only way is to hide the declaration of the local variable in a code block.
For example
#include <stdio.h>
int i = 10;
void fun2( void )
{
int i = 20;
printf("local i = %d\n",i);
{
extern int i;
printf( "global i = %d\n",i);
}
}
int main(void)
{
fun2();
}
The program output is
local i = 20
global i = 10
There is no way to access a global parameter inside a function that has a local variable with the same name. It is usually bad practice to create such local variables in C though, as you saw, it is not prohibited.
In C++ you can solve it using namespaces but there is no equivalent in C.
The best way is to pass parameters to the function
void fun2(int fromExternalWorld){
int i=50;
printf("%d ",fromExternalWorld);
printf("%d\n",i);
}
int main(void)
{
fun2(i);
}
Otherwise is not possible to have two symbols with same name visible in the same scope.
You could cheat and create a pointer to the global i before declaring the local i:
void fun2( void )
{
int *ip = &i; // get address of global i
int i = 50; // local i ”shadows" global i
printf( "local i = %d, global i = %d\n", i, *ip );
}
EDIT
Seeing as this answer got accepted, I must emphasize that you should never write code like this. This is a band-aid around poor programming practice.
Avoid globals where possible, and where not possible use a naming convention that clearly marks them as global and is unlikely to be shadowed (such as prefixing with a g_ or something similar).
I can't tell you how many hours I've wasted chasing down issues that were due to a naming collision like this.
This question already has answers here:
How can I access a shadowed global variable in C?
(6 answers)
Closed 9 years ago.
If we have a variable "x" which is defined globally and another variable with the same name "x" inside a function. when we print the value of "x" why we always get the value which is assigned inside the function? Is there any way we can print the global variable value.
int x=8;
void testCode()
{
int x=2;
printf("%d",x); //prints 2
}
In other languages, yes. In C, there is no way to access the global in the same scope. There is a way to declare a nested scope (see other answers) which would give you access to a non-static global, but it is not recommended - don't do it. If you want to get the global variable, don't hide it with a local variable of the same name.
In C When Local variables Have same Name as Global variables then Local variable get highest priority.
This is why we are always getting the local variable values when printing.
if you print out side the blcok of local variable then will prints GLOBAL Value.
You can print global value by calling another function.where you did not declare local variable with the same name
Example:
int i=20;
int get_global();
main()
{
int i=5;
printf("LOCAL=%d",i));
printf("GLOBAL=%d",get_global());
}
int get_global()
{
return i;
}
You can print global over local in following manner:
int a=6;
int main()
{
int a=5;
printf("%d",a);
{
extern int a;
printf("%d",a);
}
}
You can do something like this:
int x = 8;
void testCode()
{
int x=2;
printf("%d",x); //prints 2
{
extern int x;
printf("%d", x); // prints 8
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I access a shadowed global variable in C?
how to access global variable in C , if there is local variable with same name?
int m=20 ;
void main()
{
int m=30;
}
In C, you can. Of course this is just trivia, you should never do so in real life.
Declaring something extern can be done anywhere, and always links the declared variable to the global of that name.
#include <stdio.h>
int i = 3;
int main( int argc, char **argv ) {
int i = 6;
printf( "%d\n", i );
{ // need to introduce a new scope
extern int i; // shadowing is allowed here.
printf( "%d\n", i );
}
return 0;
}
In C++, the global is always available as ::i.
In C there's no way. actually, introducing an extra scope and with an extern declaration you can, see #Potatoswatter's answer.
In C++ you can look up identifiers in the global namespace using :: (as in ::m=15), which, by the way, is the same operator used to access members of "regular" namespaces (std::cout, ...).
Also, it's int main().
In C you don't. Give them a different name, it's confusing and a bad practice anyway.
same name is bad practice anyway until m is redefined you are accessing the global variable any way
int m=20 ;
void main()
{
print m // 20 would be printed here .
// max you can do
int m=30;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I access a shadowed global variable in C?
In C++, I can use :: operator to specify a global variable. For example:
using namespace std;
int foo = 10;
int main(){
int foo = 5;
cout<<" Global variable: "<< ::foo <<endl;
cout<<" Local Variable: " << foo <<endl;
return 0;
}
How can I do this in C?
This was subject of an earlier question. It can be achieved as follows
int foo = 10;
int main(void) {
int foo = 5;
{
extern int foo;
foo++;
}
foo++
return 0;
}
However in practice, I can't imagine running into this problem, because I could always rename the local variable or create a small static inline function that can access the global foo and that I could call.
How can I access a shadowed global variable in C? In C++ I can use :: for the global namespace.
If your file-scope variable is not static, then you can use a declaration that uses extern in a nested scope:
int c;
int main() {
{
int c = 0;
// now, c shadows ::c. just re-declare ::c in a
// nested scope:
{
extern int c;
c = 1;
}
// outputs 0
printf("%d\n", c);
}
// outputs 1
printf("%d\n", c);
return 0;
}
If the variable is declared with static, i don't see a way to refer to it.
There is no :: in c but you can use a getter function
#include <stdio.h>
int L=3;
inline int getL()
{
return L;
}
int main();
{
int L = 5;
printf("%d, %d", L, getL());
}
If you are talking about shadowed global var, then (on Linux) you can use dlsym() to find an address of the global variable, like this:
int myvar = 5; // global
{
int myvar = 6; // local var shadows global
int *pglob_myvar = (int *)dlsym(RTLD_NEXT, "myvar");
printf("Local: %d, global: %d\n", myvar, *pglob_myvar);
}
If you want your code to look sexy, use macro:
#define GLOBAL_ADDR(a,b) b =(typeof(b))dlsym(RTLD_NEXT, #a)
...
int *pglob_myvar;
GLOBAL_ADDR(myvar, pglob_myvar);
...
Depending on what you call shielded global variable in C, different answers are possible.
If you mean a global variable defined in another source file or a linked library, you only have to declare it again with the extern prefix:
extern int aGlobalDefinedElsewhere;
If you mean a global variable shadowed (or eclipsed, choose the terminology you prefer) by a local variable of the same name), there is no builtin way to do this in C. So you have either not to do it or to work around it. Possible solutions are:
getter/setter functions for accessing global variable (which is a good practice, in particular in multithreaded situations)
aliases to globals by way of a pointer defined before the local variable:
int noName;
{
int * aliasToNoName = &noName; /* reference to global */
int noName; /* declaration of local */
*aliasToNoName = noName; /* assign local to global */
}
what is a "shielded global variable" in pure C?
in C you have local variables, file local/global variables (static) and global variables (extern)
so file1.c:
int bla;
file2.c
extern int bla;
Yet another option is to reference the global before defining your local, or at least get a pointer to it first so you can access it after defining your local.
#include <stdio.h>
int x = 1234;
int main()
{
printf("%d\n",x); // prints global
int x = 456;
printf("%d\n",x); // prints local
}