Main Menu
Home
AVR News
Atmel AVR
AVR Development Tools
Valuable Tools
WinAVR toolset
Makefile for WinAVR
AVR Virtual Simulators
Hardware for prototyping
GCC and AVR-GCC
Short introduction to C
AVR-GCC Tutorial
Example AVR Projects
AVR-GCC articles
WinAVR Site Map
Scienceprog FORUM
ScienceProg BLOG
Disclaimer
WinARM Tutorial
Latest comments
Measuring motor speed and...
Dear Administrator The code above...
More...

Programming AVR ADC module...
hi please send me example usart with...
More...

Measuring motor speed and...
main.c: In function...
More...

Running TX433 and RX433 RF...
(Hopefully this is not a re-post). I am...
More...

Running TX433 and RX433 RF...
I just bnought a pair of the RX/TX 433...
More...

Friendly sites

Related Items:

Recommended sites

Enumeration of variables PDF Print E-mail
Written by Administrator   
Thursday, 14 September 2006

This is similar to #define preprocessor where you can describe a set of constatnts. Using preprocessor we use:

#define zero 0

#define one 1

#define two 3

But there is an alternative of using enumerating using keyword enum:

enum (zero=0,one, two); //zero=0, one=1; two=2

By default enumeration assigns values from 0 and up.

Now you can use enumeration like in following example:

int n;

enum (zero=0,one, two);

n=one; //n=1

You can use enum like this:

enum escapes { BELL = '\a', BACKSPACE = '\b', HTAB = '\t',

RETURN = '\r', NEWLINE = '\n', VTAB = '\v' };

or

enum boolean { FALSE = 0, TRUE };

An advantage of enum over #define is that it has scope This means that the variable (just like any other) is only visible within the block it was declared within.

Example:


main()
{
enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
/* A A
| |
| |
| ------- list of aliases.
-------------- Enumeration tag. */

enum months month; /* define 'month' variable of type 'months' */

printf("%d\n", month=Sep); /* Assign integer value via an alias
* This will return a 9 */
}



Related Items:

   

Users' Comments  
 

Average user rating

 


Add your comment
Only registered users can comment an article. Please login or register.

No comment posted



mXcomment 1.0.6 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
< Prev   Next >

© 2008 WinAVR AVR-GCC Tutorial
Joomla! is Free Software released under the GNU/GPL License.