3

I am trying to understand what is going on with the code:

cout << '5' - '3';

Is what I am printing an int? Why does it automatically change them to ints when I use the subtraction operator?

MS535
  • 47
  • 4

3 Answers3

5

In C++ character literals just denote integer values.

A basic literal like '5' denotes a char integer value, which with almost all extant character encodings is 48 + 5 (because the character 0 is represented as value 48, and the C++ standard guarantees that the digit values are consecutive, although there's no such guarantee for letters).

Then, when you use them in an arithmetic expression, or even just write +'5', the char values are promoted to int. Or less imprecisely, the “usual arithmetic conversions” kick in, and convert up to the nearest type that is int or *higher that can represent all char values. This change of type affects how e.g. cout will present the value.


* Since a char is a single byte by definition, and since int can't be less than one byte, and since in practice all bits of an int are value representation bits, it's at best only in the most pedantic formal that a char can be converted up to a higher type than int. If that possibility exists in the formal, then it's pure language lawyer stuff.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    Rats. That edit made my nearly-composed answer pointless. I salute you, and your typing speed. – user4581301 Sep 26 '15 at 01:12
  • @user4581301: Thanks! :) – Cheers and hth. - Alf Sep 26 '15 at 01:19
  • This is very informative! I just was confused as I saw someone using just a char - '0'. I guess they were trying to promote that character to an int. Using that with a plus would work or would it concatenate them? – MS535 Sep 26 '15 at 01:29
  • It would do character code addition. Since C++ inherited the notion of strings-as-simple arrays from C there's very little functionality that is available for the C like literals. However, in C++14 you can write e.g. `"Blah"s` to create a `std::string` with that value. See the end of (http://en.cppreference.com/w/cpp/language/user_literal) – Cheers and hth. - Alf Sep 26 '15 at 01:33
  • Is it really true that an `int` can be smaller than a `char`? – JorenHeit Sep 26 '15 at 14:35
  • @JorenHeit: No. Where did you pick up that? 3.9.1/2: “There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list.” The problem mentioned in the answer is that for character types all bits are value representation bits, but this is not necessarily so for `int`. So equal storage does not imply equal number range. But this is a problem for language lawyers only. ;-) – Cheers and hth. - Alf Sep 26 '15 at 18:07
  • @Cheersandhth.-Alf I'm very sorry. I apparently misread your footnote multiple times ("can" rather than "can't"), so it seemed like you stated the opposite of what I thought I knew. Again, sorry and thanks for the clarification! – JorenHeit Sep 26 '15 at 19:57
1

What you're doing here is subtracting the value for ASCII character '5' from the value for ASCII character '3'. So '5' - '3' is equivalent to 53 - 51 which results in 2.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

The ASCII value of character is here

Every character in C programming is given an integer value to represent it. That integer value is known as ASCII value of that character. For example: ASCII value of 'a' is 97. For example: If you try to store character 'a' in a char type variable, ASCII value of that character is stored which is 97.

Subtracting between '5' and '3' means subtracting between their ASCII value. So, replace cout << '5' - '3'; with their ASCII value cout << 53 - 51;. Because Every character in C programming is given an integer value to represent it.

There is a subtraction operation between two integer number, so, it prints a integer 2

Animesh Kumar Paul
  • 2,241
  • 4
  • 26
  • 37