Strings in C Tutorials
Programming
Strings in C
A string in C is a sequence of characters stored in a character array and terminated by a special character called the null character ('\0').
The null character marks the end of the string and helps C functions determine where the string ends.
Unlike many programming languages, C does not provide a built-in string data type. Strings are implemented using arrays of characters (char).
Example of String
#include <stdio.h>
int main()
{
char str[] = "Geeks";
printf("The string is: %s\n", str);
return 0;
}Output
The string is: GeeksHow Strings Are Stored in Memory
When we write:
char str[] = "Geeks";C internally stores the string as:
{'G', 'e', 'e', 'k', 's', '\0'}Notice that the null character ('\0') is automatically added at the end of the string.
This null character tells the compiler where the string ends.
Why is '\0' Important?
The %s format specifier prints characters until it encounters the null character.
For example:
printf("%s", str);The output will continue printing characters until '\0' is found.
Without the null character, the program may print garbage values or behave unexpectedly.
Accessing Characters of a String
Just like arrays, individual characters of a string can be accessed using their index.
Example
#include <stdio.h>
int main()
{
char str[] = "Geeks";
printf("%c", str[0]);
return 0;
}Output
GString Indexing
For the string:
char str[] = "Geeks";Indexes are:
Character : G e e k s
Index : 0 1 2 3 4The first character always starts at index 0.
Updating a String
Individual characters can be modified using their index.
Example
#include <stdio.h>
int main()
{
char str[] = "Geeks";
str[0] = 'R';
printf("%s", str);
return 0;
}Output
ReeksReplacing the Entire String
The entire string can be replaced using functions such as strcpy().
Example
#include <stdio.h>
#include <string.h>
int main()
{
char str[20] = "Hello";
strcpy(str, "World");
printf("%s", str);
return 0;
}Output
WorldFinding String Length
The length of a string can be calculated using the strlen() function available in the <string.h> header file.
Example
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Geeks";
printf("%d", strlen(str));
return 0;
}Output
5Important Note
The strlen() function counts only the actual characters of the string.
It does not count the null character ('\0').
For the string:
textCopy
GeeksLength = 5
String Input in C
There are multiple ways to take string input from the user.
The most common methods are:
scanf()
scanf() with scanset
fgets()
Using scanf()
The simplest way to read a string is using scanf().
Example
#include <stdio.h>
int main()
{
char str[20];
scanf("%s", str);
printf("%s", str);
return 0;
}Input
GeeksOutput
GeeksLimitation of scanf()
scanf() stops reading when it encounters:
Space
Tab
Newline
Example Input
Geeks For GeeksOutput:
GeeksOnly the first word is read.
Using scanf() with Scanset
A scanset allows scanf() to read input until a specific character is encountered.
Example
#include <stdio.h>
int main()
{
char str[50];
scanf("%49[^\n]", str);
printf("%s", str);
return 0;
}Input
Geeks For GeeksOutput
Geeks For GeeksThis method allows spaces in the input.
Using fgets()
The safest and most recommended method for reading strings is fgets().
It can read an entire line including spaces.
Example
#include <stdio.h>
int main()
{
char str[50];
fgets(str, sizeof(str), stdin);
printf("%s", str);
return 0;
}Input
Geeks For GeeksOutput
Geeks For GeeksPassing Strings to Functions
Strings can be passed to functions just like arrays.
Example
#include <stdio.h>
void printStr(char str[])
{
printf("%s", str);
}
int main()
{
char str[] = "GeeksforGeeks";
printStr(str);
return 0;
}Output
GeeksforGeeksStrings and Pointers
A string can also be accessed using pointers.
A character pointer stores the address of the first character of the string.
Example
#include <stdio.h>
int main()
{
char str[] = "Geeks";
char *ptr = str;
while(*ptr != '\0')
{
printf("%c", *ptr);
ptr++;
}
return 0;
}Output
GeeksHow It Works
char *ptr = str;The pointer stores the address of the first character.
The loop prints each character and moves the pointer to the next memory location until the null character is reached.
String Literals
A string literal is a sequence of characters enclosed in double quotes.
Examples:
"Hello"
"Welcome"
"12345"String literals are automatically terminated with a null character.
Example of String Literal
#include <stdio.h>
int main()
{
const char *str = "Hello World";
printf("%s", str);
return 0;
}Output
Hello WorldWhy Use const?
String literals are stored in read-only memory.
Therefore, modifying them is not allowed.
Correct way:
const char *str = "Hello";Avoid:
char *str = "Hello";because modifying such strings leads to undefined behavior.
Common String Functions
The <string.h> library provides many useful string functions.
strlen()
Returns the length of a string.
strlen(str);strcpy()
Copies one string into another.
strcpy(destination, source);strcat()
Concatenates two strings.
strcat(str1, str2);strcmp()
Compares two strings.
strcmp(str1, str2);Key Points
A string is a character array terminated by
'\0'.C does not have a built-in string data type.
Strings are stored using arrays of
char.String indexing starts from 0.
Individual characters can be accessed and modified using indexes.
strlen()is used to find string length.scanf(), scansets, andfgets()can be used for string input.Strings can be passed to functions like arrays.
Pointers can be used to traverse strings.
String literals are stored in read-only memory.
Always use
const char *when working with string literals.
Summary
Strings are one of the most important concepts in C programming and are used to store and manipulate textual data. Since C does not provide a dedicated string data type, strings are implemented as character arrays terminated by a null character ('\0'). Understanding string creation, input methods, string functions, pointers, and string literals is essential for building real-world C applications.