Strings in C
# CHAPTER 11
Strings in C
1. Introduction
Unlike modern languages (Java, Python, C#), C does not have a built-inString data type. Instead, a string in C is simply an array of characters ending with a special null character ('\0'). Understanding this is crucial for managing memory and avoiding buffer overflows.
2. Learning Objectives
By the end of this chapter, you will be able to:- Declare and initialize strings as character arrays.
-
Understand the role of the Null Terminator (
\0).
-
Read and print strings using
scanf,gets, andprintf.
-
Use the
<string.h>library functions (strlen,strcpy,strcmp,strcat).
3. Declaring and Initializing Strings
A string must always be one character longer than the text you want to store, to make room for the null terminator.
4. The Null Terminator (\0)
The \0 tells the C compiler where the string ends. Without it, functions like printf will keep reading memory past the end of your string, resulting in garbage characters being printed on the screen.
5. Input and Output of Strings
Output: Use%s in printf.
Input: Use %s in scanf. Note: Do NOT use the & operator when reading a string with scanf. The array name itself points to the memory address!
*Warning:* scanf("%s") stops reading when it hits a space. To read a full line (like "John Doe"), use fgets():
6. The <string.h> Library
C provides a library of functions to manipulate strings. You must #include <string.h>.
#### 1. strlen() - String Length
Returns the number of characters (excluding \0).
#### 2. strcpy() - String Copy
You CANNOT use str1 = str2 in C. You must copy the contents.
#### 3. strcat() - String Concatenation
Joins two strings together.
#### 4. strcmp() - String Compare
Compares two strings. Returns 0 if they are identical.
7. Mini Project: Password Validator
8. Common Mistakes
-
Forgetting
\0room: Declaringchar word[5] = "Hello";causes an overflow because there's no room for the 6th character (\0).
-
Using
==to compare strings:if (str1 == str2)compares their *memory addresses*, not their text. Always usestrcmp().
-
Using
&in scanf for strings:scanf("%s", &name)is wrong. Usescanf("%s", name).
9. Exercises
-
1.
Write a program that asks for the user's first and last name separately, concatenates them using
strcat, and prints the full name.
-
2.
Write a program to manually calculate the length of a string using a loop (without using
strlen).
- 3. Write a program to reverse a string manually.
10. MCQ Quiz with Answers
A string in C is actually:
Which library is required for strlen()?
What does strcmp("apple", "apple") return?
Why do you NOT use & with scanf("%s", str)?
What is the length of char str[] = "Cat"; in memory?
What function copies string A to string B?
Which format specifier is used for strings?
What happens if you try str1 = str2; for two character arrays?
Which function safely reads a string containing spaces?
What does the \0 character represent?
11. Interview Questions
-
Q: Explain why C doesn't have a
Stringdata type, and how it handles text instead.
-
Q: How does
strlen()work internally?
- Q: What is a buffer overflow, and how can strings cause it?
12. Summary
Strings in C are simply arrays of characters terminated by a null character\0. To manipulate strings, you must use the <string.h> library functions like strcpy, strcat, and strcmp. Never compare strings using ==.