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.
Related
This question already has answers here:
Can local and register variables be declared extern?
(5 answers)
Closed 3 years ago.
Assume the following:
test1.h
extern int a;
void init();
test1.c
#include <stdio.h>
#include "test1.h"
int main()
{
init();
printf("%d\n", a);
return 0;
}
test2.c
#include "test1.h"
void init()
{
int a = 1;
}
Above will give me a linker error saying a isn't defined, but it is defined under init().
However, if I change test2.c like this then it will work:
int a = 1;
void init()
{
//
}
Does extern have to be defined globally only?
The internal or external linkage may have only variables that have a file scope.
In your first example
void init()
{
int a = 1;
}
the variable a has a block scope and hence does not have a linkage.
In the header
extern int a
there is declared a variable with external linkage that has the file scope. But it was not defined. So the linker issues an error because it can not find the variable definition.
You may declare a block scope variable with the extern specifier. But it will not be a variable definition. The declaration will refer to a variable with a linkage declared in a file scope.
For example consider the following demonstrative program
#include <stdio.h>
int x = 10;
void f( void )
{
extern int x;
printf( "x = %d\n", x );
}
int main(void)
{
f();
x = 20;
f();
return 0;
}
Its output is
x = 10
x = 20
That is there is defined the file scope variable x with the external linkage. And within the outer block scope of the function f the declaration of the variable x refers to the variable x in the file scope. It does not define a new object.
This question already has answers here:
How to access global variable when there is a local and global conflict [duplicate]
(3 answers)
Closed 3 years ago.
From the below code how to print global value:
Conditions are that, we must not use extern keyword, should not comment the local initialization & should not shift the printf function.
#include <stdio.h>
int a = 20;
int main()
{
int a = 10;
printf("%d",a);
}
I expect the output of 20.
extern will not help as you have another variable with the same name in most inner scope (in this case scope of function main). So you will see this variable instead if the global one.
You need to rename one of those variables.
int a=20;
int main()
{
int b=10;
printf("%d",a);
}
you may have more inner scopes:
int a=20;
int main()
{
int a=10;
{
int a = 5;
{
int a = 2;
printf("%d",a);
}
}
}
and the result is 2
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;
}
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:
How to print value of global variable and local variable having same name?
(4 answers)
Closed 5 years ago.
for example
#include<stdio.h>
int foo = 100;
int bar()
{
int foo;
/* local foo = global foo, how to implemented? */
return 0;
}
int main()
{
int result = bar();
return 0;
}
I think in the function bar, calling foo directly will just get the global foo. How can I refer the local foo? I know in C++, there is this pointer. However, does C has something similar?
Thanks a lot!
No, by declaring foo in bar(), you have taken the global foo out of scope. Inside bar() when you refer to foo you get the local variable.