S
S
Slavka2015-12-02 06:34:39
C++ / C#
Slavka, 2015-12-02 06:34:39

Why are enum variables needed?

Why do you need to declare an enum variable? And why is the #define construct used more often in microcontroller programming?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Eddy_Em, 2015-12-02
@Eddy_Em

And why is the #define construct used more often in microcontroller programming?

Come on! Didn't meet somehow. Usually define is used if you need to define 1-2 parameters, and when there are a bunch of them, it is more convenient to use enum, which guarantees that there will be no cases when the same value was assigned to different flags.

R
RedHairOnMyHead, 2015-12-02
@ThePyzhov

Why are enum variables needed?

For comfort.
For example, it is convenient to declare enums for colors: RED, GREEN, BLUE, etc. if you write some Tetris, in which there are tetramines of different colors.

A
abcd0x00, 2015-12-02
@abcd0x00

The main difference from symbolic constants is that enums have a different scope.

#include <stdio.h>

int main(void)
{
    enum { A, B, C };
    {
        enum { A = 10 };
        printf("%d\n", A);
    }
    printf("%d %d %d\n", A, B, C);
    return 0;
}

[[email protected] c]$ .ansi t.c -o t
[[email protected] c]$ ./t
10
0 1 2
[[email protected] c]$

And the purpose of enums is to quickly create constants with meaningful names. It's not that they're in order, it's that they're different by default. That is, within the same enumeration, you do not need to take care that something is not equal to something there.

S
Slavka, 2015-12-02
@Slavka_online

No, I understand that the enum {...}; makes life easier, but why enum varName {...}; ? what is varName then used for?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question