Skip to content
Tutorials Center
My learning

Topics

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.

C
char letter = 'A';
unsigned char byte = 200;
signed char delta = -5;

Try it

C Characters are small integers

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.

C Signed or unsigned: check your platform

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

C
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 char for raw bytes. It is the only type guaranteed to have no padding bits and no trap representation, and any object may be inspected through an unsigned char * without breaking aliasing rules.
  • Use plain char for text. The standard string functions take char *.
  • Passing char to <ctype.h> functions: isalpha(c) and friends need a value in unsigned char range or EOF. With a negative plain char (for example an accented letter in UTF-8), cast first: isalpha((unsigned char)c).
  • getchar() returns int, not char, so that EOF (usually -1) is distinguishable from a real byte. Storing the result in a char breaks the end-of-file test on some platforms.
  • Promotion: in expressions a char becomes int. In printf, a char argument passed to %d or %c is 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 the char8_t type 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.