Page 143 - C-Language
P. 143

#include "resources.h" /* To make sure clashes between declaration and definition are
                                   recognised by the compiler include the declaring header into
                                   the implementing, defining translation unit (.c file).

         /* Define the resources. Keep the promise made in resources.h. */
         const char * const resources[RESOURCE_MAX] = {
           "<unknown>",
           "OK",
           "Cancel",
           "Abort"
         };


        A program using this could look like this:

        main.c:


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

         #include "resources.h"


         int main(void)
         {
           EnumResourceID resource_id = RESOURCE_UNDEFINED;

           while ((++resource_id) < RESOURCE_MAX)
           {
             printf("resource ID: %d, resource: '%s'\n", resource_id, resources[resource_id]);
           }

           return EXIT_SUCCESS;
         }


        Compile the three file above using GCC, and link them to become the program file main for
        example using this:


         gcc -Wall -Wextra -pedantic -Wconversion -g  main.c resources.c -o main


        (use these -Wall -Wextra -pedantic -Wconversion to make the compiler really picky, so you don't
        miss anything before posting the code to SO, will say the world, or even worth deploying it into
        production)

        Run the program created:


         $ ./main


        And get:


         resource ID: 0, resource: '<unknown>'
         resource ID: 1, resource: 'OK'
         resource ID: 2, resource: 'Cancel'
         resource ID: 3, resource: 'Abort'





        https://riptutorial.com/                                                                             119
   138   139   140   141   142   143   144   145   146   147   148