Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can also use <a href="http://msdn.microsoft.com/en-us/library/whbyts4t%28v=vs.80%29.aspx" rel="nofollow noreferrer">C Enumeration Declarations</a>, <code>enums</code>: </p> <pre><code>typedef enum {rose, red, rat} index; </code></pre> <p>Now in your code you can access <code>arr[rat]</code> that is == <code>arr[2]</code>.</p> <p>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 <code>enum</code>. </p> <blockquote> <p>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"). </p> </blockquote> <p>Macro I will prefer for array size: </p> <pre><code>#define SIZE 100; int arr[SIZE] </code></pre> <p>Thanks to @<a href="https://stackoverflow.com/users/1863081/junix">junix</a> to add very nice point in my answer: </p> <p>It will be nice idea to defined <code>enum</code> with to add a last element that give total number of elements, e.g.: </p> <pre><code>typedef enum { rose, red, rat, /* add new elements here */ item_count } index; </code></pre> <p>Now my enum domain range is <code>[0, item_count]</code>, and we can use <code>item_count</code> value for example like: </p> <pre><code>int arr[item_count]; </code></pre> <p>And of-course this benefit we don't have with macros!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload