- C language
- Basic grammar
- here
Character literal
A character literal is a representation of a single character in ASCII code on the source code. Enclose one character in the ASCII code with "'".
// Character literal 'a' 'b'
This is a char type and will be an ASCII code value. If you want to output as ASCII code characters, use "%c" of the printf function.
#include <stdio.h> int main (void) { char ch ='a'; // ASCII code (number) printf("%d\n", ch); // Characters corresponding to ASCII code printf("%c\n", ch); }
This is the output result.
97 a
Character literal escape sequences
Escape sequence of character literals. You can express tabs and line breaks with "\ t" and "\ n".
\ t | Tab |
---|---|
\n | Line feed (LF) |
\ 0 | null character |
This is a sample escape sequence for character literals.
#include <stdio.h> int main (void) { // Character literal escape sequences printf("%d%d%d\n",'\ t','\ n','\ 0'); }
This is the output result.
9 10 0
Any character in ASCII code
You can use the escape sequence "\ x hexadecimal ASCII code" to represent any character in the ASCII code. The following represents the ASCII code "a" in hexadecimal.
\ x61
This is a sample that uses any character of ASCII code in the escape sequence of a character literal.
#include <stdio.h> int main (void) { // Character literal escape sequences printf("%c\n",'\ x61'); }
This is the output result. a is output.
a
Escape sequence list
Symbol | Meaning |
---|---|
\ a | Bell |
\ b | Backspace |
\ f | Page feed (clear) |
\n | Line break (line break) |
\ r | Page breaks |
\ t | tab |
\ v | Vertical tab |
\\ | \ |
\? | ? |
\' | Single Quotation (') |
\ " | Double quotation (") |
\ 0 | Null character |
\ N | Octal constant |
\ xN | Hexadecimal constant |