Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is compiler dependent and may differ between enums. The following are the semantics</p> <pre><code>enum X { A, B }; // A has type int assert(sizeof(A) == sizeof(int)); // some integer type. Maybe even int. This is // implementation defined. assert(sizeof(enum X) == sizeof(some_integer_type)); </code></pre> <p>Note that "some integer type" in C99 may also include extended integer types (which the implementation, however, has to document, if it provides them). The type of the enumeration is some type that can store the value of any enumerator (<code>A</code> and <code>B</code> in this case). </p> <p>I don't think there are any penalties in using enumerations. Enumerators are integral constant expressions too (so you may use it to initialize static or file scope variables, for example), and i prefer them to macros whenever possible. </p> <p>Enumerators don't need any runtime memory. Only when you create a variable of the enumeration type, you may use runtime memory. Just think of enumerators as compile time constants. </p> <p>I would just use a type that can store the enumerator values (i should know the rough range of values before-hand), cast to it, and send it over the network. Preferably the type should be some fixed-width one, like <code>int32_t</code>, so it doesn't come to conflicts when different machines are involved. Or i would print the number, and scan it on the other side, which gets rid of some of these problems. </p> <hr> <p><strong>Response to Edit</strong></p> <p>Well, the compiler is not required to use any size. An easy thing to see is that the sign of the values matter - unsigned types can have significant performance boost in some calculations. The following is the behavior of GCC <code>4.4.0</code> on my box</p> <pre><code>int main(void) { enum X { A = 0 }; enum X a; // X compatible with "unsigned int" unsigned int *p = &amp;a; } </code></pre> <p>But if you assign a <code>-1</code>, then GCC choses to use <code>int</code> as the type that <code>X</code> is compatible with</p> <pre><code>int main(void) { enum X { A = -1 }; enum X a; // X compatible with "int" int *p = &amp;a; } </code></pre> <p>Using the option <code>--short-enums</code> of GCC, that makes it use the smallest type still fitting all the values. </p> <pre><code>int main() { enum X { A = 0 }; enum X a; // X compatible with "unsigned char" unsigned char *p = &amp;a; } </code></pre>
 

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