Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Char Data Type (Visual Basic)
In this articleHolds unsigned 16-bit (2-byte) code points ranging in value from 0 through 65535. Each code point, or character code, represents a single Unicode character. RemarksUse the The default value of Unicode CharactersThe first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. These first 128 code points are the same as those the ASCII character set defines. The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions. Unicode uses the remaining code points (256-65535) for a wide variety of symbols, including worldwide textual characters, diacritics, and mathematical and technical symbols. You can use methods like IsDigit and IsPunctuation on a Type ConversionsVisual Basic does not convert directly between If the type checking switch (the
Option Strict Statement) is on, you must append the literal type character to a single-character string literal to identify it as the
Programming Tips
See also
FeedbackSubmit and view feedback for To this point, the fundamental data types we’ve looked at have been used to hold numbers (integers and floating points) or true/false values (Booleans). But what if we want to store letters or punctuation?
The char data type was designed to hold a single The char data type is an integral type, meaning the underlying value is stored as an
integer. Similar to how a Boolean value ASCII stands for American Standard Code for Information Interchange, and it defines a particular way to represent English characters (plus a few other symbols) as numbers between 0 and 127 (called an ASCII code or code point). For example, ASCII code 97 is interpreted as the character ‘a’. Character literals are always placed between single quotes (e.g. ‘g’, ‘1’, ‘ ‘). Here’s a full table of ASCII characters:
Codes 0-31 are called the unprintable chars, and they’re mostly used to do formatting and control printers. Most of these are obsolete now. If you try to print these chars, the results are dependent upon your OS (you may get some emoji-like characters). Codes 32-127 are called the printable characters, and they represent the letters, number characters, and punctuation that most computers use to display basic English text. Initializing chars You can initialize char variables using character literals:
You can initialize chars with integers as well, but this should be avoided if possible
Warning Be careful not to mix up character numbers with integer numbers. The following two initializations are not the same:
Character numbers are intended to be used when we want to represent numbers as text, rather than as numbers to apply mathematical operations to. Printing chars When using std::cout to print a char, std::cout outputs the char variable as an ASCII character:
This produces the result: ab We can also output char literals directly:
This produces the result: c Inputting chars The following program asks the user to input a character, then prints out the character:
Here’s the output from one run: Input a keyboard character: q You entered q Note that std::cin will let you enter multiple characters. However, variable ch can only hold 1 character. Consequently, only the first input character is extracted into variable ch. The rest of the user input is left in the input buffer that std::cin uses, and can be extracted with subsequent calls to std::cin. You can see this behavior in the following example:
Input a keyboard character: abcd You entered: a You entered: b If you want to read in more than one char at a time (e.g. to read in a name, word, or sentence), you’ll want to use a string instead of a char. A string is a collection of sequential characters (and thus, a string can hold multiple symbols). Char size, range, and default sign Char is defined by C++ to always be 1 byte in size. By default, a char may be signed or unsigned (though it’s usually signed). If you’re using chars to hold ASCII characters, you don’t need to specify a sign (since both signed and unsigned chars can hold values between 0 and 127). If you’re using a char to hold small integers (something you should not do unless you’re explicitly optimizing for space), you should always specify whether it is signed or unsigned. A signed char can hold a number between -128 and 127. An unsigned char can hold a number between 0 and 255. Escape sequences There are some characters in C++ that have special meaning. These characters are called escape sequences. An escape sequence starts with a ‘\’ (backslash) character, and then a following letter or number. You’ve already seen the most common escape
sequence:
This outputs: First line Second line Another commonly used escape sequence is
Which outputs: First part Second part Three other notable escape sequences are: Here’s a table of all of the escape sequences:
Here are some examples:
Prints: "This is quoted text" This string contains a single backslash \ 6F in hex is char 'o' Warning Escape sequences start with a backslash (\), not a forward slash (/). If you use a forward slash by accident, it may still compile, but will not yield the desired result. Newline (\n) vs. std::endl We cover this topic in lesson 1.5 -- Introduction to iostream: cout, cin, and endl. What’s the difference between putting symbols in single and double quotes? Single chars are always put in single quotes (e.g. Text put between double quotes (e.g. “Hello, world!”) is treated as a string. For now, you’re welcome to use string literals in your code:
Best practice Put stand-alone chars in single
quotes (e.g. Avoid multicharacter literals For backwards compatibility reasons, many C++ compilers support multicharacter literals, which are char literals that contain multiple characters (e.g. Best practice Avoid multicharacter literals (e.g. Multicharacter literal support sometimes causes problems for new programmers. Consider the following simple program:
The programmer expects this program to print the value 312142 The issue here is that the programmer accidentally
used Warning Make sure that your newlines are using escape sequence What about the other char types, wchar_t, char16_t, and char32_t? wchar_t should be avoided in almost all cases (except when interfacing with the Windows API). Its size is implementation defined, and is not reliable. It has largely been deprecated. As an aside… The term “deprecated” means “still supported, but no longer recommended for use, because it has been replaced by something better or is no longer considered safe”. Much like ASCII maps the integers 0-127 to American English characters, other character encoding standards exist to map integers (of varying sizes) to characters in other languages. The most well-known mapping outside of ASCII is the Unicode standard, which maps over 144,000 integers to characters in many different languages. Because Unicode contains so many code points, a single Unicode code point needs 32-bits to represent a character (called UTF-32). However, Unicode characters can also be encoded using multiple 16-bit or 8-bit characters (called UTF-16 and UTF-8 respectively). char16_t and char32_t were added to C++11 to provide explicit support for 16-bit and 32-bit Unicode characters. char8_t has been added in C++20. You won’t need to use char8_t, char16_t, or char32_t unless you’re planning on making your program Unicode compatible. Unicode and localization are generally outside the scope of these tutorials, so we won’t cover it further. In the meantime, you should only use ASCII characters when working with characters (and strings). Using characters from other character sets may cause your characters to display incorrectly. What data type can be used to hold any single character including numbers?One of the most widely used data types is a string. A string consists of one or more characters, which can include letters, numbers, and other types of characters.
What uses curly braces containing a number that indicates the desired position for a variable in a list that follows a string used for formatting purposes?Chapter 02 Key Terms Review. How is a boolean variable declared in a program?Boolean variables are variables that can have only two possible values: true, and false. To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false.
What operator is used for exponentials in the C# programming language?Description. The caret (^) is used as the exponentiation operator. Note: The exponent operator should not be confused with the base-10 exponent symbol.
|