Loading content...
Loading content...
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).
#include <stdio.h>
int main()
{
char str[] = "Geeks";
printf("The string is: %s\n", str);
return 0;
}The string is: GeeksWhen 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.
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.
Just like arrays, individual characters of a string can be accessed using their index.
#include <stdio.h>
int main()
{
char str[] = "Geeks";
printf("%c", str[0]);
return 0;
}GFor 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.
Individual characters can be modified using their index.
#include <stdio.h>
int main()
{
char str[] = "Geeks";
str[0] = 'R';
printf("%s", str);
return 0;
}ReeksThe entire string can be replaced using functions such as strcpy().
#include <stdio.h>
#include <string.h>
int main()
{
char str[20] = "Hello";
strcpy(str, "World");
printf("%s", str);
return 0;
}WorldThe length of a string can be calculated using the strlen() function available in the <string.h> header file.
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Geeks";
printf("%d", strlen(str));
return 0;
}5The strlen() function counts only the actual characters of the string.
It does not count the null character ('\0').
For the string:
textCopy
GeeksLength = 5
There are multiple ways to take string input from the user.
The most common methods are:
scanf()
scanf() with scanset
fgets()
The simplest way to read a string is using scanf().
#include <stdio.h>
int main()
{
char str[20];
scanf("%s", str);
printf("%s", str);
return 0;
}GeeksGeeksscanf() stops reading when it encounters:
Space
Tab
Newline
Geeks For GeeksOutput:
GeeksOnly the first word is read.
A scanset allows scanf() to read input until a specific character is encountered.
#include <stdio.h>
int main()
{
char str[50];
scanf("%49[^\n]", str);
printf("%s", str);
return 0;
}Geeks For GeeksGeeks For GeeksThis method allows spaces in the input.
The safest and most recommended method for reading strings is fgets().
It can read an entire line including spaces.
#include <stdio.h>
int main()
{
char str[50];
fgets(str, sizeof(str), stdin);
printf("%s", str);
return 0;
}Geeks For GeeksGeeks For GeeksStrings can be passed to functions just like arrays.
#include <stdio.h>
void printStr(char str[])
{
printf("%s", str);
}
int main()
{
char str[] = "GeeksforGeeks";
printStr(str);
return 0;
}GeeksforGeeksA string can also be accessed using pointers.
A character pointer stores the address of the first character of the string.
#include <stdio.h>
int main()
{
char str[] = "Geeks";
char *ptr = str;
while(*ptr != '\0')
{
printf("%c", *ptr);
ptr++;
}
return 0;
}Geekschar *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.
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.
#include <stdio.h>
int main()
{
const char *str = "Hello World";
printf("%s", str);
return 0;
}Hello WorldString 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.
The <string.h> library provides many useful string functions.
Returns the length of a string.
strlen(str);Copies one string into another.
strcpy(destination, source);Concatenates two strings.
strcat(str1, str2);Compares two strings.
strcmp(str1, str2);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, and fgets() 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.
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.