2

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.

Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80

4 Answers4

4

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.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • I know this process already. But how compiler handle char and int? Does compiler store both in different place? I'm really confused between char and int process.. Sorry – Asif Mushtaq Jun 04 '15 at 15:12
  • I got useful line from your integra promotion link.. **that is In particular, arithmetic operators do not accept types smaller than int as arguments** I think it's clearing now that arithmetic operators only work with data types those are greater than or equal to int. Compiler automatically use ASCII Value of data types those smaller than int? – Asif Mushtaq Jun 04 '15 at 15:21
  • @AsifMushtaq For example, on my machine `short` is 2 bytes, less than an `int`, which is 4. However `cout << (short)x;` displays the ASCII code, not the character. Whereas `cout << (uint8_t)x;` displays the character corresponding to the ASCII code, as `uint8_t` is convertible to `char`. – vsoftco Jun 04 '15 at 22:12
1

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.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
0

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.

Blank
  • 16
  • 3
0

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

Steephen
  • 14,645
  • 7
  • 40
  • 47