Why does subtracting '0' in C result in the number that the char is representing?
Because the char are all represented by a number and '0' is the first of them all.
On the table below you see that:
'0' => 48
'1' => 49
'9' => 57.
As a result: ('9'
- '0'
) = (57 − 48) = 9
Source: http://www.asciitable.com
What does [c - '0'] mean in array?
Every character has a numeric value; that means if you input the character "0" you're not actually inputting the number "0". You're inputting the character "0" which its value is 48 in the ASCII table (it may be different in other character sets...)
so when you input the character "0" you get the actual number zero in the following (According to the ASCII table):
c - '0' means c - 48
'0' - '0' means 48 - 48 = 0
'1' - '0' means 49 - 48 = 1
'2' - '0' means 50 - 48 = 2
and so on...
notice that the code is designed to work with different character sets. not just ASCII.
Search "ASCII table" in google to see the chart in order to understand it better.
How does subtracting the character '0' from a char change it into an int?
The value of a char
can be 0-255, where the different characters are mapped to one of these values. The numeric digits are also stored in order '0'
through '9'
, but they're also not typically stored as the first ten char
values. That is, the character '0'
doesn't have an ASCII value of 0
. The char value of 0
is almost always the \0
null character.
Without knowing anything else about ASCII, it's pretty straightforward how subtracting a '0'
character from any other numeric character will result in the char value of the original character.
So, it's simple math:
'0' - '0' = 0 // Char value of character 0 minus char value of character 0
// In ASCII, that is equivalent to this:
48 - 48 = 0 // '0' has a value of 48 on ASCII chart
So, similarly, I can do integer math with any of the char
numberics...
(('3' - '0') + ('5' - '0') - ('2' - '0')) + '0') = '6'
The difference between 3
, 5
, or 2
and 0
on the ASCII chart is exactly equal to the face value we typically think of when we see that numeric digit. Subtracting the char '0'
from each, adding them together, and then adding a '0'
back at the end will give us the char value that represent the char that would be the result of doing that simple math.
The code snippet above emulates 3 + 5 - 2
, but in ASCII, it's actually doing this:
((51 - 48) + (53 - 48) - (50 - 48)) + 48) = 54
Because on the ASCII chart:
0 = 48
2 = 50
3 = 51
5 = 53
6 = 54
C++- Adding or subtracting '0' from a value
The C and C++ standards require that the characters '0'..'9'
be contiguous and increasing. So to convert one of those characters to the digit that it represents you subtract '0'
and to convert a digit to the character that represents it you add '0'
.
What does subtracting by char 0 do?
It's subtracting unicode code points. So if c
was something like 9
the operation is '9' - '0'
. The code point for 9
is 57
and 0
is 48
, so it will perform 57 - 48
which results in 9
.
The only reason I can think of doing this over an int.Parse
would be speed - this is an arithmetic operation which is much faster than whatever int.Parse
does.
I don't understand the process converting string to int
In c++ characters can be implicitly cast to integers using their ASCII codes. I don't really want to spoil the fun of solving the given problem so i'll just provide a hint here:
Given a single digit number '2' and '4' with an ASCII code of 50 and 52 (decimal) respectively, subtracting '0' with an ASCII code of 48 from both numbers, you get the actual numerical values of the characters (50-48 = 2) and so on.
Have fun coding!
Char Subtraction in c++
According to the C++ Standard (2.3 Character sets)
- ...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 the codes of adjacent digits in any character set differ by 1.
Thus in this code snippet
char x='2';
x-='0';
if(x) cout << x << endl;
the difference between '2'
and '0'
(the difference between codes that represent these characters; for example in ASCII these codes are 0x32 and 0x30 while in EBCDIC they are 0xF2 and 0xF0 correspondingly) is equal to 2
.
You can check this for example the following way
if(x) cout << ( int )x << endl;
or
if(x) cout << static_cast<int>( x ) << endl;
If you just write
if(x) cout << x << endl;
then the operator << tries to output x
as a printable character image of the value 2
because x
is of type char
.
Related Topics
Rand() Returns Same Values When Called Within a Single Function
C++ Lambda With Captures as a Function Pointer
Difference Between Files Written in Binary and Text Mode
What's the Best Free C++ Profiler For Windows
Generate Random Numbers Using C++11 Random Library
Dynamically Allocating an Array of Objects
Should I Use Static_Cast or Reinterpret_Cast When Casting a Void* to Whatever
What Are the Mechanics of Short String Optimization in Libc++
How to Create a Dynamic Array of Integers
In C++ Books, Array Bound Must Be Constant Expression, But Why the Following Code Works
Should I Prefer Pointers or References in Member Data
Is It Better to Use Std::Memcpy() or Std::Copy() in Terms to Performance
How to Use Enums as Flags in C++
When and Why Do I Need to Use Cin.Ignore() in C++