473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to differentiate local copy and global copy of a variable with the same name

72
Hi All,

My question is I have one global variable say num, and one local (to main) variable with the same name.

In main how can I access global variable? In C++ ::(scope resolution operator) will avoid this problem, Is there any such operator in C?

Thanks,
Manjunath
Aug 25 '09 #1
4 2806
JosAH
11,448 Expert 8TB
@manjuks
C doesn't have such an operator so you have to 'get out of' your main() function to be able to 'see' your global variable 'sum' again; note that C (and C++) use lexical scoping so any other function that doesn't define an identifier 'sum' can access the global variable.

kind regards,

Jos
Aug 25 '09 #2
manjuks
72
Hi Jos,

Thanks for your reply.

Thanks,
Manjunath
Aug 25 '09 #3
Banfa
9,065 Expert Mod 8TB
It would, of course, be better not to have global and local variables with the same name in the first place. In fact some people use a variable naming convention where global variables have a prefix, g or g_ are common, specifically to avoid this and to make the programmer aware when they are using global data.

Another point of view is that global data is generally bad and should just be avoided.
Aug 25 '09 #4
donbock
2,426 Expert 2GB
This name-scope issue can be completely local to a single function:
Expand|Select|Wrap|Line Numbers
  1. void confusion(int a) {
  2.    ...
  3.    if (a > 0) {
  4.       float a;
  5.       ...
  6.    } else {
  7.       long a;
  8.       ...
  9.    }
  10.    ...
  11. }
Ths function has three different variables named "a", each with a different type. As you move from block to block you gain access to one of these "a" variables but lose access to the others. Such usage may be legal, but it is unwise.
Aug 25 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kyle Root | last post by:
I'm writing a little program that compares two versions and tells you whether you are upgrading, downgrading, or it's the same version. Unfortunately, I haven't gotten very far, in fact I'm at a...
3
by: Mart Rogers | last post by:
We have a situation where we have decided (in an attempt to keep things simple) to stick to local assemblies rather than global ones. However we now have the following situation looming AssyA ...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
9
by: Shilpa | last post by:
Hi, I just wanted to know whether we can access global variable within a local block , where both variables are having same name. For ex: int temp=5 ; { int temp=10;
4
by: Mike | last post by:
Hi ! I have some strange problem and I would like to know if it is a bug or not : In my projects, in 2 different .cpp files, I use the same name to define a local structure: file1.cpp : ...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
2
by: Matt Nordhoff | last post by:
Sebastjan Trepca wrote: Python doesn't like when you read a variable that exists in an outer scope, then try to assign to it in this scope. (When you do "a = b", "b" is processed first. In...
2
by: zheng4t | last post by:
I am learning C++. I found the following code in the book The C++ Programming Language by Bjarne Stroustrup. struct Pair { string name; double val; }; vector<Pairpairs; double& value(const...
4
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.