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

enum question.

Consider the following code:

#include <stdio.h>

#define SOMEVAL 1234
enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW };

void print_val(enum tree_types tree)
{
printf("tree is: %d\n", tree);
}

int main()
{
enum tree_types trees;
trees = PINE;
print_val(trees);
trees = OAK;
print_val(trees);
trees = ELM;
print_val(trees);
trees = SOMEVAL;
print_val(trees);
return 0;
}

Why is the assignmnt with SOMEVAL allowed? I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?

Thanks.

--
Email: The handle, (dot seperated), at gmail dot com.
Mar 10 '07 #1
13 2428
At_sea_with_C wrote:
Consider the following code:

#include <stdio.h>

#define SOMEVAL 1234
enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW };

void print_val(enum tree_types tree)
{
printf("tree is: %d\n", tree);
}

int main()
{
enum tree_types trees;
trees = PINE;
print_val(trees);
trees = OAK;
print_val(trees);
trees = ELM;
print_val(trees);
trees = SOMEVAL;
print_val(trees);
return 0;
}

Why is the assignmnt with SOMEVAL allowed? I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?
It's allowed because "what you thought" is wrong. In C, an
enum is merely a disguise for some kind of integer (the compiler
chooses which kind), plus the introduction of some named constants
(all of type `int', accept no substitutes).

One semi-useful application of this freedom is when the enum
values aren't just arbitrary numeric codes, but can be combined
in meaningful ways:

enum CarStatus {
ALL_GREEN = 0x0,
DOOR_OPEN = 0x1,
BELTS_FASTENED = 0x2,
HEADLIGHTS_ON = 0x4
} status;
...
status = DOOR_OPEN | HEADLIGHTS_ON;

The upshot is that C's enum is mostly useful for documentation,
not for enforcement. Also, some debuggers are able to convert
enum values to the names; in your example a debugger might be
able to show you PINE instead of 10.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 10 '07 #2
On Mar 10, 8:24 am, At_sea_with_C <blindal...@dev.null.invalidwrote:

(snipped)
I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?

Thanks.

An excerpt from a draft of the C standard (n869.pdf):

"Annex I
(informative)
Common warnings
1 An implementation may generate warnings in many situations,
none of which are specified as part of this International Standard.
The following are a few of the more common situations.

....

A value is given to an object of an enumeration type other than
by assignment of an enumeration constant that is a member of that
type,
or an enumeration variable that has the same type,
or the value of a function that returns the same enumeration type
(6.7.2.2)"
So, I suppose some implementations may
generate the type of warning you had expected, but
it's not required.

--
Hope this helps,
Steven

Mar 10 '07 #3
At_sea_with_C wrote:
Consider the following code:

#include <stdio.h>

#define SOMEVAL 1234
enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW };

void print_val(enum tree_types tree)
{
printf("tree is: %d\n", tree);
}

int main()
{
enum tree_types trees;
trees = PINE;
print_val(trees);
trees = OAK;
print_val(trees);
trees = ELM;
print_val(trees);
trees = SOMEVAL;
print_val(trees);
return 0;
}

Why is the assignmnt with SOMEVAL allowed? I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?
Because C's enums are broken. Maybe you where thinking of C++ where
this is illegal?

--
Ian Collins.
Mar 10 '07 #4
At_sea_with_C wrote:
>
.... snip ...
>
Why is the assignmnt with SOMEVAL allowed? I thought an enum type
was only allowed to have values specified in the enumeration list.
Why is compiler not even warning about it? Am I wrong?
Stronger typed languages will catch it, but not C. In C an enum
just defines an integer type, with some specific named values.
This is basically the price you pay for allowing any disconnected
enum values, e.g something like 0,1,2, 10, 100.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

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

Mar 11 '07 #5
CBFalconer wrote:
At_sea_with_C wrote:

.... snip ...
>>Why is the assignmnt with SOMEVAL allowed? I thought an enum type
was only allowed to have values specified in the enumeration list.
Why is compiler not even warning about it? Am I wrong?


Stronger typed languages will catch it, but not C. In C an enum
just defines an integer type, with some specific named values.
This is basically the price you pay for allowing any disconnected
enum values, e.g something like 0,1,2, 10, 100.
I don't think the range has anything to do with it. C++ enums can have
disconnected values and still enforce valid assignments.

--
Ian Collins.
Mar 11 '07 #6
CBFalconer wrote:
At_sea_with_C wrote:
... snip ...
>Why is the assignmnt with SOMEVAL allowed? I thought an enum type
was only allowed to have values specified in the enumeration list.
Why is compiler not even warning about it? Am I wrong?

Stronger typed languages will catch it, but not C. In C an enum
just defines an integer type, with some specific named values.
This is basically the price you pay for allowing any disconnected
enum values, e.g something like 0,1,2, 10, 100.
No, it's the price you pay to be able to do

enum Flags {FOO, BAR};
enum Flags f = FOO | BAR;
enum Flags f2 = -1;

Isn't it a nice feature?

Yevgen
Mar 11 '07 #7
At_sea_with_C wrote:
Consider the following code:

#include <stdio.h>

#define SOMEVAL 1234
enum tree_types { PINE = 10, BIRCH, LARCH, OAK = 100, MAPLE, ELM, WILLOW };

void print_val(enum tree_types tree)
{
printf("tree is: %d\n", tree);
}

int main()
{
enum tree_types trees;
trees = PINE;
print_val(trees);
trees = OAK;
print_val(trees);
trees = ELM;
print_val(trees);
trees = SOMEVAL;
print_val(trees);
return 0;
}

Why is the assignmnt with SOMEVAL allowed? I thought an enum type was only
allowed to have values specified in the enumeration list. Why is compiler
not even warning about it? Am I wrong?

Thanks.
It's implementation defined whether enum objects are checked for
assignments with values other than their allowed enumeration
constants. There's no requirement for a compiler to emit a diagnostic.
I think C++ has stronger checking for enum objects, but then the point
is moot, since we're discussing C.

Mar 11 '07 #8
Ian Collins wrote:
CBFalconer wrote:
>At_sea_with_C wrote:

.... snip ...
>>Why is the assignmnt with SOMEVAL allowed? I thought an enum type
was only allowed to have values specified in the enumeration list.
Why is compiler not even warning about it? Am I wrong?

Stronger typed languages will catch it, but not C. In C an enum
just defines an integer type, with some specific named values.
This is basically the price you pay for allowing any disconnected
enum values, e.g something like 0,1,2, 10, 100.

I don't think the range has anything to do with it. C++ enums can
have disconnected values and still enforce valid assignments.
C++ doesn't care how much code it generates. Think about the run
time code needed to do the checking (it can't be done at compile
time, because the values are ints and can be the results of
expressions or function calls).

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

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

Mar 11 '07 #9
CBFalconer wrote:
Ian Collins wrote:
>>CBFalconer wrote:
>>>At_sea_with_C wrote:

.... snip ...
Why is the assignmnt with SOMEVAL allowed? I thought an enum type
was only allowed to have values specified in the enumeration list.
Why is compiler not even warning about it? Am I wrong?

Stronger typed languages will catch it, but not C. In C an enum
just defines an integer type, with some specific named values.
This is basically the price you pay for allowing any disconnected
enum values, e.g something like 0,1,2, 10, 100.

I don't think the range has anything to do with it. C++ enums can
have disconnected values and still enforce valid assignments.


C++ doesn't care how much code it generates. Think about the run
time code needed to do the checking (it can't be done at compile
time, because the values are ints and can be the results of
expressions or function calls).
Sorry, but it is. You can't assign an int to an enum variable without a
cast in C++. There isn't any runtime checking, so you can assign
bollocks values if you go out of your way to do so.

--
Ian Collins.
Mar 11 '07 #10
Ian Collins wrote:
CBFalconer wrote:
Ian Collins wrote:
>CBFalconer wrote:
At_sea_with_C wrote:

.... snip ...

Why is the assignmnt with SOMEVAL allowed? I thought an enum type
was only allowed to have values specified in the enumeration list.
Why is compiler not even warning about it? Am I wrong?

Stronger typed languages will catch it, but not C. In C an enum
just defines an integer type, with some specific named values.
This is basically the price you pay for allowing any disconnected
enum values, e.g something like 0,1,2, 10, 100.

I don't think the range has anything to do with it. C++ enums can
have disconnected values and still enforce valid assignments.
C++ doesn't care how much code it generates. Think about the run
time code needed to do the checking (it can't be done at compile
time, because the values are ints and can be the results of
expressions or function calls).
Sorry, but it is. You can't assign an int to an enum variable without a
cast in C++. There isn't any runtime checking, so you can assign
bollocks values if you go out of your way to do so.
That's true. C++ does require a cast to assign out-of-range values,
(or the wrong types?), to enums.
enum seems to be a full-fledged type in C++, whereas, in C, it appears
to simply be a method to define a list of int constants.

Mar 11 '07 #11
Ian Collins wrote:
CBFalconer wrote:
.... snip ...
>>
C++ doesn't care how much code it generates. Think about the run
time code needed to do the checking (it can't be done at compile
time, because the values are ints and can be the results of
expressions or function calls).

Sorry, but it is. You can't assign an int to an enum variable
without a cast in C++. There isn't any runtime checking, so you
can assign bollocks values if you go out of your way to do so.
We are talking about C, not C++.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

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

Mar 11 '07 #12
CBFalconer wrote:
Ian Collins wrote:
CBFalconer wrote:
... snip ...
>
C++ doesn't care how much code it generates. Think about the run
time code needed to do the checking (it can't be done at compile
time, because the values are ints and can be the results of
expressions or function calls).
Sorry, but it is. You can't assign an int to an enum variable
without a cast in C++. There isn't any runtime checking, so you
can assign bollocks values if you go out of your way to do so.

We are talking about C, not C++.
You (both) were talking about C++.

Mar 11 '07 #13
CBFalconer wrote:
Ian Collins wrote:
>>CBFalconer wrote:

.... snip ...
>>>C++ doesn't care how much code it generates. Think about the run
time code needed to do the checking (it can't be done at compile
time, because the values are ints and can be the results of
expressions or function calls).

Sorry, but it is. You can't assign an int to an enum variable
without a cast in C++. There isn't any runtime checking, so you
can assign bollocks values if you go out of your way to do so.

We are talking about C, not C++.
Have you read the first sentence in your message you quoted?

--
Ian Collins.
Mar 11 '07 #14

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

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
10
by: James Brown | last post by:
I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING }; (it goes on and on like that) This is what I would like to do: TOKEN t1 = TOK_ID; // ok...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
3
by: K. Wilder | last post by:
I need to declare a project level Enum that any procedure in any class can reference so there's continuity with the values. How do I do this? At present, if I declare a Module in a project and...
34
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...
4
by: ice8595 | last post by:
Hi there, I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated. Essentially I'm...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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,...

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.