I want to write a function that will take a string which represents a hexadecimal number and converts it to an integer, I want to put the hexadecimal digits in an enum, but when I use an element from the enum I get an error while compiling.
Here is the code:
#include <stdio.h>
int htoi (char h[]);
enum HexDigits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F};
int main () {
enum HexDigits h = 9;
return 0;
}
int htoi (char h[]) {
}
and here is the error im getting:
C:\Users\KiKo-SaMa\Desktop\C>gcc hello.c -o hello
hello.c:4:17: error: expected identifier before numeric constant
What could be the problem with what I’m doing?
Enum members have to be named and their names (like any other name) aren’t allowed to start with a digit. If you want to use digits, you have to add something in front, e.g. a letter or an underscore (e.g.
H0or_0).Also, as an additional suggestion, it’s a lot easier to simply add your valid characters into a string and then use
strchr()for conversion:Also note that you might as well just use the hexadecimal representation of numeric constants:
0x0,0x1,…0xF.