0

The following shows that enums can take on different types depending on what is in the enumeration list. If all values are positive then the type of the enum is unsigned int. If one of the values is negative the type is signed int.

/*  stack exchange sample code for signed & unsigned enums */
#define _GNU_SOURCE 1
#include <stdio.h>
#include <assert.h>

int main ( int argc, char *argv[] ) {
  enum color_ek {red=0, org, yel, grn, blu, ind, vio} color;
  enum COLOR_ek {RED=0, ORG, YEL, GRN, BLU, IND, VIO, BLK=-100} COLOR;
   /* Signed count DOWN to red works OK. */
   printf("Counting signed COLOR down from VIO to RED: ");
  for (COLOR=VIO; RED<=COLOR ; COLOR-- ) {
      printf("%d,  ",COLOR);
      assert( RED <= COLOR && COLOR <= VIO );
  }
  printf("\nCOLOR DOWNTO RED loop DONE. On exit COLOR=%d \n",COLOR);

   /* Unsigned count DOWN to org works OK. */
   printf("Counting unsigned color down from vio to org: ");
  for (color=vio; org<=color ; color-- ) {
      printf("%d,  ",color);
      assert( org <= color && color <= vio );
  }
  printf("\ncolor DOWNTO org loop DONE. On exit color=%d \n",color);

     /* Unsigned count DOWN to  red does not work */
     printf("Counting unsigned color down from vio to red: ");
     fflush(stdout);
  for (color=vio; color>=red ; color--) {
        printf("%d,  ",color);
        fflush(stdout);
        assert(red <= color && color <= vio );
  }
  printf("color DOWNTO red loop DONE.On exit color=%d \n",color);
  return 0 ;
} /* end main */

>     /*
>     The result of the above is:
>     
>     scratchpad$ gcc enum_stackexg.c && ./a.out
>     Counting signed COLOR down from VIO to RED: 6,  5,  4,  3,  2,  1,  0,
>     COLOR DOWNTO RED loop DONE. On exit COLOR=-1
>     Counting unsigned color down from vio to org: 6,  5,  4,  3,  2,  1,
>     color DOWNTO org loop DONE. On exit color=0
>     Counting unsigned color down from vio to red: 6,  5,  4,  3,  2,  1,  0,  -1,
>         a.out: enum_stackexg.c:34: main: Assertion `red <= color && color <= vio' failed.
>     Aborted (core dumped)
>     
>     */
Greg
  • 35
  • 5
  • 1
    This code is missing `#include`s` and `main` and is intermixed with output. Could you explain what the output means? Could you shorten the code to something readable? Could you post full compilable code? Why are you using `stderr` and not `stdin`? | The only question you asked is `Is this a BUG I can file with the GCC maintainers?` - the answer is "no", – KamilCuk Dec 01 '21 at 19:27
  • 1
    Does any of the following answer your question? https://stackoverflow.com/questions/366017/what-is-the-size-of-an-enum-in-c https://stackoverflow.com/questions/40906704/define-data-type-of-enum-in-c-for-memory https://stackoverflow.com/questions/3509552/whats-the-data-type-of-c-enum-of-clang-compiler – KamilCuk Dec 01 '21 at 19:30
  • You could run a recent [GCC](http://gcc.gnu.org/) on your `greg.c` source as `gcc -Wall -Wextra -fverbose-asm -S grec.c` and look inside the generated `greg.s` assembler file. You could also write your own GCC [plugin](https://gcc.gnu.org/onlinedocs/gccint/Plugins.html) (perhaps starting from [bismon](https://github.com/bstarynk/bismon/)...), or use [Frama-C](http://frama-c.com/). Look also into [DECODER](https://decoder-project.eu/). You may want to read [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – Basile Starynkevitch Dec 02 '21 at 06:20
  • See also [RefPerSys](http://refpersys.org/), read books about generating C code (like Pitrat's [Artificial Beings, the conscience of a conscious machine](https://www.amazon.com/Artificial-Beings-Conscience-Conscious-Machine/dp/1848211015)...), and read books on programming with C. Take inspiration from existing open source C programs like [GNU make](https://www.gnu.org/software/make/). Perhaps contact me by email to `basile@starynkevitch.net`, mentioning the URL of your question – Basile Starynkevitch Dec 02 '21 at 06:23

0 Answers0