How to access global value without using extern keyword? [duplicate] - c

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

Related

defining an extern variable in a fucntion [duplicate]

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.

Is there a scope resolution operator in C language?

I am reading a book on the C language ('Mastering C'), and found the topic on scope resolution operator (::) on page 203, on Google Books here.
But when I run the following code sample (copied from the book), the C compiler gives me an error. I searched on the internet but I am unable to find any reference to a scope resolution operator in C.
#include <stdio.h>
int a = 50;
int main(void)
{
int a =10;
printf("%d",a);
printf("%d\n", ::a);
return 0;
}
So if I want to access a global variable then how could I do that from within the main() function ?
No. C does not have a scope resolution operator. C++ has one (::). Perhaps you are (or your book is) confusing C with C++.
You asked how you could access the global variable a from within a function (here main) which has its own local variable a. You can't do this in C. It is lexically out of scope. Of course you could take the address of the variable somewhere else and pass that in as a pointer, but that's a different thing entirely. Just rename the variable, i.e. 'don't do that'
No, namespaces are a feature of C++.
It is, however, possible to refer to global a in your example.
You can achieve this by using the extern keyword:
#include <stdio.h>
int a = 50;
int main(void)
{
int a = 10;
printf("%d\n",a);
{ // Note the scope
extern int a; // Uses the global now
printf("%d\n", a);
}
return 0;
}
That's a bit tricky, though. It's bad style. Don't do that.
:: operator is available in C++ not C. If you wanted to access the global variable, use
#include <stdio.h>
int a = 50;
int main(void)
{
int a =10;
printf("%d",a); //prints 10
{
extern int a;
printf("%d", a); //prints 50
}
return 0;
}
Or you could use a pointer which holds the address of the global variable a and then dereference the pointer if you want to print the value of the global variable a.
No (the :: operator is C++ specific). In C, if you use the same identifier in different overlapping scopes (say, file scope a and block scope a), the block scope identifier shadows the file scope identifier and there is no way to refer to the shadowed identifier.
It is generally best programming practice to avoid shadowed variables. Many lint type programs can warn about this situation.
In plain C, there is no scope resolution. You have to name your variables differently.
That means that all variables a below are different ones:
#include <stdio.h>
int a = 50;
int main(void)
{
int a = 10;
{
int a = 20;
int i;
for (i = 0; i < 10; i++) {
int a = 30;
}
}
return 0;
}
You may use pointers to access and edit global variables in C.
#include <stdio.h>
#include <stdlib.h>
int a;
a=78;
int *ptr=&a; //pointer for global a
int main()
{
int a=0;
printf("%d",a); //Prints 0 as local variable
printf("%d",*ptr);
ptr=30; //changes the value of global variable through pointer
printf("%d",*ptr); //Now it prints 30
return 0;
}
It entirely depends on what kind of compiler you're using to execute your code.
#include <stdio.h>
int a = 50;
int main(void)
{
int a =10;
printf("%d\n",a);
printf("%d\n", ::a);
return 0;
}
Try running this code in Turbo C compiler, and you will get the results.
Now, regarding the C book that you're following, the examples codes present in the book must be in terms with Turbo C/C++ compiler and not the gcc compiler.

Global value is not acessable in another file? [duplicate]

This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 8 years ago.
Global value is not accessible in another file?Mycode is below please help me to fix
flie1.c
#include<stdio.h>
extern int i=9;
int main()
{
printf("i m in main\n");
}
file2.c
printf("%d\n",i);
i am compiling both file at once as cc file1.c file2.c
Change it like this, and it will work:
file1.c
#include <stdio.h>
int i = 9; // define the variable
void print(); // declare print: say there is a function called `print` with no arguments and no return type (because the function is DEFINED in file2.c)
int main() {
printf("im in main");
print();
return 0;
}
file2.c
extern int i; // declare "i": say there is a variable called `i` with type of `int` (because the variable is DEFINED in file1.c)
void print() {
printf("%d\n", i);
}
When you have an extern variable, it should also have its 'original' declaration (without using extern). extern simply says ' this variable is defined elsewhere', so you need to define the variable somewhere.
Simply add:
int i=9;
to file2.c (in the top of the file, the 'globals' area')
and you can change your extern declaration to:
extern int i;
(without the assignment of the value 9) in file1.c

understanding the concept of a global variable [duplicate]

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
}
}

How I can access a hidden variable in C [duplicate]

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.

Resources