C Notes
Learn about strings in C programming including character arrays, null terminator, string declaration, initialization methods, and how strings are stored in memory.
Unlike languages such as Python or Java that have built-in string types, C doesn't have a dedicated string data type. Instead, strings in C are simply arrays of characters terminated by a special null character '\0'. This design gives C programmers direct control over string memory but also places the responsibility of proper management squarely on their shoulders.
Understanding how strings work in C is essential because nearly every real program deals with text — file names, user input, messages, configuration data, and more.
What is a String in C?
A string in C is defined as a sequence of characters stored in a character array, ending with the null character '\0' (ASCII value 0). This null terminator is how C functions know where the string ends.
Key Point: A string of length N requires a character array of size at least N+1 to accommodate the null terminator.
String Declaration Methods
Method 1: Character Array with Size
Method 2: Initialize with String Literal
Method 3: Automatic Size Calculation
char greeting[] = "Hello"; // Size automatically set to 6Method 4: Character-by-Character
Method 5: String Pointer (Read-Only)
char *message = "Hello"; // Points to string literal (read-only memory)String Literal vs Character Array
This distinction is critical and frequently tested in interviews:
| Feature | char str[] = "Hello" | char *str = "Hello" |
|---|---|---|
| Storage | Stack (modifiable) | Read-only data segment |
| Modifiable? | Yes | No (undefined behavior) |
sizeof(str) | 6 (array size) | 4 or 8 (pointer size) |
| Can reassign? | No (str = "Bye" invalid) | Yes (str = "Bye" valid) |
arr: Jello ptr: Hello
Reading Strings from User
Using scanf (Reads Until Whitespace)
Enter your name: John Hello, John!
Limitation: scanf("%s", ...) stops reading at the first whitespace character. If you type "John Smith", only "John" is stored.Using fgets (Reads Entire Line)
Enter your full name: John Smith Hello, John Smith!
Using gets (DANGEROUS — Never Use)
Printing Strings
Hello, World! Hello, World! Hello, World! Hello, World!
The Null Terminator: Why It Matters
The null character '\0' is what distinguishes a string from a random character array. Without it, string functions have no way to know where the string ends:
Length of good: 5 Length of bad: 7
The second strlen call reads past the array boundary until it happens to find a zero byte in memory — this is undefined behavior and the result is unpredictable.
String Memory Diagram
| Address | 0xFF0 0xFF1 0xFF2 0xFF3 0xFF4 0xFF5 ... 0xFF9 |
| Value | │ 'C' │ 'a' │ 't' │ '\0' │ 0 │ 0 │ .. │ 0 │ |
| Index | [0] [1] [2] [3] [4] [5] ... [9] |
Common String Operations Overview
| Operation | Function | Header |
|---|---|---|
| Length | strlen(s) | <string.h> |
| Copy | strcpy(dest, src) | <string.h> |
| Concatenate | strcat(dest, src) | <string.h> |
| Compare | strcmp(s1, s2) | <string.h> |
| Search char | strchr(s, c) | <string.h> |
| Search substring | strstr(s1, s2) | <string.h> |
Array of Strings
You can create an array of strings using a 2D character array:
Available fruits: 1. Apple 2. Banana 3. Cherry 4. Mango
Interview Questions
Q1: What's the difference between char str[5] = "Hello" and char str[6] = "Hello"?
char str[5] = "Hello" stores all 5 characters but has no room for the null terminator — it's technically not a valid C string. char str[6] = "Hello" correctly stores 5 characters plus '\0'.
Q2: Why doesn't scanf need the & operator for strings?
Because the array name already evaluates to the address of the first element. Writing scanf("%s", name) is equivalent to scanf("%s", &name[0]) — both pass the same address.
Q3: What happens if you forget the null terminator?
String functions like strlen, strcpy, and printf("%s", ...) will read past the array boundary until they encounter a zero byte in memory. This leads to undefined behavior — crashes, garbage output, or security vulnerabilities.
Q4: Can you modify a string literal in C?
No. String literals like "Hello" are stored in read-only memory. Attempting to modify them through a pointer causes undefined behavior (typically a segmentation fault). Always use char arr[] = "Hello" if you need a modifiable string.
Summary
- Strings in C are null-terminated character arrays — no built-in string type exists
- Always allocate N+1 bytes for a string of length N (for the
'\0') - Use
fgetsinstead ofscanforgetsto safely read strings with spaces char[]creates a modifiable copy;char*pointing to a literal is read-only- The null terminator
'\0'is essential — without it, string functions fail strlenreturns the string length (excluding'\0');sizeofreturns total array size
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Strings in C Programming - Complete Introduction.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, strings, introduction, strings in c programming - complete introduction
Related C Programming Topics