๐งต C Strings Overview โ Managing Text with Character Arrays
๐งฒ Introduction โ What Are Strings in C?
In C programming, a string is a one-dimensional array of characters terminated by a null character '\0'
. Unlike higher-level languages, C doesnโt have a built-in string
typeโstrings are handled manually using character arrays and functions from the <string.h>
library.
๐ฏ In this guide, youโll learn:
- How strings are stored and terminated in C
- Syntax for declaring and initializing strings
- How to access and manipulate strings
- Core functions used for string operations
๐งต Declaring and Initializing Strings
๐น Method 1: Character Array with Initialization
char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
๐น Method 2: Using String Literal (Preferred)
char str[] = "Hello";
The compiler automatically adds the
'\0'
null terminator at the end.
๐ฅ Accessing and Modifying Strings
Access individual characters using array indexing:
printf("%c", str[1]); // Output: 'e'
str[0] = 'M'; // Changes "Hello" to "Mello"
๐งช Common String Functions in <string.h>
Function | Purpose |
---|---|
strlen() | Returns length of string (excluding \0 ) |
strcpy() | Copies one string to another |
strcat() | Concatenates two strings |
strcmp() | Compares two strings |
strchr() | Finds first occurrence of a character |
strstr() | Finds substring |
โ ๏ธ Important Notes
- Strings must be null-terminated (
'\0'
), or undefined behavior occurs - Direct assignment like
str1 = "Hello"
is invalid after declarationโusestrcpy()
- Use
gets()
with caution (deprecated). Preferfgets()
for safe input.
๐ Best Practices
- โ
Always ensure space for the
'\0'
character - โ
Use
fgets()
for input to prevent buffer overflow - โ
Use
const char*
for immutable string literals - โ Donโt modify string literalsโthey are stored in read-only memory
๐ Summary โ Recap & Next Steps
C strings are arrays of characters with a '\0'
terminator. While powerful and flexible, they require careful management to avoid memory errors and buffer overflows.
๐ Key Takeaways:
- Strings are
char
arrays ending with'\0'
- Can be initialized with character lists or string literals
- Use
<string.h>
functions for safe and common operations - Always allocate space for the null terminator
โ๏ธ Real-World Relevance:
Strings are used in user input, filenames, file I/O, commands, messaging, and anywhere text processing is required.
โ Frequently Asked Questions (FAQ)
โ How is a string stored in memory?
โ
As a contiguous block of characters followed by a null terminator '\0'
.
โ Can I assign a string to a char[]
after declaration?
โ No. Use strcpy()
instead:
strcpy(str, "Hello");
โ What happens if I forget the null terminator?
โ C will not know where the string endsโmay cause buffer overflows or print garbage values.
โ How do I get string input in C?
โ
Use fgets()
:
fgets(str, sizeof(str), stdin);
โ Can I use ==
to compare strings in C?
โ No. Use strcmp()
from <string.h>
to compare content:
if (strcmp(str1, str2) == 0) { /* strings match */ }
Share Now :