enum Named integer constants with enumerations

enum Named integer constants with enumerations

enum defines integer constants for constant expressions, incrementing from 0 or set values. Names share one namespace; weak typing; compatible types vary.

CategoryStruct / Union / Enum
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

enum Color { RED, GREEN = 5, BLUE };

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

RED = 0, GREEN = 5, selected = 6

📚 Related Tutorials

❓ FAQ

QIs the first enum value always 0?
AOnly an uninitialized first item defaults to 0. In enum E { A = 3, B };, A is 3 and B automatically becomes 4. Each enumeration constant can be assigned its own value.
QCan enumeration constants be negative or have duplicate values?
AYes, provided the values meet the applicable standard version's requirements for representation as int constants. For example, A = -1 and B = -1 are valid. The identifiers themselves cannot be duplicated in the same namespace.
QCan an enum variable hold only values listed in the enumeration?
AC does not impose that restriction. An enum variable can hold other representable integer values according to its compatible integer type. Compiler warnings can help, but the language does not make it a closed-set type.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏