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);
}
Share:
509,092
Cute
Author by

Cute

Updated on April 23, 2021

Comments

  • Cute
    Cute about 3 years

    Possible Duplicates:
    How to convert a single char into an int
    Character to integer in C

    Can any body tell me how to convert a char to int?

    char c[]={'1',':','3'};
    
    int i=int(c[0]);
    
    printf("%d",i);
    

    When I try this it gives 49.

  • mmx
    mmx almost 15 years
    atoi takes a char* not a char.
  • Jennifer Miles
    Jennifer Miles almost 15 years
    Isn'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
    Paul Tomblin almost 15 years
    If you pass &c to something expecting a null terminated string, you've going to be in a world of hurt.
  • mmx
    mmx almost 15 years
    Nope, to be correct, it expects a NULL-terminated char* not just an address.
  • Tobi
    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
    Cute almost 15 years
    Not working .I expect answer as 1 but it gives a 6 digit nummber garbage i think
  • Paul Tomblin
    Paul Tomblin almost 15 years
    Sorry, I changed it to c[0] since that's what the example code uses.
  • David Sykes
    David Sykes almost 15 years
    How could unicode affect the representation of a digit in a char variable?
  • Paul Tomblin
    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
    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
    Antony Thomas almost 8 years
    strtol is a better alternative these days.
  • Risinek
    Risinek over 7 years
    Better use strtol function. atoi can return undefined behavior, if the number is out of int range.
  • some
    some over 6 years
    All 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...