char datatype

char data type is a non-numeric data type. Then why signed and unsigned modifiers are there in char data type?
char is a numeric type.
@hellos,
how?
int data type is numeric data type then how char is a numeric data type?
C++ has many numeric types.
* char
* short
* int
* long
* long long
* wchar_t
* char16_t
* char32_t
* size_t
* intptr_t
@hellos,
can i give the integer value to char data type as follows

//code 1
char j=1;
cout<<j;

is the above code 1 is right?

//code 2
char j=1.5;
cout<<j;

is the above code 2 is right?

//code 3
char k=a; //where a is the alphabet a
cout<<k;

is the above code 3 right?
since char is numeric data type,how it can take alphabet a
I am not a compiler. If you want to see if the code is right, try compiling and running it yourself.

since char is numeric data type,how it can take alphabet a
Characters are stored by computers by being mapped to integers.
Last edited on
Each character is represented by a number. When you print a char it will print it as a character. It will not print the number.

In code 1 you might see some character, or you might not see anything at all because 1 might not map to any printable character.

Code 2 will do the exact same as code 1 because char is an integer type so the decimal part will be discarded.

In code 3 you need to put single quotes around the character 'a'. This will write the number that represents the character a to k. Assuming some ASCII compatible encoding is used the number is 97.

If you want to print the number stored by a char you can do that by casting the value to an int before printing.
1
2
char c = 'a'; 
cout << static_cast<int>(c) << endl;
Topic archived. No new replies allowed.