C Reference ยท Types and Literals
C char, signed char and unsigned char
Reference for the C char types: size, signed versus unsigned char, character constants, promotion to int, and safe use with the ctype.h functions.
char is the smallest integer type in C. By definition sizeof(char) is 1, and one char is one byte of the machine, which holds CHAR_BIT bits (8 on every mainstream platform). It is the type of text characters in strings, and also the type used to look at raw memory byte by byte.
C has three distinct character types: char, signed char and unsigned char. Plain char behaves like one of the other two, but which one is implementation-defined. On x86-64 Linux with GCC it is signed; on ARM Linux it is usually unsigned.
char letter = 'A'; unsigned char byte = 200; signed char delta = -5;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
char name; signed char name; unsigned char name;
| Name | Description |
|---|---|
char |
Holds a character code. Signedness is implementation-defined. A distinct type from the other two. |
signed char |
At least -127 to 127 (usually -128 to 127). |
unsigned char |
0 to UCHAR_MAX (255 when CHAR_BIT is 8). |
Related constants in <limits.h>: CHAR_BIT, CHAR_MIN, CHAR_MAX, SCHAR_MIN, SCHAR_MAX, UCHAR_MAX. A character constant such as 'A' has type int in C (it is char in C++), and escape sequences like '\n', '\0', '\x41' and '\101' are allowed. C23 adds u8'a', a UTF-8 character constant of type unsigned char.
Errors / undefined behavior: storing a value that does not fit into a signed char through assignment is implementation-defined (usually wraps). Arithmetic on char happens after promotion to int, so signed char arithmetic does not overflow before the result is stored back.
Usage notes
- Use
unsigned charfor raw bytes. It is the only type guaranteed to have no padding bits and no trap representation, and any object may be inspected through anunsigned char *without breaking aliasing rules. - Use plain
charfor text. The standard string functions takechar *. - Passing
charto<ctype.h>functions:isalpha(c)and friends need a value inunsigned charrange orEOF. With a negative plainchar(for example an accented letter in UTF-8), cast first:isalpha((unsigned char)c). getchar()returnsint, notchar, so thatEOF(usually -1) is distinguishable from a real byte. Storing the result in acharbreaks the end-of-file test on some platforms.- Promotion: in expressions a
charbecomesint. Inprintf, acharargument passed to%dor%cis therefore fine. - Not necessarily ASCII. The standard guarantees only that digits
'0'..'9'are consecutive. In practice every compiler you will meet uses ASCII or UTF-8. - UTF-8 text is a sequence of
char; one visible character may need several bytes. Use thechar8_ttype and the helpers in<uchar.h>(see uchar.h) for Unicode-aware code. - Size is 1 by definition, even on a machine where a byte is 16 bits.
Related
int,shortandlong,unsignedtypes, Strings in C, Literals,isalpha- Lessons: Characters, Data types