c++ - subtracting characters of numbers -


i trying understand going on code:

cout << '5' - '3'; 

is printing int? why automatically change them ints when use subtraction operator?

in c++ character literals denote integer values.

a basic literal '5' denotes char integer value, extant character encodings 48 + 5 (because character 0 represented value 48, , c++ standard guarantees digit values consecutive, although there's no such guarantee letters).

then, when use them in arithmetic expression, or write +'5', char values promoted int. or less imprecisely, “usual arithmetic conversions” kick in, , convert nearest type int or *higher can represent char values. change of type affects how e.g. cout present value.


* since char single byte definition, , since int can't less 1 byte, , since in practice bits of int value representation bits, it's @ best in pedantic formal char can converted higher type int. if possibility exists in formal, it's pure language lawyer stuff.


Comments