How do char and int work in C++

1.2k Views Asked by At

May be I'm going to ask a stupid question, but I want to confirm that how char works? Let me explain with examples what i want to ask. Let suppose I declare a char variable and then input 6 or any integer character.

#include <iostream>
using namespace std;
int main(){
    char a;
    cin >> a;
    cout << a*a; // I know here input's ASCII value will multiply
    return 0;
}

Same as for integer input 6

#include <iostream>
using namespace std;
int main(){
    int a;
    cin >> a;
    cout << a*a; // Why compiler not take input's ASCII Value here?
    return 0;
}

I think now my question is clear.

4

There are 4 best solutions below

3
On BEST ANSWER

char is a fundamental data type, of size 1 byte (not necessarily 8bits!!!), capable of representing at least the ASCII code range of all characters. So, for example, char x = 'a'; really stores the ASCII value of 'a', in this case 97. However, the ostream operators are overloaded so they "know" how to deal with char, and instead of blindly displaying 97 in a line like cout << x; they display the ASCII representation of the character, i.e. 'a'.

However, whenever you do a * a, the compiler performs what's called integral promotion, and implicitly converts the data in the char to an int. So, for the compiler, the expression a * a is translated into (int)a * (int)a, which is the result of 97 * 97, so you end up displaying 9409.

Note that for the compiler char and int are two different types. It can differentiate between the two. So operator<< displays a character only when the input is of type char or of a type implicitly convertible to char.

1
On

In the case of char, it's not the binary which is being multiplied, it's the ASCII value of whatever you typed in. In the case of 6, the ASCII value is 54, so 2916 is output. When a is an int, 6 is stored directly, so 36 is output.

0
On

In the case of int it reads the number you type in as a numeric value instead of its ASCII value. Therefore you can ONLY use numbers for integers however char allows you to use any character (Ex. A,B,C,D | 1,2,3,4 | !#%^) and it uses its ASCII value. The ASCII value of 4 is 52 for example, and the ASCII value of A, * are 65, and 42 respectively. NOTE: Capital Letters are different in comparison to lower case letters (Ex. S = 83, while conversely s = 115).

Hopefully this helped even a little bit.

0
On

This is because of implicit type conversion:

Implicit conversions are performed whenever an expression of some type T1 is used in context that does not accept that type, but accepts some other type T2, in particular:

When the expression is used as an operand with an operator that expects T2

In your example

char a;
cin >> a;
cout << a*a; /

Operator * expects an integral type , so type char is implicitly converted to int, so while converting char to int, system use corresponding ASCII value of char. Please see more details here http://en.cppreference.com/w/cpp/language/implicit_cast