How to handle strings in C using char arrays, strlen, strcpy, and related functions.
char array ending with \0printf("%s", s); prints itstrlen, strcpy, strcmp are in <string.h>c + ('a'-'A')strcpy is unsafe β prefer snprintfchar s[10] is just an array\0 (NUL terminator) β it marks the endchar is really an integer (an ASCII code)strlen, learn how to use them first, and worry about the internals later"Hello" is really a 6-byte array: {'H','e','l','l','o','\0'}. "String = array + terminator" is enough to get you started.
char.'\0', which marks where the string stops.
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.char c = 'A'; and char c = 65; are exactly the same thing.printf("%c", 65) β A / printf("%d", 'A') β 65
| 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 upper- and lowercase letters powers several common tricks.#include <stdio.h> int main(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 works printf("%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); // β 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 for '7') int n = ch - '0'; // 55 - 48 = 7 printf("%d\n", n); // β 7
if (c >= 'A' && c <= 'Z') // uppercase? if (c >= '0' && c <= '9') // digit? // <ctype.h> also provides isupper(c), isdigit(c)
char c = 'γ'; triggers an error or warning. Non-ASCII characters need multiple bytes (3 bytes in UTF-8), so they live in a char array or a wchar_t. Only ASCII (letters, digits, punctuation) is safe in a single char."Hello" is laid out in memory.'\0'. Too small an array causes a buffer-overflow bug.
scanf, note that you don't need &!&? An array name like name decays to the address of its first element, so the address is already passed automatically.
scanf("%s") gotchas: it stops at whitespace and doesn't check input length. Long input can overflow the array β in practice, fgets is safer.
strlen from the standard library is the easy way. To see how it works, let's build a home-made version.'\0' is so important — it's the marker that tells you where the string ends.#include <string.h>.| Function | Purpose | Example | Notes |
|---|---|---|---|
strlen(s) | String length | strlen("abc") β 3 | Doesn't include '\0' |
strcpy(dst, src) | Copy a string | strcpy(s, "Hello") | dst must be large enough |
strncpy(dst, src, n) | Copy up to n chars | strncpy(s, src, 10) | Helps prevent buffer overflow |
strcat(dst, src) | Concatenate | strcat(s, " World") | dst must fit the result |
strcmp(s1, s2) | Compare strings | strcmp(a, b) == 0 | 0 = equal; +/- show order |
strncmp(s1, s2, n) | Compare first n chars | strncmp(s, "ab", 2) | Handy for prefix checks |
strchr(s, c) | Find a character | strchr(s, 'o') | Returns NULL if not found |
strstr(s, sub) | Find a substring | strstr(s, "llo") | Returns NULL if not found |
#include <stdio.h> #include <string.h> int main(void) { char greeting[50]; strcpy(greeting, "Hello"); // greeting = "Hello" strcat(greeting, ", World!"); // greeting = "Hello, World!" printf("%s (%lu chars)\n", greeting, strlen(greeting)); return 0; }
==!if (s1 == s2) compares addresses, not contents.strcmp(s1, s2) == 0.strcpy and strcat don't check the destination size.strncpy.struprstrlen'e') appearsCheck your understanding of this lesson!
C strings are always terminated with a null character \0, which is how code can tell where the string ends.
"Hello" is 5 characters plus the null terminator, so 6 bytes total. You need char s[6] or larger.
strcpy copies, strcmp compares, and strlen returns the length. Include <string.h> to use them.