I posted other question: What type should I use for binary representation of C enum?, and by the answer, I have to know my compiler's enum data-type.
What's the data-type of C enum on Clang compiler?
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.
The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.
enum can be displayed as a string and processed as an integer. The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
The keyword “enum” is used to declare an enumeration. Here is the syntax of enum in C language, enum enum_name{const1, const2, ....... }; The enum keyword is also used to define the variables of enum type.
Like most (all, maybe) C compilers, the size of an enumerated type can vary. Here's an example program and its output:
#include <stdio.h>
typedef enum
{
val1 = 0x12
} type1;
typedef enum
{
val2 = 0x123456789
} type2;
int main(int argc, char **argv)
{
printf("1: %zu\n2: %zu\n", sizeof(type1), sizeof(type2));
return 0;
}
Output:
1: 4
2: 8
All that the standard requires is:
The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration.
A quick web search didn't turn up a clang manual that specified its behaviour, but one is almost certainly out there somewhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With