Home > Back-end >  error expected identifier before string constant when defining enum in c
error expected identifier before string constant when defining enum in c

Time:12-04

I am new to C, I am trying define a enum like this:

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

but when i try to compile i get this error

error expected identifier before string constant

full code:

#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 */

}


}


thanks!

CodePudding user response:

Enums are not strings you can define them like this

enum PrimaryColor {Red, Yellow, Blue};

and use them like this

PrimaryColor.Red; //this is = 1

I susggest you read up on enums : good starting place

  • Related