Remember the trailing \0 (NUL terminator) β it marks the end
Recall that char is really an integer (ASCII code)
For unfamiliar functions (e.g., strlen), learn how to use them first, internals later
π‘ Tip:"Hello" is really a 6-byte array {'H','e','l','l','o','\0'}. "String = array + terminator" is enough to start.
What is a string?
C has no string type. Instead, strings are represented as arrays of char.
Key point: Every C string ends with a null character '\0', which marks where the string ends.
char name[20] = "Claude"; // up to 19 chars + '\0'
printf("%s\n", name); // -> Claude
%s is the string format specifier. Pass just the array name (the address of the first element).
The truth about char β characters are numbers (ASCII)
Before going deeper you need this key fact: A char is really a 1-byte (8-bit) integer. The letter 'A' you see on screen is nothing more than the number 65 interpreted as a character.
Core idea:char c = 'A'; and char c = 65; are exactly the same. printf("%c", 65) β A οΌ printf("%d", 'A') β 65
ASCII cheat sheet (main ranges)
Char
Decimal
Meaning
'\0'
0
NUL (string terminator)
'\n'
10
newline
' '
32
space
'0'β'9'
48β57
digits ('0' = 48)
'A'β'Z'
65β90
uppercase letters
'a'β'z'
97β122
lowercase letters
'a' - 'A' = 32 β the gap between lower and upper case drives several common tricks.
Example: characters and numbers interchange
#include<stdio.h>intmain(void) {
char c = 'A';
printf("c = %c\n", c); // β A (as a character)printf("c = %d\n", c); // β 65 (as a number)// arithmetic on characters worksprintf("%c\n", c + 1); // β B (65+1=66)printf("%c\n", 'Z' - 3); // β W// storing 65 in a char is the same as 'A'char d = 65;
printf("%c\n", d); // β Areturn0;
}
Classic idioms
β Upper to lower case
char c = 'A';
char lower = c + ('a' - 'A'); // 'A' + 32 = 'a'// or: char lower = c + 32;
β‘ Convert a digit character to its integer value
char ch = '7'; // this is 55 (ASCII for '7')int n = ch - '0'; // 55 - 48 = 7printf("%d\n", n); // β 7
β’ Character tests (uppercase / digit)
if (c >= 'A' && c <= 'Z') // uppercase?if (c >= '0' && c <= '9') // digit?// <ctype.h> also provides isupper(c), isdigit(c)
Why is char an integer in the first place?
Back in the 1960s ASCII (American Standard Code for Information Interchange) defined a mapping "letters/digits/symbols β 7-bit numbers". C inherited this by treating characters as integers. This simple design is what makes character arithmetic so easy in C.
Caveat: non-ASCII characters don't fit in one char. char c = 'γ'; is an error or warning. Non-ASCII characters need multiple bytes (3 bytes in UTF-8), so you store them in a char array or use wchar_t. Only ASCII (letters, digits, punctuation) is safe in a single char.
char array memory layout
Let's see how the string "Hello" is laid out in memory.
Mind the size! "Hello" is 5 characters, but you need 6 bytes including the trailing null character '\0'. Too small an array causes a buffer overflow bug.
String I/O
Use %s for string I/O. In scanf, note that you don't need &!