C0 | Documentation
About Downloads Tutorial References Courses
C0 Tutorial C0 and other languages Other references

Characters

Characters are a type to represent components of strings. C0 uses the ancient ASCII encoding of characters, which means there are 128 different character codes (0 through 127). We can mention characters directly in the program, using the syntax c, where c is a printable ASCII character or a so-called escape sequence. The usual printable ASCII characters, in the lexicographic order, are

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

where the first character is a space. The only escape sequences you will ordinarily use are \n (for newline), \" (to include a doublequote in a string), and perhaps \t for a horizontal tab to align output. Even though in the program text these look like two characters, they actually just represent a single character internally. The complete list of escape sequences is shown at the end of this page.

The character \0 (NUL) has a somewhat special status with respect to strings. It cannot be used inside strings because it is considered and end-of-string character. This is a C legacy we preserved because it does not affect typical C0 programs. For further discussion, see strings.

Characters can be compared directly for equality (==, !=) and inequality (<, <=, >=, >), using their lexicographic ordering, which is the same as the numeric ordering using their ASCII code.

Library functions

We can convert between ASCII codes and integers using the pair of functions

int char_ord(char c)
  //@ensures 0 <= \result && \result < 128;
  ;
char char_chr(int n)
  //@requires 0 <= n && n < 128;
  ;

Escape sequences

esc seq char code hex value meaning
\0 NUL 0x00 end-of-string
\a BEL 0x07 bell (alert)
\b BS 0x08 backspace
\t TAB 0x09 horizontal tab
\n LF 0x0A line feed (newline)
\v VT 0x0B vertical tab
\f FF 0x0C form feed (newpage)
\r CR 0x0D return
\' ' 0x27 single quote
\" " 0x22 double quote
\\ \ 0x5D backslash