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 values arranged in seven cells represent the string "coffee".'\0', the null character. It marks the end; without it, printf cannot determine where the string stops.char name[20] = "coffee"; // Reserve 20 char cells: up to 19 characters plus '\0' printf("%s\n", name); // → coffee
%s is the string format specifier. Pass the array name, which provides the address of its first element."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, 9); | If src is too long, the terminating '\0' is not added — add it yourself |
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 (%zu 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.snprintf(dst, sizeof dst, "%s", src); is handy (the result is always '\0'-terminated). If you use strncpy, add the trailing '\0' yourself as shown in the table above.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.