Home
βΊ
Arrays & Strings
βΊ
Lesson 12: Strings
Lesson 12: Strings
How to handle strings in C using char arrays, strlen, strcpy, and related functions.
What is a string?
C has no string type . Instead, strings are represented as arrays of char .
Key point: Every C string ends with a null character '\0' , which marks where the string ends.
char name[20] = "Claude"; // up to 19 chars + '\0'
printf("%s\n", name); // -> Claude
%s is the string format specifier. Pass just the array name (the address of the first element).
char array memory layout
Let's see how the string "Hello" is laid out in memory.
char msg[6] = "Hello"; ← allocates 6 bytes (5 chars + '\0')
Mind the size! "Hello" is 5 characters, but you need 6 bytes including the trailing null character '\0' . Too small an array causes a buffer overflow bug.
String I/O
Use %s for string I/O. In scanf, note that you don't need & !
char name[20];
printf("Name? ");
scanf("%s", name); // no & needed
printf("Hello, %s\n", name);
Why no &? An array name like name decays to the address of its first element , so its address is passed automatically.
[!] Pitfalls of scanf("%s"): It splits on whitespace and does not check input length. Long input can overflow the array (in practice, fgets is safer).
Counting string length
strlen (from the standard library) is the easy way. To understand how it works, let's look at a "home-made" version.
// Count until '\0'
int myStrlen(char s[]){
int i = 0;
while(s[i] != '\0') i++;
return i;
}
int main(void){
char msg[] = "Hello";
printf("%d\n", myStrlen(msg)); // -> 5
}
You can see why the null character '\0' is so important — it's the marker that signals the end of the string.
string.h functions
The most important string functions available via #include <string.h>.
Function Purpose Example Notes
strlen(s)String length strlen("abc") -> 3Does not include '\0'
strcpy(dst, src)Copy string strcpy(s, "Hello")dst must be large enough
strncpy(dst, src, n)Copy up to n chars strncpy(s, src, 10)Prevents buffer overflow
strcat(dst, src)Concatenate strcat(s, " World")dst must fit the result
strcmp(s1, s2)Compare strings strcmp(a, b) == 00 = equal; +/- for order
strncmp(s1, s2, n)Compare first n chars strncmp(s, "ab", 2)Good 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
strcpy and strcat example
#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 ;
}
Never compare strings with ==! if (s1 == s2) compares addresses, not contents. Always compare with strcmp(s1, s2) == 0.
Beware of buffer overflow: strcpy and strcat do not check destination size. Use a large enough array, or limit the copy with strncpy.
Try it yourself — strings
A program that prints a string and its length.
Click "Run" to execute...
▶ Run
Review Quiz
Check your understanding of this lesson!
Q1. What is at the end of a C string?
Newline character \n
Null character \0
Space
C strings are always terminated by a null character \0, which lets code determine the string's length.
Q2. What array size is needed to store "Hello"?
5
6
7
"Hello" is 5 characters plus the null terminator, so 6 bytes total. char s[6] or larger is required.
Q3. Which function copies a string?
strcpy
strcmp
strlen
strcpy copies, strcmp compares, strlen returns the length. Include string.h to use them.
Recommended Books to Deepen Your Understanding
Combine this interactive site with books for more practice.
π
Painfully Learning C
by MMGames
A classic C book for beginners. Builds solid fundamentals with clear explanations.
View on Amazon
π
New Clear C Language (Beginner)
by Bohyoh Shibata
Plenty of diagrams and exercises. Widely used as a university textbook.
View on Amazon
π
The C Programming Language, 2nd Ed.
by Brian Kernighan & Dennis Ritchie
Known as K&R. The original C book. Ideal after completing the basics.
View on Amazon
* Links above are affiliate links. Purchases help support this site.