sahil arora
sahil arora

Reputation: 106

How can i access global symbol using function in c++?

I want to access the value assigned to global variable in main function from the function. I don't want to pass argument in function.

I have tried referring different stack overflow similar questions and C++ libraries .

#include <iostream>

long s;  // global value declaration

void output()  // don't want to pass argument
{
    std::cout << s;
}

int main()
{
    long s;
    std::cin >> s;  // let it be 5
    output()
}

I expect the output to be 5 but it shows 0.

Upvotes: 4

Views: 10879

Answers (6)

Luke Autry
Luke Autry

Reputation: 71

You defined output() on the global scope which means it will refer to the long s variable on the global scope not the long s defined in main().

Upvotes: 0

Tahir Musharraf
Tahir Musharraf

Reputation: 143

You can do simply and use:: before the global variable or just remove the

long s; 

From main() because as you are declaring local variable s in the main() function. The Global s and local s are different despite having the same name. lets learn more by the following example by giving local and Global variable different name.

    #include <iostream>

long x;  // global value declaration

void output()  // don't want to pass argument
{
    std::cout << x;
}

int main()
{
    long s;
    std::cin >> s;  //here you are storing data on local variable s
    output() // But here you are calling global variable x. 
}

In main() function s is local and x is global variable and you are calling the global variable in output() function. you can use:: (scope resolution operator) for calling global x in main if u have the same naming or just call as it by variable name if they have a different name.

PS: If you have any question just do comment down hope this will help you and understand what's the mistake. Read more about the scope of the local and global variable here

Upvotes: 0

Swordfish
Swordfish

Reputation: 13134

It is important for you to know that the local variable s declared in main() and the variable s declared at file scope aren't identical despite the name.

Since the local variable s declared in the function main() shadows (see Scope - Name Hiding) the global variable s you have to use the Scope Resolution Operator :: to access the global variable s declared at file-scope:

#include <iostream>

long s;

void output()
{
    std::cout << s;   // no scope resolution operator needed because there is no local
}                     // s in output() shadowing ::s

int main()
{
    long s;           // hides the global s
    std::cin >> ::s;  // qualify the hidden s
    output()
}

... or get rid of the local s in main().

That said using global variables (without real need) is considered very bad practice. See What’s the “static initialization order ‘fiasco’?. While that doesn't affect PODs it will bite you sooner or later.

Upvotes: 1

To access a global variable you should use of :: sign before it :

long s = 5;          //global value definition

int main()
{
    long s = 1;              //local value definition
    cout << ::s << endl;     // output is 5
    cout << s << endl;       // output is 1
}

Also It's so simple to use global s in cin :

cin >> ::s;
cout << ::s << endl;

Please try it online

Upvotes: 6

Shovo
Shovo

Reputation: 129

First of all, when you declare a global variable without assigning a value it automatically sets it's value to 0.

And another thing, you should know about your scope. The variable s in your main function has no existence in the output function.

In the output function , you are getting 0 because your global variable is set to 0.

You can just change you global variable's value by assigning different value to it.

long s; //global value declaration
void output() //don't want to pass argument
{
   cout<<s;  
}
int main()
{
   cin>>s; //let it be 5
   output()
}

Upvotes: -1

Roya Ghasemzadeh
Roya Ghasemzadeh

Reputation: 691

You are declaring another variable s in your main function. line 7 of your code. and it's the variable which is used in cin. either delete that line or use :: before s.

long s;
cin >> ::s; //let it be 5
output();

Upvotes: 2

Related Questions