Strings (Lesson)
Strings in C Programming
In C, strings are not a separate primary data type like they are in Python or Java. Instead, they are treated as a special type of array.
9.1. Defining a String
A string is defined as a one-dimensional array of characters. It is a collection of characters stored in contiguous memory locations.
Syntax
char string_name[size];
Memory Representation
When you define char name[6] = "Sunil";, the memory looks like this:
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Value | S | u | n | i | l | \0 |
| Addr | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 |
Note: Always allocate N+1 space for a string of length N to accommodate the null terminator.
9.2. Understanding the NULL Character (\0)
The null character \0 is the most important concept in C strings. It is an escape sequence with an ASCII value of 0.
Why do we need it?
- End of String Marker: Since character arrays don't store their own size, functions like
printf("%s")continue reading memory until they find a\0. - Loop Termination: Most string processing is done using loops that run
while(str[i] != '\0'). - Memory Boundary: It prevents functions from accessing memory outside the intended string range.
9.3. Initialization of Strings
There are four primary ways to initialize strings in C:
1. String Literal (Implicit Null)
The most common way. The compiler automatically adds \0.
char str[] = "C Programming";
2. Character Array (Explicit Null)
When using curly braces, you must provide the \0 yourself.
char str[] = {'C', ' ', 'P', 'r', 'o', 'g', '\0'};
3. Pointer Initialization
This creates a string literal in read-only memory and a pointer to it.
char *ptr = "Hello";
Note: Modifying ptr[0] might cause a segmentation fault depending on the compiler.
4. Partial Initialization
If the size is larger than the content, the rest is automatically filled with null characters.
char str[10] = "Hi"; // Index 2-9 are all '\0'
9.4. Reading and Writing Strings
Reading Strings
A. scanf() (The Simple Way)
- Syntax:
scanf("%s", str); - Issue: It stops reading at the first whitespace (space, tab, or newline). If you enter "John Doe", it only stores "John".
- Risk: It doesn't check for buffer overflow.
B. fgets() (The Safe Way)
- Syntax:
fgets(str, size, stdin); - Advantage: You specify the maximum number of characters to read, preventing memory crashes.
- Note: It includes the newline character
\nif there is room.
Writing Strings
A. printf()
- Flexible; allows formatting with other variables.
printf("Name: %s\n", name);
B. puts()
- Simpler for single strings.
- Automatically appends a newline character
\nto the output.
9.5. Library Functions for Strings (string.h)
To use these functions, you must include #include <string.h>.
1. strlen(str)
- Calculates: Number of characters before the null character.
- Return Type:
size_t(unsigned integer). - Example:
strlen("Hi")returns2.
2. strcpy(dest, src)
- Action: Copies everything from
srctodest, including the\0. - Caution:
destmust be large enough to holdsrc.
3. strcat(dest, src)
- Action: Removes the
\0ofdest, appendssrc, and adds a new\0. - Example:
"Hello " + "Sunil"="Hello Sunil".
4. strcmp(str1, str2)
- Compares ASCII values of characters one by one.
0: Strings are identical.Positive:str1comes afterstr2alphabetically.Negative:str1comes beforestr2alphabetically.
Practical Implementation: User Authentication Simulation
This program demonstrates reading strings with spaces and comparing them.
#include <stdio.h>
#include <string.h>
int main() {
char saved_password[] = "Admin123";
char input_password[20];
char username[50];
printf("--- Welcome to the System ---\n");
// Using fgets to allow names with spaces
printf("Enter Full Name: ");
fgets(username, sizeof(username), stdin);
// Remove trailing newline from fgets
username[strcspn(username, "\n")] = 0;
printf("Enter Password: ");
scanf("%s", input_password);
if (strcmp(saved_password, input_password) == 0) {
printf("\nAccess Granted!\n");
printf("Hello, %s. Your name has %lu characters.\n", username, strlen(username));
} else {
printf("\nAccess Denied. Incorrect Password.\n");
}
return 0;
}
Key Differences Summary
| Feature | scanf | fgets |
|---|---|---|
| White Spaces | Stops at space | Includes spaces |
| Security | Risky (Overflow) | Safe (Size limit) |
| Newline | Left in buffer | Included in string |