If i have the following :
char id[5]="1";
and I want to use the id as a integer,i have to convert it first or is enough (int)id
to use the value from that string ?
If i have the following :
char id[5]="1";
and I want to use the id as a integer,i have to convert it first or is enough (int)id
to use the value from that string ?
No... you would need to convert it using a function call such as int n = atoi( id )
A char array is just that... an array of char... it is not a number in the sense that you're thinking. You need to use a function call to convert it from the string representation of a value to the actual numerical integer value you're looking for.
C does not provide type conversions between strings and numerical data.
To convert from a string to an int, you need to use atoi
or (better) strtol
.
strtol
is better, because it does not result in undefined behaviour if the input string is not convertable to a number, and it allows you to detect cases like "42xyz"
and react correctly.