Author Topic: Just learned about enum in C  (Read 1499 times)

0 Members and 1 Guest are viewing this topic.

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Just learned about enum in C
« on: October 23, 2012, 05:14:07 am »
As the title said, I just learned about enum (and typedef) in C... and it's pretty freaking cool ;)

take this for example:
Code: (c) [Select]
#include <stdio.h>
#include <stdlib.h>

typedef enum
{
   
    summer,fall,winter,spring
   
} season;

void printPhrase(season s);

int main(void)
{
    printPhrase(summer);
    return 0;
}

void printPhrase(season s)
{
    if (s == summer)
        printf("It's hot outside\n");
    else if (s == fall)
        printf("It's getting cooler outside\n");
    else if(s == winter)
        printf("It's fn cold out\n");
    else if(s == spring)
        printf("it's getting warmer\n");
    else
        printf("I have no idea what you're talking about ;)");
}

Now correct me if I'm wrong, but this is my understanding of the code above.  Create a variable type named season that is an enum.  Inside that variable type season, you can have 4 options (similar to a const char can only be one letter/symbol) summer, fall, winter and spring.

Then there is the prototype for the function printPhrase which takes a variable of the type "season" which we just defined above.

Skipping down to the actual printPhrase function it takes the argument and stores it into "s".  Next it compares it to the contents that "season" has (summer, winter, fall, spring).  If it equals one of the "seasons" or it is a valid season, then printf a phrase.

As I said before, I just learned this and I might be lacking in some areas so if you see a fault in my explanation, please clarify ;)

One thing I'm kind of shaky on is the contents of the enum... there is no ";" after it, those uninitialized "variables" if that is what you'd call them, look so naked in this explicit C language.
« Last Edit: October 23, 2012, 01:36:13 pm by Polyphony »
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Just learned about enum in C
« Reply #1 on: October 23, 2012, 01:20:05 pm »
Good explanation about the usefulness of enum variables, but in your if statement you don't have an else at the end. What if you don't provide a valid variable to printPhase? Nothing will happen. So say like else printf("Season only exist in another dimension"). It's just looks so unclean seeing an else if without ending else :P Also int main(void) :P

Lol again, some people write code and the commenters comment on the design/technicalities of the code and not on the point the writer wanted to make. But I guess this is good because it makes people code better on a semiconscious level. And if there's nothing extra to say about the point, then I guess sharing your experience to help in advancement is a good thing.
Easter egg in all *nix systems: E(){ E|E& };E

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Just learned about enum in C
« Reply #2 on: October 23, 2012, 03:20:47 pm »
You don't actually need to include 'stdlib.h' for that particular piece of code.  I invite you to experiment a little bit with your new knowledge of enum and produce a small program that will output the name of every card in a standard deck of playing cards.  Don't forget to handle special card names like 'ace', 'deuce', 'jack' and 'joker'.

Good luck && have fun!
-Xires

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: Just learned about enum in C
« Reply #3 on: October 24, 2012, 01:03:10 am »

Challenge Accepted ;)
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple