473,399 Members | 3,106 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,399 software developers and data experts.

Local Vs Global variabls

Suppose that we have declared a global variable say int x in C language. A variable with the same name i.e., int x is also declared in a function so that it is a local variable to that function. Now, the question is how to access global value of x in that function?
Sep 3 '09 #1
4 2259
JosAH
11,448 Expert 8TB
Have a look at this recent thread.

kind regards,

Jos
Sep 3 '09 #2
OraMaster
135 100+
@vidyagouri
Hello Vidya
In C, the local variable takes precedance over the global, unless the global is declared static, in which case the local one cannot even be declared.

Regds,
Bhushan
Sep 4 '09 #3
JosAH
11,448 Expert 8TB
@OraMaster
Who told you that nonsense?

kind regards,

Jos
Sep 4 '09 #4
Banfa
9,065 Expert Mod 8TB
No declaring a global static does not prevent you declaring a local variable with the same name, for example this is valid

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. static void PrintGlobal(void);
  4.  
  5. static int V1 = 5;
  6.  
  7. int main()
  8. {
  9.     int V1 = 10;
  10.  
  11.     printf("V1: %d\n", V1);
  12.  
  13.     PrintGlobal();
  14.  
  15.     return 0;
  16. }
  17.  
  18. static void PrintGlobal(void)
  19. {
  20.     puts("Global Data");
  21.     printf("V1: %d\n", V1);
  22. }
  23.  
Output:
V1: 10
Global Data
V1: 5
Sep 4 '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...
0
by: mjcsfo | last post by:
I can't seem to find a reference nor any helpful threads on this topic. I've gotten the following error in two circumstances: 1. A complex type has nested within it another complex type, in the...
1
by: daniel.bron | last post by:
I'm maintaining a C++ application written by a developer who has now left. The app is a multithreaded client/server app for financial data. My compiler is MS visual C++ 6.0. I'm a C++ neophyte....
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;
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
8
by: Sullivan WxPyQtKinter | last post by:
I am confused by the following program: def f(): print x x=12345 f() result is: 12345
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.