How do you declare an enum for four compass directions?

Enhance your programming skills with the RECF Programming Test. Features flashcards and multiple choice questions with hints and explanations. Prepare for success!

Multiple Choice

How do you declare an enum for four compass directions?

Explanation:
This checks how to create a named enumeration type in C so you can use a single type name for the values. The form that defines an anonymous enum with the four compass names and then typedefs it to Direction is the clean, conventional way: it creates a type called Direction and the constants NORTH, EAST, SOUTH, and WEST that you can use as values of that type. With this, you can write code like Direction d = NORTH; and pass those values around easily. The other patterns don’t achieve the same goal. Declaring an anonymous enum without a typedef would create a variable named Direction rather than a reusable type. A version that uses a named enum tag plus a typedef with the same identifier is valid C, but it’s more verbose and can be less clear to readers who expect a straightforward type alias. Using different names like UP, DOWN, LEFT, RIGHT doesn’t define the four compass directions as a single, named enum type; it’s just a different set of identifiers.

This checks how to create a named enumeration type in C so you can use a single type name for the values. The form that defines an anonymous enum with the four compass names and then typedefs it to Direction is the clean, conventional way: it creates a type called Direction and the constants NORTH, EAST, SOUTH, and WEST that you can use as values of that type. With this, you can write code like Direction d = NORTH; and pass those values around easily.

The other patterns don’t achieve the same goal. Declaring an anonymous enum without a typedef would create a variable named Direction rather than a reusable type. A version that uses a named enum tag plus a typedef with the same identifier is valid C, but it’s more verbose and can be less clear to readers who expect a straightforward type alias. Using different names like UP, DOWN, LEFT, RIGHT doesn’t define the four compass directions as a single, named enum type; it’s just a different set of identifiers.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy