How to increment an Enum in C?

Asked

Viewed 241 times

2

Objectively as I do to increment ++ or += 1 one of the variables of enum (the increment will be in a loop / switch):

enum {um = 0, dois = 0 ... seis = 0};

I read that one question and I didn’t get it right. Some better structure hint to store the data (which are six strings their respective values)?

1 answer

3


There is no secret and that is what that code shows. The enumeration is created. Declares a variable of the enumeration type, assigns one of the possible values to the variables, and can already use an enumeration constant, even if it may be any value, there is no use check, and then makes the increment. The variable will be a normal integer.

#include <stdio.h>

typedef enum {um = 1, dois = 2, tres = 3} Numeros;

int main(void) {
    Numeros numero = um;
    numero++;
    printf("%d", numero);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

This is the question you asked and it is answered. But the description also indicates that you do not want it. The first thing you need to define is whether you need to string even, or just needs a set of variables that hold some values and that those values should be incremented, then just use a array of int very simple, which is a much more basic resource of C. and as already programmed in C before should know how to do.

There is a question that shows the practical use of this.

  • In that case the name is required, vlw.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.