473,388 Members | 1,400 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,388 software developers and data experts.

Identifier collisions between global level and static identifiers

Hi,

#include <stdio.h>
#include <stdlib.h>
int var;
int main(void)
{
static int var;
/* Does this name have a separate existence as compared to the global var? */
return EXIT_SUCCESS;
}

What does the standard say about this situation? Is this legal?

Regards and thanks,
Anupam
Nov 14 '05 #1
3 1797
an**************@persistent.co.in (Anupam) wrote:
#include <stdio.h>
#include <stdlib.h>
int var;
int main(void)
{
static int var;
/* Does this name have a separate existence as compared to the global var? */
Yes. Within main(), var refers to the static local object. Outside
main(), var refers to the filescope object.
return EXIT_SUCCESS;
}

What does the standard say about this situation? Is this legal?


Yes.

Richard
Nov 14 '05 #2
On 6 Feb 2004 04:56:10 -0800, an**************@persistent.co.in
(Anupam) wrote:
Hi,

#include <stdio.h>
#include <stdlib.h>
int var;
int main(void)
{
static int var;
/* Does this name have a separate existence as compared to the global var? */
return EXIT_SUCCESS;
}

What does the standard say about this situation? Is this legal?
static or no static, simple scoping rules control visibility.

Here's an expansion of your example:

#include <stdio.h>
#include <stdlib.h>
int var = 50;
int main(void)
{
{
printf("value of var is %d\n", var); // 50

static int var = 1000; // hides file scope version
printf("value of var is %d\n", var); // 100
}
printf("value of var is %d\n", var); // 50

return EXIT_SUCCESS;
}

This would be an offensive thing to do in real code, but illustrates
the scoping rules... (requires C99 or C++ to compile, though)
-leor

Regards and thanks,
Anupam


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
Anupam wrote:

#include <stdio.h>
#include <stdlib.h>
int var;
int main(void)
{
static int var;
/* Does this name have a separate existence as compared to the global var? */ return EXIT_SUCCESS;
}

What does the standard say about this situation? Is this legal?


Yes, function scope, yes respectively.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark F. Haigh | last post by:
In Andrei Alexandrescu's book _Modern C++ Design_, Chapter 8 ("Object Factories"), Section 8.4 ("Type Identifiers"), he states: The only problem that remains is the management of type...
2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
3
by: Zhigang Cui | last post by:
Hi, When I saw 'global static', the first response was there was no such term. But I realized it's an idiom later. When we learn C language, when we study C standard, when we study compiler, we...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
23
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over...
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
10
by: teddarr | last post by:
I am trying to construct a class with several functions and identifiers. The identifier in question is static double AIR which will hold the value of the annual interest rate in the class. I have...
0
by: Gordon Burditt | last post by:
>----------------------------- 8< ---------------------------------- Not if you include <string.h>, in which case the second paragraph above is an issue. Otherwise, it's OK. It's OK, but I...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.