E
Edward Rutherford
Hello,
I have enum and I want to turn it into a string using the number I've
assigned to and concatenating a string to the end of it (or display an
error string for invalid enums):
typedef enum {
Enabled = 1,
Disabled = 2
} State;
#define State_String(x) ( \
(x == Enabled) ? #x":Enabled" : \
(x == Disabled) ? #x"
isabled" : \
#x":Unknown" \
)
int main(void) {
int i = Enabled;
printf("State: %s\n", State_String(i));
i = Disabled;
printf("State: %s\n", State_String(i));
i = Disabled + 1;
printf("State: %s\n", State_String(i));
return 0;
}
I want the output to look like
1:Enabled
2
isabled
3:Unknown
But the output is
i:Enabled
i
isabled
i:Unknown
Anybody know how I can do this in a macro?
// EPR
I have enum and I want to turn it into a string using the number I've
assigned to and concatenating a string to the end of it (or display an
error string for invalid enums):
typedef enum {
Enabled = 1,
Disabled = 2
} State;
#define State_String(x) ( \
(x == Enabled) ? #x":Enabled" : \
(x == Disabled) ? #x"
#x":Unknown" \
)
int main(void) {
int i = Enabled;
printf("State: %s\n", State_String(i));
i = Disabled;
printf("State: %s\n", State_String(i));
i = Disabled + 1;
printf("State: %s\n", State_String(i));
return 0;
}
I want the output to look like
1:Enabled
2
3:Unknown
But the output is
i:Enabled
i
i:Unknown
Anybody know how I can do this in a macro?
// EPR