C Syntax Collections Enum

 

- Info:

- Enum is collection of const int where each one has its alias as if it were defined using #define keyword.

- Difference is that aliases inside enum are only visible inside the block where enum was declared.

- For each member you can define int value.

- If value is not defined member will have value which is one more then the value of previous member.

- Default value of first member is 0.

- Variables defined as enum are considered as normal int, and can have ANY integer value.

- This tutorial is based on http://publications.gbdirect.co.uk/c_book/chapter6/enums.html.

 

- Example:

- This example shows basic enum operations.

- Create C Console ApplicationTest.c.

 

Test.c

#include <stdio.h>

#define A -10

#define B -9

int main() {

{

/* DECLARE ENUM SEQUENCE AND CREATE TWO INT VARIABLES. */

enum sequence{

a=-10, b, c, d=20, e, f, g=20, h

} sequence1, sequence2;

/* CREATE TWO MORE INT VARIABLES. */

enum sequence seq3, seq4;

/* DISPLAY ENUM MEMBERS. */

printf("%i", a);

printf("%i", b);

}

printf("%i", A); /* enum is not visible here any more. */

return 1;

}