Learn the char type: 'A' versus "A", ASCII codes, integer representation, character input with scanf, and leftover newlines.
charis one character stored in a one-byte box 'A'(single quotes) printf("%c", c)for output / scanf("%c", &c)for input %c twice: discard the newline first c + ('a'-'A')ch - '0'int for integers and double for real numbers. Now meet char, named after character. A char variable stores one character value and occupies one byte. The syntax is simple:char c = 'A'; // Store the character A in c printf("%c\n", c); // → A (display A on screen)
'A'%c(c stands for character) 'A'(single quotes) and "A"(double quotes) are different 'A'— single quotes char c = 'A';
"A"— double quotes \0)char s[] = "A";
"A" a string is an array of char values followed by \0 as its terminator . 'A' because it interprets the number 65 as a character . char c = 'A';and char c = 65;is mean the same thing printf("%c", 65) → A / printf("%d", 'A') → 65'A' + 1 to become 'B' through character arithmetic | Character | Decimal | Hexadecimal | Description |
|---|---|---|---|
| \0 | 0 | 0x00 | Null character (string terminator) |
| \t | 9 | 0x09 | Horizontal tab |
| \n | 10 | 0x0A | Newline |
| ␣ | 32 | 0x20 | Space |
| Character | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Decimal | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 |
| Character | A | B | C | D | E | F | G | H | I | J | K | L | M |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Decimal | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 |
| Character | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
| Decimal | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 |
| Character | a | b | c | d | e | f | g | h | i | j | k | l | m |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Decimal | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 |
| Character | n | o | p | q | r | s | t | u | v | w | x | y | z |
| Decimal | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 |
'a' - 'A' = 32 holds for these ASCII letters. This offset enables case conversion. #include <stdio.h> int main(void) { char c = 'A'; printf("c = %c\n", c); // → A (display as a character) printf("c = %d\n", c); // → 65 (display as an integer) // Character arithmetic also works printf("%c\n", c + 1); // → B (65+1=66, the code for 'B') printf("%c\n", 'Z' - 3); // → W // Storing 65 in a char is equivalent to storing 'A' char d = 65; printf("%c\n", d); // → A return 0; }
char c = 'A'; char lower = c + ('a' - 'A'); // 'A' + 32 = 'a' // Or: char lower = c + 32;
char ch = '7'; // This is 55 (ASCII code for '7') int n = ch - '0'; // 55 - 48 = 7 (the integer seven) printf("%d\n", n); // → 7
if (c >= 'A' && c <= 'Z') // Uppercase? if (c >= '0' && c <= '9') // Digit? // <ctype.h> also provides isupper(c) and isdigit(c)
char c = 'あ'; produces an error or warning on common implementations. A Japanese character takes multiple bytes (typically three in UTF-8), so use an array of char, use wchar_t, or handle the encoding explicitly. The examples here use ASCII letters, digits, and symbols that fit in a char. 'A'or 'あ' , memory stores numeric codes . A char being an integer is simply the result of assigning a number to a character . printf is scanf. Use %c for a character, and pass the variable’s address with &, just as with %d for int.#include <stdio.h> int main(void) { char c; printf("Enter one character: " ); scanf("%c", &c); // Remember the &! printf("Character entered: %c\n" , c); return 0; } /* Example Enter one character: A (type A and press Enter) Character entered: A */
%c(one character) &c, just as &n is passed for int input.c. printf("%c", c) displays c, while scanf("%c", &c) reads into c. Input requires its address, &c.scanf can read a leftover newline int n; char c; scanf("%d", &n); // Enter a number, then press Enter scanf("%c", &c); // Try to read a character next: this does not work as intended! printf("n=%d c=[%c]\n", n, c);
5, press Enter, and then enter A, c receives the leftover '\n' before you can type A. Input passes through a buffer: a waiting area for characters.5 followed by Enter, the buffer contains: scanf("%d", &n)is takes only 5 and stores it in n . The newline \nis remains :scanf("%c", &c)is reads the first character in the buffer . That character is \n, so: A has not yet been entered, but c already contains \n instead. %c using two calls . Discard the leftover newline first, then read the intended character: %cread twice %c twice: store the first character in dummy to discard it, then read the character you want. int n; char dummy; char c; scanf("%d", &n); scanf("%c", &dummy); // Discard the leftover '\n' scanf("%c", &c); // Read the next character entered printf("n=%d c=[%c]\n", n, c); // → n=5 c=[A], as expected!
%c discards the newline; the second %c reads the intended character. . %d reads the number but can leave Enter's newline behind. %c treats that newline as a character, which is why consecutive input needs care. %c" %c" skips the leftover newline and then reads one character, so you don't need dummy. This is what real programs usually use.scanf("%d", &n); scanf(" %c", &c); // the space before %c skips the '\n'
dummy approach assumes exactly one newline is left over, so it breaks if you type extra spaces before pressing Enter after "5".Check your understanding of this lesson!
char c = 'A';, what does printf("%d", c) output? A char stores an integer (an ASCII code). 'A' is 65, so %d prints 65, while %c prints A.
'A'(single quotes) and "A"(double quotes): what is the difference? 'A' has type int. Storing its value in char c uses a one-byte variable. The string literal "A" is a two-byte char array containing 'A' and '\0'.scanf("%d", &n), how can you read one character correctly? The first %c discards the leftover newline into dummy; the second reads the intended character. Although it's not among the choices, writing scanf(" %c", &c) (a space before %c) also skips the newline.