How to convert char to integer in C?
509,092
Solution 1
In the old days, when we could assume that most computers used ASCII, we would just do
int i = c[0] - '0';
But in these days of Unicode, it's not a good idea. It was never a good idea if your code had to run on a non-ASCII computer.
Edit: Although it looks hackish, evidently it is guaranteed by the standard to work. Thanks @Earwicker.
Solution 2
The standard function atoi()
will likely do what you want.
A simple example using "atoi":
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int useconds = atoi(argv[1]);
usleep(useconds);
}
Author by
Cute
Updated on April 23, 2021Comments
-
Cute about 3 years
Possible Duplicates:
How to convert a single char into an int
Character to integer in CCan any body tell me how to convert a
char
toint
?char c[]={'1',':','3'}; int i=int(c[0]); printf("%d",i);
When I try this it gives 49.
-
mmx almost 15 yearsatoi takes a char* not a char.
-
Jennifer Miles almost 15 yearsIsn't that just semantics? A char* gives a char as well, just pass the address. But ok, I didn't look at the specifics, my C is starting to get kind of rusty.
-
Paul Tomblin almost 15 yearsIf you pass &c to something expecting a null terminated string, you've going to be in a world of hurt.
-
mmx almost 15 yearsNope, to be correct, it expects a NULL-terminated char* not just an address.
-
Tobi almost 15 years@Frans,Noldorin: I would consider it very dangerous to "just" pass a pointer to a char array which isn't NULL terminated to a function expecting a NULL terminated string. In the example, remove the colon from the char array and the code will read uninitialized memory.
-
Cute almost 15 yearsNot working .I expect answer as 1 but it gives a 6 digit nummber garbage i think
-
Paul Tomblin almost 15 yearsSorry, I changed it to c[0] since that's what the example code uses.
-
David Sykes almost 15 yearsHow could unicode affect the representation of a digit in a char variable?
-
Paul Tomblin almost 15 years@David, you're probably right. I was just thinking that there might be things in the Unicode character set that look like digits but aren't. But really, the problem is other character sets, since the standard does not guarantee that 0-9 are contiguous integer values.
-
Daniel Earwicker almost 15 years@Paul, actually the C standard does guarantee that. Paragraph 2.2.1 "In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous." So your answer is perfectly valid in a conforming C implementation.
-
Antony Thomas almost 8 yearsstrtol is a better alternative these days.
-
Risinek over 7 yearsBetter use
strtol
function. atoi can return undefined behavior, if the number is out of int range. -
some over 6 yearsAll digits in the Unicode class Number, Decimal Digit are continuous from 0 to 9. Example 0123456789, ۰۱۲۳۴۵۶۷۸۹, ߀߁߂߃߄߅߆߇߈߉, ०१२३४५६७८९, ০১২৩৪৫৬৭৮৯, 𝟎𝟏𝟐𝟑𝟒𝟓𝟔𝟕𝟖𝟗, 𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡, 𝟢𝟣𝟤𝟥𝟦𝟧𝟨𝟩𝟪𝟫, 𝟬𝟭𝟮𝟯𝟰𝟱𝟲𝟳𝟴𝟵, 𝟶𝟷𝟸𝟹𝟺𝟻𝟼𝟽𝟾𝟿. Even EBCDIC has the digits continuous from 0 to 9. But if you are using Baudot, then you have a few problems...