Char: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
[[category:variables]][[Category:Glossary]]
'''<code>char</code>''' is the amount of memory needed to store one [[W:ASCII|ASCII]] character. On architectures that Source supports, this is one [[W:byte|byte]] or eight [[W:bit|bit]]s.
A '''character''' (or '''char''') is, programmatically, a number used to represent a single eight-bit character, or byte (multi-byte character encoding schemes such as [[UTF-8]] and [[UTF-16]] can complicate matters, so be warned). This variable is declared by the name char, and its value must be surrounded by apostrophes:


      //Either case will end compilation
<source lang=cpp>
    char anA = A;  //Wrong    Compiler Says: "Cannot convert from unknown type to char"  Error
char MyChar = 'T'; // single quote marks
    char anA = "A";  //Wrong  Compiler Says: "Cannot convert from const char[2] to char"    Error
</source>
    char anA = 'A'; //Correct
   
{{note|If you are familiar with arrays, the second error should look odd to you! "A" is a const char[2]? The reason is that "A" is actually an A followed by a null, so the computer knows where the string ends.}}


At its core, a char is really just an 8-bit [[integer]], its value ranging from 0 to 255. In fact, if you require a integer to store a very small value, a character can be used for this purpose, but it is important to remember that if you wish to use a char for this purpose that they are by definition unsigned:
{{tip|Despite its name, there is no need for char to be used to store character data. It can also be employed simply as a means of storing one byte of data.}}


    char smallInteger = 127;  //Ok!
Arrays of <code>char</code> or <code>[[wchar_t]]</code> are known as [[string]]s, and are used to store text.
    char smallInteger = -23;  //Wrong  Compiler Says: "Signed/unsigned mismatch, possible loss of data"  Warning
      //In this case smallInteger will be set as 23


However, a different keyword, ''signed'' may be applied to it in order to allow negative values to be stored, but the greatest absolute value it can hold will be cut in half.
== See also ==


    signed char smallInteger = 238; //Wrong  Compiler Says "Truncation of constant value" Warning
* [[String]]
      //In this case smallInteger will be set as (2^7)-1, the largest value possible
* <code>[[wchar_t]]</code>
    signed char smallInteger = 72;  //Ok!
    signed char smallInteger = -96; //Ok!


This is why when assigning a char an ASCII number value you must use apostrophes, so that the compiler know to convert that keyboard character into its 8-bit ASCII value.
[[category:Variables]]
 
[[Category:Glossary]]
[[String]]s are [[array]]s of, or [[pointer]]s to characters. These are used more often than characters are in the [[Source Engine]].

Revision as of 06:13, 8 December 2010

char is the amount of memory needed to store one ASCII character. On architectures that Source supports, this is one byte or eight bits.

char MyChar = 'T'; // single quote marks
Tip.pngTip:Despite its name, there is no need for char to be used to store character data. It can also be employed simply as a means of storing one byte of data.

Arrays of char or wchar_t are known as strings, and are used to store text.

See also