Page 155 - C-Language
P. 155

{
                 printf("BLUE\n");
             }
         }


        For more on typedef see Typedef


        Enumeration with duplicate value


        An enumerations value in no way needs to be unique:


         #include <stdlib.h> /* for EXIT_SUCCESS */
         #include <stdio.h> /* for printf() */


         enum Dupes
         {
            Base, /* Takes 0 */
            One, /* Takes Base + 1 */
            Two, /* Takes One + 1 */
            Negative = -1,
            AnotherZero /* Takes Negative + 1 == 0, sigh */
         };

         int main(void)
         {
           printf("Base = %d\n", Base);
           printf("One = %d\n", One);
           printf("Two = %d\n", Two);
           printf("Negative = %d\n", Negative);
           printf("AnotherZero = %d\n", AnotherZero);

           return EXIT_SUCCESS;
         }


        The sample prints:


         Base = 0
         One = 1
         Two = 2
         Negative = -1
         AnotherZero = 0


        enumeration constant without typename


        Enumeration types can also be declared without giving them a name:


           enum { buffersize = 256, };
           static unsigned char buffer [buffersize] = { 0 };


        This enables us to define compile time constants of type int that can as in this example be used
        as array length.

        Read Enumerations online: https://riptutorial.com/c/topic/5460/enumerations



        https://riptutorial.com/                                                                             131
   150   151   152   153   154   155   156   157   158   159   160