-4

Here is the source code:

    #include <stdio.h>

    enum coordinate_type{ RECTANGULAR = 1,POLAR };

    int main(void)
    {
        int RECTANGULAR;
        printf("%d %d\n",RECTANGULAR,POLAR);
        return 0;
    }

Why is the result the following:

3 2
petermlm
  • 930
  • 4
  • 12
  • 27
Chan
  • 3
  • 2
  • 5
    Because you declare an uninitialized variable `RECTANGULAR` which, by coincidence, contains the value `3` when you print it. It's undefined behaviour and could contain any value. You probably meant `int value = RECTANGULAR;` and then print `value` with `printf()`. – Andreas Fester Aug 31 '15 at 11:21
  • 1
    What do you think that `int RECTANGULAR; ` does? – Oliver Charlesworth Aug 31 '15 at 11:21
  • 1
    possible duplicate of [What happens to a declared, uninitialized variable in C? Does it have a value?](http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – dandan78 Aug 31 '15 at 11:25
  • 2
    You should compile with all warnings and debug info, e.g. `gcc -Wall -Wextra -g`. Then you'll get warnings, and you should improve your code till you get no warnings. – Basile Starynkevitch Aug 31 '15 at 11:26

1 Answers1

5

You are redefining what RECTANGULAR is in the main function. It gets initialized with a "random" value, in this case it is 3, but it could be anything else.

POLAR keps its value of 2 because of how the enum is defined.

Try redefining the RECTANGULAR variable in main to see different outputs.

petermlm
  • 930
  • 4
  • 12
  • 27