E
E
Egorithm2016-07-02 16:38:32
C++ / C#
Egorithm, 2016-07-02 16:38:32

How to use the enum type?

I can't figure it out, I've skimmed through the whole book. Googled. I still didn't understand anything.
Here is an example:

#include <stdio.h>

void main(void) {
    enum type {A,B,C} var;
    
    printf("%d\n%d\n%d\n",A,B,C);
    printf("\n%d\n",var);
    
    enum type D,E,F;
    printf("\n%d\n%d\n%d\n",D,E,F);
    
    system("PAUSE");
}

With what is inside the block, everything is clear. But with what type and var
are (although by analogy with struct and union it is understandable, but ...)
Here is the result of the work:
46b4a607d2bf45b4be70d9ba78364388.png
So why is this needed? How to apply it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Zhilin, 2016-07-02
@EgoRusMarch

type-- is the type name of your enum. var-- the name of the variable of the newly declared type. The code can be rewritten like this:

enum type {
    A,B,C
};
enum type var;

The example is, of course, terrible. It seems that type-- is a key word, classical is better enum fruit { apple, orange };. Plus, to define variables in the type declaration -- advanced feature, I would not give it in the first example.

M
maaGames, 2016-07-02
@maaGames

In the example D,E,F are not initialized. Garbage is output to the console.

A
abcd0x00, 2016-07-03
@abcd0x00

To begin with, you need to throw out the material in which you read this:
When you find the material where it is written:
then you will find out what enum is and so on.
It’s just that if you study from shitty materials, then you can’t learn anything, because the author of the materials himself doesn’t know anything.
There is a good book, the main one in the C world
https://en.wikipedia.org/wiki/The_C_Programming_La...
250 pages where you will find everything you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question