margarit
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
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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops ... I have been really slow in typing today lol. Sorry, evil one :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can I define it like this?
yes.you can.
Debug your code to see the behaviour,.
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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;