Ahmed Mohamed
Ahmed Mohamed

Reputation: 413

C - Convert char to int

I know that to convert any given char to int, this code is possible [apart from atoi()]:

int i = '2' - '0';

but I never understood how it worked, what is significance of '0' and I don't seem to find any explanation on the net about that.

Thanks in advance!!

Upvotes: 1

Views: 5524

Answers (10)

Alex
Alex

Reputation: 1684

In C, a character literal has type int. [Character Literals/IBM]

In your example, the numeric value of '0' is 48, the numeric value of '2' is 50. When you do '2' - '0' you get 50 - 48 = 2. This works for ASCII numbers from 0 to 9.

See ASCII table to get a better picture.

Edit: Thanks to @ouah for correction.

Upvotes: 8

devsnd
devsnd

Reputation: 7722

When you cast a char to an int it actually maps each character to the appropriate number in the ascii table.

This means that '2' - '0' is translated to 50 - 48. So you could also find out the numeric distance of two letters in the same way, e.g. 'z' - 'a' equals 122 - 97 equals 25

You can look up the numeric representaions of each ASCII character in thsi table: http://www.asciitable.com/

Actually a char is just a unsigned byte: C just treats it differently for different operations. For example printf(97) yields 97 as output, but printf((char)97) will give you 'a' as output.

Upvotes: 2

Razvan
Razvan

Reputation: 10093

This works only because ASCII assigns codes to characters in order i.e. '2' has a character code that is with 2 bigger than the character code of '0'.

In an another encoding it wouldn't work.

Upvotes: 2

fvu
fvu

Reputation: 32953

Both terms are internally represented by the ASCII code of the number, and as numeric digits have consecutive ASCII codes subtracting them gives you the difference between the two numbers.

You can do similar tricks with characters as well, eg shift lowercase to uppercase by subtracting 32 from a lowercase character

'a' - 32 = 'A'

Upvotes: 2

ouah
ouah

Reputation: 145829

'0' to '9' are guaranteed to be sequential values in C in all character sets. This not limited to ASCII and C is not limited to the ASCII character set.

So sequential here means that '2' value is '0' + 2.

Regarding int and char note that '0' and '9' values are of type int in C and not of type char. A character literal is of type int.

Upvotes: 2

Sufian Latif
Sufian Latif

Reputation: 13356

It's all about the ASCII codes of the corresponding characters.

In C, all the digits (0 to 9) are encoded in ASCII by values 48 to 57, sequentially. So '0' actually gets value 48, and '2' has the value 50. So when you write int i = '2' - '0';, you're actually subtracting 48 from 50, and get 2.

Upvotes: 2

Femaref
Femaref

Reputation: 61427

Have a look at the ASCII table.

'0' is represented as 0x30, '9' is represented as 0x32.

This results in

0x32 - 0x30 = 2

Upvotes: 2

Luke Morgan
Luke Morgan

Reputation: 1930

All the chars in C are represented with an integer value, the ASCII code of the character. For instance '0' corresponds to 48 and '2' corresponds to 50, so '2'-'0' gets you 50-48 = 2

Link to an ASCII table: http://www.robelle.com/smugbook/ascii.html

Upvotes: 3

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43498

Any character literal enclosed in single quotes corresponds to a number that represents the ASCII code of that character. In fact, such literals evaluate not to char, but to int, so they are perfectly interchangeable with other number literals.

Within your expression, '2' is interchangeable with 50, and '0' is interchangeable with 48.

Upvotes: 2

Dan
Dan

Reputation: 1486

When you use the commas ' ' you are treating the number as a char, and if this is given to an int, the int will take the value of the ASCII code of this character.

Upvotes: 2

Related Questions