In C, a string is not a built-in data type like int or float. It is implemented as a one-dimensional array of characters that is terminated by a special character: the null character (\0).
1. The Null Terminator (\0)
The null character (\0) is vital because it marks the end of the string. Without it, C functions that process strings (like printf or strlen) wouldn’t know where the string ends, potentially reading past the allocated memory.
2. Declaring and Initializing Strings
Strings can be declared and initialized using several methods, all of which result in a character array.
A. Array Initialization (Implicit Null Termination)
When initializing a character array using a string literal enclosed in double quotes ("), the compiler automatically adds the null terminator at the end.
// Size is automatically 6 (5 characters + 1 for '\0')
char greeting[] = "Hello";
// Using a pointer (the pointer points to the start of the string literal)
char *message = "Welcome";
B. Array Initialization (Manual Termination)
If you initialize a character array element by element, you must manually include the null terminator (\0).
char city[5] = {'P', 'a', 'r', 'i', 's'};
// WARNING: This is NOT a C string! It lacks '\0'.
char country[8] = {'C', 'a', 'n', 'a', 'd', 'a', '!', '\0'};
// This is a properly terminated C string.
3. String Input (scanf and fgets)
While scanf can read strings using the %s format specifier, it is generally unsafe for modern C programming because it stops reading input at the first whitespace character (space, tab, newline) and can cause a buffer overflow if the input is longer than the array size.
Safer String Input (fgets)
The fgets() function is the preferred method for reading strings from input because it allows you to specify the maximum number of characters to read, preventing buffer overflows.
Syntax: fgets(string_name, size, stdin);
char input_name[50];
printf("Enter your full name: ");
// Read up to 49 characters from standard input (stdin) into input_name
fgets(input_name, 50, stdin);
4. Essential String Library Functions (<string.h>)
Working with C strings often requires functions from the standard <string.h> library.
| Function | Description | Example |
strlen() | Returns the length of the string (number of characters before \0). | int len = strlen(name); |
strcpy() | Copies one string into another. | strcpy(dest, src); |
strcat() | Concatenates (joins) two strings. | strcat(dest, src); |
strcmp() | Compares two strings lexicographically (alphabetically). Returns 0 if they are identical. | if (strcmp(s1, s2) == 0) |
Example: Copying and Concatenating
#include <string.h>
// ...
char source[] = "World";
char destination[20] = "Hello "; // Must have enough space
// Copy "Hello " into destination (strcpy is for full copy)
// strcat appends source to destination
strcat(destination, source);
// destination now holds "Hello World"
printf("%s\n", destination);
Warning on Assignment: You cannot use the simple assignment operator (
=) to copy strings after declaration.str1 = str2;will attempt to assign the pointer address, not copy the contents. You must usestrcpy()or similar functions.
