Char: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Different character encoding schemes may use more than one char per character...)
No edit summary
 
(15 intermediate revisions by 11 users not shown)
Line 1: Line 1:
[[category:variables]]
{{lang|Char}}
A '''character''' 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:
'''<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.


      //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]?
See [[string]]s for more info!


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 [[integer | 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. <code>char*</code> means a [[pointer]] to a char, which is almost always the start of a string.
    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.
In {{goldsrc|2}} and {{source|2}}, unsigned chars (0-255) are typedefed as {{codelink|byte}}.


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

Latest revision as of 08:08, 3 May 2025

English (en)Deutsch (de)Русский (ru)Translate (Translate)

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. char* means a pointer to a char, which is almost always the start of a string.

In GoldSrc GoldSrc and Source Source, unsigned chars (0-255) are typedefed as byte.

See also