ENUM defines a list of enumerators which are declared automatically as INTEGER data type constants.
Syntax 1:ENUM Enumerator0, Enumerator1, Enumerator2, ... Parameters:
|
If the ο¬rst enumerator does not specify a value, its value is 0. Each subsequent enumerator, without a specified value, has a value defined by adding 1 to the value of the previous member of the enumeration list.
An enumerator can be assigned a value with an = specifying an integer as in the following Syntax 2.
Syntax 2:ENUM EnumeratorA = 3, EnumeratorB, EnumeratorC Parameters:
|
The program below demonstrates beginning a series with the base number 100 then
ENUM a = 100, b, c, d = 200, e, f PRINT a, b, c, d, e, f
100 101 102 200 201 202
Instead of coding :
MACRO Mon = 1 MACRO Tue = 2 MACRO Wed = 3 MACRO Thu = 4 MACRO Fri = 5 MACRO Sat = 6 MACRO Sun = 7
ENUM could be used.
ENUM Mon=1, Tue, Wed, Thu, Fri, Sat, Sun
π BCX's implementation of single line ENUM places the declarations inside the SUB or FUNCTION within which they are declared. If a single line ENUM expression is declared in a console app outside of any user defined SUB or FUNCTION then, when translated to C, the ENUM C translation is placed in, and scope-limited to, the C code main() function. Consider the following sample:
ENUM Mon=1, Tue, Wed, Thu, Fri, Sat, Sun 'declared and visible only in main() PRINT Wed CALL foo1() CALL foo2() SUB foo1 ENUM Mon=11, Tue, Wed, Thu, Fri, Sat, Sun PRINT Wed END SUB SUB foo2 ENUM Mon=21, Tue, Wed, Thu, Fri, Sat, Sun PRINT Wed END SUB
which translates to the following, relevant sections of, C code.
// ************************************************* // User's Subs and Functions // ************************************************* void foo1 () { enum Mon{Mon=11,Tue,Wed,Thu,Fri,Sat,Sun}; printf("% .15G \n",(double)Wed); } void foo2 () { enum Mon{Mon=21,Tue,Wed,Thu,Fri,Sat,Sun}; printf("% .15G \n",(double)Wed); } // ************************************************* // Main Program // ************************************************* int main(int argc, char *argv[]) { enum Mon{Mon=1,Tue,Wed,Thu,Fri,Sat,Sun}; printf("% .15G \n",(double)Wed); foo1(); foo2(); return EXIT_SUCCESS; // End of main program }
π In a console application, if a GLOBAL ENUM is needed, the above restriction regarding scope can be circumvented by using the
ENUM . . . END ENUM
block syntax which is always GLOBAL in scope as in the following Syntax 3.
Syntax 3:ENUM EnumeratorA = 3 EnumeratorB EnumeratorC END ENUM Parameters:
|
ENUM . . . END ENUMblock example.
ENUM Mon=1 Tue Wed Thu Fri Sat Sun END ENUM ' This syntax structure declares ENUM with GLOBAL scope ' and, therefore, is visible everywhere. CALL foo0() CALL foo1() CALL foo2() SUB foo0 PRINT Wed ' Declared in an ENUM block, "Wed" has GLOBAL scope END SUB SUB foo1 ENUM Mon=11, Tue, Wed, Thu, Fri, Sat, Sun PRINT Wed END SUB SUB foo2 ENUM Mon=21, Tue, Wed, Thu, Fri, Sat, Sun PRINT Wed END SUB
3 13 23
which translates to the following C code; showing relevant sections.
// ************************************************* // User's GLOBAL Enumerations // ************************************************* enum { Mon=1, Tue, Wed, Thu, Fri, Sat, Sun }; // ************************************************* // User's Prototypes // ************************************************* void foo0 (void); void foo1 (void); void foo2 (void); // ************************************************* // User's Global Initialized Arrays // ************************************************* // ************************************************* // Runtime Functions // ************************************************* // ************************************************* // User's Subs and Functions // ************************************************* void foo0 () { printf("% .15G \n",(double)Wed); } void foo1 () { enum Mon{Mon=11,Tue,Wed,Thu,Fri,Sat,Sun}; printf("% .15G \n",(double)Wed); // Declared in an ENUM block, "Wed" has GLOBAL scope } void foo2 () { enum Mon{Mon=21,Tue,Wed,Thu,Fri,Sat,Sun}; printf("% .15G \n",(double)Wed); } // ************************************************* // Main Program // ************************************************* int main(int argc, char *argv[]) { foo0(); foo1(); foo2(); return EXIT_SUCCESS; // End of main program }
β
In the above Syntax 3: example, if an ENUM single syntax
structure
ENUM Mon=1, Tue, Wed, Thu, Fri, Sat, Sun
is used instead of an ENUM block syntax structure
ENUM Mon=1 Tue Wed Thu Fri Sat Sun END ENUM
the example will not compile because the "Wed" identifier in
SUB foo0 PRINT Wed END SUB
is considered undeclared as, in the SUB, it is out of the scope of the main() function.
β