【问题标题】:error expected identifier before string constant when defining enum in c在 c 中定义枚举时,字符串常量之前的错误预期标识符
【发布时间】:2021-12-04 02:30:43
【问题描述】:

我是 C 新手,我正在尝试定义这样的枚举:

enum PrimaryColor {"red","yellow","blue"}; /* can only be set to red yello or blue */

但是当我尝试编译时,我得到了这个错误

error expected identifier before string constant

完整代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    enum PrimaryColor {"red","yellow","blue"}; /* can only be set to red yello or blue */
    /* enums use a number to represent the value under the hood and we can sepcify a number that represents the value
    ex:
    */
    enum Directions {"right","left","up","down"=100}; /* here down is reprecented by a 100 /*

    /* when you specify the number reprecenting the value the number next to it will be the number before +1
    ex:
    */


    enum Directions {"right","left"=7,"down","up"}; /* left is reprecented by 7 so the number reprecenting the value(down) next to it is reprecented by 8 */
    enum Directions {"right","left"=17,"down","up"}; /* left is reprecented by 7 so the number reprecenting the value(down) next to it is reprecented by 18 */

}


}


谢谢!

【问题讨论】:

    标签: c enums


    【解决方案1】:

    枚举不是字符串,你可以这样定义它们

    enum PrimaryColor {Red, Yellow, Blue};
    

    并像这样使用它们

    PrimaryColor.Red; //this is = 1
    

    我建议你阅读枚举:good starting place

    【讨论】:

    • 感谢从 python 迁移到 c 有点令人困惑
    猜你喜欢
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多