C language for MSX
Column by Nicola Brogelli (Part 3)
Enumerations
The enum or enumerations are particular data types that contain a list of constants, each of which is associated with an integer value. Specifically, the constant values contained in the enumeration can be defined by the user or if there are no particular needs we can leave the association of the value to the constant to the language.
Below I report the syntax of an enumeration
enum enumeration_name {constant1, constant2, …};
The enum keyword is necessary to define the enumeration type “structure”, enumeration_name is the associated name, while the constants are shown inside the curly brackets. By default the integer values associated with the constants have a base of 0, therefore the first constant will have a value of 0, the second 1 etc.
enum week {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
However, it is possible to associate custom values with constants and this is done using the following syntax:
enum seventh {Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6, Sun = 7};
Alternatively, it is possible to initialize only the first constant of the enumeration, thus doing all the other constants will have progressive values increased by one unit compared to the first constant.
enum seventh {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};
For those interested theMSX Italy Association created a R&D working group on C programming for MSX. For more info:associazioneMSXitalia@gmail.com




0 Comments