Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-27-2005, 09:04 AM   #1
Registered: Sep 2005
Posts: 145

Rep: Reputation: 15
C, enum confusion

As C tutor said : "the enumerator is of type int", so what are the differencences between int a and b:
enum a {a,b,c};
int a;
enum a b;
Old 09-27-2005, 10:22 AM   #2
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Do you understand the concept of an enum? It's just a convenient way of declaring some constants.
enum t
  a, b, c

/* is equivalent to */
const int a = 0;
const int b = 1;
const int c = 2;
Old 09-27-2005, 10:59 AM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,986

Rep: Reputation: 1444Reputation: 1444Reputation: 1444Reputation: 1444Reputation: 1444Reputation: 1444Reputation: 1444Reputation: 1444Reputation: 1444Reputation: 1444
"Well, not exactly, but close."

When you declare the enumeration-type "t," it is physically an integer having three useful values. Those values are 0, 1, and 2. But an enumeration-type allows you to give meaningful names to those "magic numbers." The value 0 corresponds to "a" and so-on.

What this does for you, the programmer, is to be descriptive. When you use an enum, you not only give "meaningful names" to "magic numbers," but you also enable the compiler to detect typographic errors. If you tried to assign the value "b" to a variable that is not an "enum t" type, the compiler will at least produce a warning. And a warning can save days of time and lots of precious hair.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
HOTSWAP: where to find #ENUM signal? iflorea Programming 0 11-20-2005 07:58 AM
C Enum exvor Programming 3 09-27-2005 03:51 PM
a little confusion..... b0nd Linux - Newbie 1 02-17-2005 07:45 AM
WM Confusion phoeniXflame Slackware 3 02-16-2003 06:19 AM
Some confusion about RH 7.3 psyklops Linux - Distributions 2 05-08-2002 04:08 PM

All times are GMT -5. The time now is 01:42 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration