Tutorial by Examples

An enumeration is a user-defined data type consists of integral constants and each integral constant is given a name. Keyword enum is used to define enumerated data type. If you use enum instead of int or string/ char*, you increase compile-time checking and avoid errors from passing in invalid con...
There are several possibilities and conventions to name an enumeration. The first is to use a tag name just after the enum keyword. enum color { RED, GREEN, BLUE }; This enumeration must then always be used with the keyword and the tag like this: enum color chosenColor = RE...
An enumerations value in no way needs to be unique: #include <stdlib.h> /* for EXIT_SUCCESS */ #include <stdio.h> /* for printf() */ enum Dupes { Base, /* Takes 0 */ One, /* Takes Base + 1 */ Two, /* Takes One + 1 */ Negative = -1, AnotherZero /* Takes Negativ...
Enumeration types can also be declared without giving them a name: enum { buffersize = 256, }; static unsigned char buffer [buffersize] = { 0 }; This enables us to define compile time constants of type int that can as in this example be used as array length.

Page 1 of 1