In C, a **String** is a sequence of characters terminated by a null character \0. While other languages have a dedicated "String" type, C treats strings as one-dimensional arrays of type char. This gives programmers granular control over memory but requires a solid understanding of how character arrays work.
When you define a string, C allocates memory for each character in the sequence plus one extra byte for the Null Terminator (\0). This null character tells functions like printf() where the string ends.
For example, the string "HELLO" takes up **6 bytes** of memory, not 5, because of the hidden \0 at the end.
There are two primary ways to create a string in C. You can either use a string literal (easiest) or an array of individual characters.
Since a string is an array, you can access individual characters using their **index number**. Just like numeric arrays, the index starts at 0.
Because strings are written inside double quotes, you cannot simply put another double quote inside them. To include special symbols, you must use the backslash \ as an escape character.
| Escape Sequence | Result | Description |
|---|---|---|
\' |
' | Single quote |
\" |
" | Double quote |
\\ |
\ | Backslash |
C provides a set of powerful functions to manipulate strings, but you must include the <string.h> library to use them. These functions save you from writing complex loops for simple tasks.
Working with strings in C is slightly different than other languages like Python or Java:
char s[10]; s = "Hello";. You must use strcpy() or initialize it at the time of declaration.== to compare strings (e.g., if(str1 == str2)). This compares the memory addresses, not the text. Use strcmp() instead.scanf() to read a string, it stops at the first whitespace (space, tab, or newline). To read an entire line of text including spaces, use the fgets() function instead.