LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 09-27-2005, 08:04 AM   #1
cigarstub
Member
 
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, 09:22 AM   #2
addy86
Member
 
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.
Code:
enum t
{
  a, b, c
};

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

Rep: Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952
"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.
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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


All times are GMT -5. The time now is 02:32 AM.

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