char and wchar_t Type Specifiers

The char specifier has the following syntax:

>>-+----------+--char------------------------------------------><
   +-unsigned-+
   '-signed---'
 
 

The char specifier is an integral type.

A char has enough storage to represent a character from the basic character set. The amount of storage allocated for a char is implementation-dependent.

You initialize a variable of type char with a character literal (consisting of one character) or with an expression that evaluates to an integer.

Use signed char or unsigned char to declare numeric variables that occupy a single byte.

C++ For the purposes of distinguishing overloaded functions, a C++ char is a distinct type from signed char and unsigned char.

Examples of the char Type Specifier

The following example defines the identifier end_of_string as a constant object of type char having the initial value \0 (the null character):

const char end_of_string = '\0';

The following example defines the unsigned char variable switches as having the initial value 3:

unsigned char switches = 3;

The following example defines string_pointer as a pointer to a character:

char *string_pointer;

The following example defines name as a pointer to a character. After initialization, name points to the first letter in the character string "Johnny":

char *name = "Johnny";

The following example defines a one-dimensional array of pointers to characters. The array has three elements. Initially they are a pointer to the string "Venus", a pointer to "Jupiter", and a pointer to "Saturn":

static char *planets[ ] = { "Venus", "Jupiter", "Saturn" };

The wchar_t Type Specifier

The wchar_t type specifier is an integral type that has enough storage to represent a wide character literal. (A wide character literal is a character literal that is prefixed with the letter L, for example L'x')

Related References

IBM Copyright 2003