I've been searching the internet for some time, but didn't find a simple solution for a actually simple problem in my eyes. I guess it has been asked already:
I'm reading a value like 20.1
or XYZ
via sscanf
from a file and saving it in char *width_as_string
.
All functions should be valid in -std=c99
.
Now I want to check if the value in width_as_string
is an integer. If true, it should be saved in int width
. If false, width
should remain with the value 0
.
My approaches:
int width = 0;
if (isdigit(width_as_string)) {
width = atoi(width_as_string);
}
Alternatively, convert width_as_string
to int width
and convert it back to a string. Then compare if it is the same. But I'm not sure how to achieve that. I already tried itoa
.
Functions like isdigit
and itoa
are not valid in std=c99
, therefore I can't use them.
Thanks.