Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the data-type of C enum of Clang compiler?

Tags:

c

types

enums

clang

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?

like image 586
eonil Avatar asked Aug 18 '10 06:08

eonil


People also ask

What is the data type of enum in C?

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.

Which data type can be used with enum?

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.

What is the default type of enum?

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.

What is the syntax for enum in C?

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.


1 Answers

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.

like image 150
Carl Norum Avatar answered Nov 01 '22 21:11

Carl Norum