In C, a String is not a built-in data type like in Java or Python. Instead, it is a one-dimensional array of characters that is terminated by a special character called the Null Character (\0).
The null character is essential because it tells functions like printf() where the string ends in memory. Without it, the program would continue reading garbage data from the RAM.
There are two primary ways to create a string in C:
char name[] = "John"; (Modifiable)char *name = "John"; (Stored in read-only memory; usually immutable)Since strings are arrays, you cannot compare or copy them using == or =. You must use the functions provided in the string.h header.
| Function | Purpose |
|---|---|
strlen(str) |
Returns the length (excluding \0). |
strcpy(dest, src) |
Copies src into dest. |
strcat(dest, src) |
Appends src to the end of dest. |
strcmp(s1, s2) |
Returns 0 if strings are identical. |
Reading strings from a user requires caution to avoid "Buffer Overflows."
length + 1 to leave room for the \0.if (str1 == str2). This compares memory addresses, not the text content. Use strcmp() instead.To store a list of names, you use a 2D character array where each row is a string.