if i initialize x=65. it can show A (ascii code 65 is A). but 167 cannot.. why?

#include <iostream>
using namespace std;

      char x=167;
      return EXIT_SUCCESS;
8 Years
Discussion Span
Last Post by Ancient Dragon

i mean if initialize char x=65, cout<<x; will show 'A'
but if i initialize char x=167, cout<<x; it doesnt show any thing...


when you write a program, you express C source files as text lines containing characters from the source character set. when a program executes in the target environment, it uses characters from the target character set. these character sets are related, but need not have the same encoding.

this is all that you can assume:
every character set must contain a distinct code value for each distinct character in the basic C character set. the code value zero is reserved for the null character (and is always in the character set). code values for the digits are contiguous, with increasing values for characters from '0' to '9'. code values for any two letters (eg. 'a' and 'b') are not necessarily contiguous. code values for the basic C character set are positive when stored in an object of type char.

you can change locales and, therefore, target character sets while the program is running (by calling the function setlocale). the translator encodes character constants and string literals for the default "C" locale ( 'classic' locale in C++ ) which is the locale in effect at program startup.

> if i initialize x=65. it can show A
this is not guaranteed in any way. 65 may not be a code value in the target character set at all, or it may be the code value for some other character. you just happen to be running in a target environment where code value 65 maps to an 'A'.

> but 167 cannot.. why?
167 may be larger than std::numeric_limits<char>::max() on the target environment (eg. i386),
or even if it is a positive value that can be held in a char, 167 may not be a code value for any character in the target character set.

if you want a character to be displayed as an 'A' in the target environment, initialize it this way: char x = 'A' ; . and use the default locale while displaying it.


If you are using MS-DOS or MS-Windows you can draw lines draw extended ascii codes See the chart for other line-drawing characters. The other characters will not appear on your screen or show up as small circles/squares because most fonts don't use them for anything.

Simple example:

int main()
    char array[80];
    array[79] = 0;
    cout << array << "\n";
    return 0;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.