You can also use C Enumeration Declarations, enums
:
typedef enum {rose, red, rat} index;
Now in your code you can access arr[rat]
that is == arr[2]
.
Generally I avoid macro for this kind of constants (where many constants are of similar types). I think it should be a type with a user defined domain that is enum
.
An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration tag and defines the set of named integer identifiers (called the "enumeration set," "enumerator constants," "enumerators," or "members").
Macro I will prefer for array size:
#define SIZE 100;
int arr[SIZE]
Thanks to @junix to add very nice point in my answer:
It will be nice idea to defined enum
with to add a last element that give total number of elements, e.g.:
typedef enum { rose,
red,
rat,
/* add new elements here */
item_count
} index;
Now my enum domain range is [0, item_count]
, and we can use item_count
value for example like:
int arr[item_count];
And of-course this benefit we don't have with macros!