Using atoi to compare char array with int

So I have a function that will put a received value into a char array, and then afterwards I need to check if the data in a char array is equal to a value such as 00, 01, 02 etc.

I originally tried to do this like this:

if (val == "01") {
//do thing
}

This spits out an error, and I found the easiest way to fix this was by using the atoi function to convert the char array to and int. This is my code

if (atoi(val) == 01) {
//do thing
}

This works, but I feel like it's not the proper way of doing this. Are there any issues associated with doing this? Is there a better option?

If you want to keep the char array, you can test it using strcmp.

Don't prefix integer constants with a '0'. The compiler takes that to mean you're using octal notation.

That's helpful. Once I got to values over 8, it would create problems. Thanks!

values over 7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.