473,765 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum types and extern

I tried to post this before and I apologize if I am repeating myself,
but I do not see the post anywhere.

But anyway,

I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:

enum myenum
{
val1,
val2,
val3,
} enum_var;

When I try to reference enum_var in data.h I always get an error. I
have tried numerous ways of referencing it and defining it but I
always seem to get a compiler error.

So how can I externally reference enum_var in data.h so that I can use
enum_var in my program? And if I am totally missing the point on
this, if someone could clear up my confusion about enum or extern that
would be great.

Thank you,

Charlie

Apr 24 '07 #1
10 3034
On 24 Apr 2007 08:19:40 -0700, in comp.lang.c , Charlie
<ev**********@y ahoo.comwrote:

>I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:
<snip>
>When I try to reference enum_var in data.h I always get an error.
Move the definition of the enum to your header, put the variable in
the source file and the extern reference in the header, below the enum
definition.

By the way, globals are generally considered a bad idea in C.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Apr 24 '07 #2
Mark McIntyre wrote:
>
.... snip ...
>
By the way, globals are generally considered a bad idea in C.
Depends on the application.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 25 '07 #3
CBFalconer wrote:
Mark McIntyre wrote:
>>
... snip ...
>>
By the way, globals are generally considered a bad idea in C.

Depends on the application.
Most natural numbers are greater than 17, despite the existence of
5, 11, and 15.

--
"How am I to understand if you won't teach me?" - Trippa, /Falling/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Apr 25 '07 #4
In article <f0**********@m urdoch.hpl.hp.c om>,
Chris Dollin <ch**********@h p.comwrote:
>>By the way, globals are generally considered a bad idea in C.
>Depends on the application.
>Most natural numbers are greater than 17, despite the existence of
5, 11, and 15.
In practice, most natural numbers are 1.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 25 '07 #5
Richard Tobin wrote:
In article <f0**********@m urdoch.hpl.hp.c om>,
Chris Dollin <ch**********@h p.comwrote:
>>>By the way, globals are generally considered a bad idea in C.
>>Depends on the application.
>>Most natural numbers are greater than 17, despite the existence of
5, 11, and 15.

In practice, most natural numbers are 1.
One application global variable does sound about right.

--
"Our future looks secure, but it's all out of our hands." /Man and Machine/
- Magenta

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Apr 25 '07 #6
Chris Dollin said:
Richard Tobin wrote:
>In article <f0**********@m urdoch.hpl.hp.c om>,
Chris Dollin <ch**********@h p.comwrote:
>>>>By the way, globals are generally considered a bad idea in C.
>>>Depends on the application.
>>>Most natural numbers are greater than 17, despite the existence of
5, 11, and 15.

In practice, most natural numbers are 1.

One application global variable does sound about right.
I'd consider 1 to be an upper bound. On average, it should be less than
that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 25 '07 #7
Richard Heathfield wrote:
Chris Dollin said:
>One application global variable does sound about right.

I'd consider 1 to be an upper bound. On average, it should be less than
that.
My last chunk of C [about 20K code+comments] seems to have
8 (according to a makeshift grep of the header files).

Three are equivalents of std[in,out,err], one's a debugging flag,
one's an allocation pool, one's a debugging flag, one's a title
string, and the other two are names/formats of shortcodes.

So seven of them are really constants (ie, they're set up once
and not changed thereafter), of which at least two could be
put behind functional bars.

Not too bad: could do better. (Yes, They could all be wrapped
inside one [pointer to] state variable.)

--
"The path to the web becomes deeper and wider." - October Project

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Apr 25 '07 #8
Chris Dollin wrote:
Three are equivalents of std[in,out,err], one's a debugging flag,
one's an allocation pool, one's a debugging flag, one's a title
string, and the other two are names/formats of shortcodes.
Oops, sorry about the echo echo.

--
"It is seldom good news." ~Crystal Ball~, /The Tough Guide to Fantasyland/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Apr 25 '07 #9
Richard Tobin wrote:
Chris Dollin <ch**********@h p.comwrote:
>>>By the way, globals are generally considered a bad idea in C.
>>Depends on the application.
>Most natural numbers are greater than 17, despite the existence
of 5, 11, and 15.

In practice, most natural numbers are 1.
Please do not remove attributions for lines you quote.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 25 '07 #10

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

Similar topics

11
37092
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't explicitly say about enum's forward declaration, but one example shows it in 18.2.1, clause 4:
2
5495
by: Sandy | last post by:
I have an enum like this file1.cpp enum e {e1, e2}; extern void fun(); void main() { fun(); }
31
3616
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
12
3351
by: Laurent Deniau | last post by:
If I understand well, an enumeration is only garantee to hold at most an int (6.7.2.2-2). So I would like to know: how to store a long in an enum? enum { p2_31 = 1L << 31 }; // boom how to define a synonym of a constant address?
34
11201
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
12
6722
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: public enum Days { Sunday };
2
4764
by: Charlie | last post by:
I use a file to define all of my global variables, data.c, and then I create a header file, data.h, where I reference all the variables with the keyword "extern." I include the header in every file. I define an enum and variable of that type in data.c, enum myenum { val1, val2,
12
22753
by: Charlie | last post by:
I have a file, data.c, where I define all of my global variables. I then have a header file, data.h, which I include in every file in which I reference all of the variables defined in data.c. For the most part, I am having no problem with this. However, I defined an variable of type myenum in data.c: enum myenum {
7
11154
by: bashill.zhu | last post by:
bzhu@TY-PC /h/working/tcplex/ch8/testenum $ cat lexer.h namespace Lexer { enum Token_value; extern Token_value string_value; void get_token(); } bzhu@TY-PC /h/working/tcplex/ch8/testenum
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9955
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9833
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.