97

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.

Community
  • 1
  • 1
Cute
  • 13,643
  • 36
  • 96
  • 112

2 Answers2

162

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.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • Not working .I expect answer as 1 but it gives a 6 digit nummber garbage i think – Cute May 15 '09 at 13:05
  • Sorry, I changed it to c[0] since that's what the example code uses. – Paul Tomblin May 15 '09 at 13:12
  • How could unicode affect the representation of a digit in a char variable? – David Sykes May 15 '09 at 14:49
  • @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. – Paul Tomblin May 15 '09 at 15:18
  • 35
    @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. – Daniel Earwicker May 27 '09 at 08:43
  • All digits in the Unicode class [Number, Decimal Digit](http://www.fileformat.info/info/unicode/category/Nd/list.htm) 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... – some Oct 14 '17 at 06:38
41

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);
}
Zorawar
  • 6,505
  • 2
  • 23
  • 41
Frans Bouma
  • 8,259
  • 1
  • 27
  • 28
  • 12
    atoi takes a char* not a char. – Mehrdad Afshari May 15 '09 at 12:51
  • 1
    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. – Frans Bouma May 15 '09 at 12:53
  • 47
    If you pass &c to something expecting a null terminated string, you've going to be in a world of hurt. – Paul Tomblin May 15 '09 at 12:54
  • 3
    Nope, to be correct, it expects a NULL-terminated char* not just an address. – Mehrdad Afshari May 15 '09 at 12:54
  • 2
    @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. – Tobi May 15 '09 at 12:59
  • strtol is a better alternative these days. – Antony Thomas Jun 04 '16 at 21:16
  • Better use [`strtol` function](http://www.cplusplus.com/reference/cstdlib/strtol/). atoi can return undefined behavior, if the number is out of int range. – Risinek Nov 10 '16 at 11:51