Link to home
Start Free TrialLog in
Avatar of margarit
margaritFlag for Israel

asked on

enum in C

Hello,

I define typedef enum. Then define parameter from this type.
What would happen If i am trying to set an unillegal value to this parameter? What value would it get?
Is there any way to check if temp contains legal parameters from enum?

THANKS
Margarit
typedef enum Numbers
{
     one = 1,
     two =2,
    three = 3
} myParam;
 
void main ()
{
   temp =4;
myParam = temp;
}

Open in new window

SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of margarit

ASKER

Thanks,
Numbers - is a name of enum?
myParam - defines a type ?
Can I define it like this?
typedef enum {
     one = 1,
     two =2,
    three = 3
} myParam;
 
 
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops ... I have been really slow in typing today lol. Sorry, evil one :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can I define it like this?
yes.you can.

Debug your code to see the behaviour,.
#include <stdio.h>
#include <stdlib.h>
#define ENABLE_DEBUG //Comment out this line to see the output
#define TRUE 1
/*
Enum usually used for type check of constant inputs.It is good coding practice to use CAPITAL letters for
enum members.usually typedef not used with enum
*/
 
typedef enum Numbers
{
    ONE = 1,
    TWO =2,
    THREE = 3
} myParam;
 
int main ()
{
 
    int input;
    int temp=4;
    myParam p;
    while (TRUE)
    {
#ifdef ENABLE_DEBUG
        p=4;
#endif
 
        printf("\nvalue of p is now %d \n",p);
        printf("Enter any value:");
        scanf("%d",&input);
 
        if (input==ONE)
        {
            printf("one");
        }
        else if (input==TWO)
        {
            printf("two");
        }
        else if (input==THREE)
        {
            printf("three");
        }
        else
        {
            printf("Not valid\n");
        }
    }
    return 0;
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial