0

Iam beginner in c++ , so why this happens

for Example :

            char b = 48; // cout's 0

            char b = '48'; // cout's only 8 
RaHuL
  • 101
  • 1
  • 12
  • 5
    Because you can't have a char with multiple chars in it... – Mad Physicist Oct 29 '19 at 05:24
  • As to why the first b cout's 0, it's related to the position of numerical symbols (aka digits) in the (E)ASCII table: https://www.qwant.com/?q=ascii+table&client=ext-firefox-light-sb. 48 just so happen to be where '0' is held, so cout'ing a character which value is '48' will result in the string '0' being output. – Alceste_ Oct 29 '19 at 05:26
  • @Mad Physicist do you mean '4' and '8' are 2 separate chars? – RaHuL Oct 29 '19 at 05:31
  • 3
    Because [Why should I always enable conpiler warnings?](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warning) – n. m. could be an AI Oct 29 '19 at 05:34

2 Answers2

3
char b = 48; // cout's 0

This output 0 because the character 0 has ASCII value of 48.

char b = '48'; // cout's only 8

This output 8 because you can only have one character in char type varibale.

tyb9900
  • 214
  • 2
  • 11
  • 3
    Be careful how you word that. Technically multi-character literals are completely valid in C++. They have a type of `int` but their actual value is implementation defined. I would guess in this instance the '8' is the low byte of the integer and assigning to a `char` has simply truncated the value. – Martin York Oct 29 '19 at 05:35
  • 3
    if you're on a big endian machine the it'll likely print out `4` instead of 8 – phuclv Oct 29 '19 at 05:36
0

When you assign an integer to a character type variable, the variable stores the character defined by the integer(assuming it's an ASCII code).

But when you are assigning character type data in a character type variable, it stores the last assigned character to the variable.

Masudur Rahman
  • 1,585
  • 8
  • 16
  • 1
    in C `'8'` is an int, but this question is tagged [tag:c++] and in C++ it's a `char` – phuclv Oct 29 '19 at 05:29
  • I didn't understand completely. Can you please provide some reference related to this ? – Masudur Rahman Oct 29 '19 at 06:36
  • there are a lot of questions on SO: [Size of character ('a') in C/C++](https://stackoverflow.com/q/2172943/995714), [Why are C character literals ints instead of chars?](https://stackoverflow.com/q/433895/995714)... *"But when you are assigning character type data in a character type variable, it stores the last assigned character to the variable"* is also **wrong**, since the value of multi-character literal is implementation-defined [C++ multicharacter literal](https://stackoverflow.com/q/3960954/995714) – phuclv Oct 29 '19 at 06:44
  • "stores the character defined by the integer" -- don't get hung up on "character". A value is a value, and a `char` variable holds an integral value. On output the **value** in a `char` variable is used to decide which character to **display** (and that's where ASCII or some other encoding comes in). On input each character typed on the keyboard is interpreted as an integral value (again depending on the encoding in use, which is almost always ASCII these days). – Pete Becker Oct 29 '19 at 14:10
  • @phuclv -- in `char b = '8';` it doesn't matter whether the type of `'8'` is `int` or `char`. It works exactly as expected. It's the `'48'` that's funky. – Pete Becker Oct 29 '19 at 14:13