0

I have an enum declaration as below.

//declaring the situations can happen by next step
enum step {CANGO, CANTGO, WILLFALL, LOSEPOINT, GAINPOINT};

and I want to delcare a function which its output is of that declared enum. How I can do that?

1 Answer 1

4

Easy:

enum step myfunction();

Or if you want you could use typedef, but I wouldn't recommend it in this case:

typedef enum step step;

step myfunction();

The trick to remember is that in C, an enum type must be referred to using the enum keyword, so a plain step would not work, which is probably what you tried first.

Of course, with the typedef trick it works. You can be even lazier and write:

typedef enum step {CANGO, CANTGO, WILLFALL, LOSEPOINT, GAINPOINT} step;

But I'm on the opinion that an enum must look like an enum (a struct like an struct, and so on), and this typedef hides that.

Sign up to request clarification or add additional context in comments.

3 Comments

why don't you recommend typedef in this case?
@MelikaBarzegaranHosseini: Because you are hiding the nature of the type. If your intention is to hide that it is an enum (ie. an abstract data type) then go on, but if it is not, the typedef only obfuscates the code and confuses the reader of the code.
strong enough to change my opinion that I'd better not use that. and a good help. thanks. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.