Definition: A character array is an array whose elements are of type char. It can store individual characters and can also be used to store a string.
Definition: A character array is an array whose
elements are of type char. It can store individual
characters and can also be used to store a string.
char letters[5] = {'A', 'B', 'C', 'D', 'E'};
stores five characters in a character array.
Definition: A string in C is a sequence of characters
terminated by a special character called the
null character \0.
'H' 'E' 'L' 'L' 'O' '\0'
| Character Array | String |
|---|---|
| Array of characters. | Character sequence terminated by \0. |
Need not contain \0. |
Must end with \0 to be a C string. |
Example: {'A','B','C'} |
Example: "ABC" |
Definition: The null character \0
marks the end of a string in C. It is automatically added when a
string literal is used to initialize a character array with enough
space.
char name[] = "RAM"; is stored as:
'R' 'A' 'M' '\0'
Definition: A character array is declared using
the char data type followed by the array name and size.
char name[20];
char name[20]; can hold a string of up to
19 characters plus the null character.
A string can be initialized using a string literal enclosed in double quotation marks.
char name[] = "Amit";
char name[] = "Amit";
Definition: Each character in a string is accessed using its index. Like other C arrays, string indexing starts from 0.
char name[] = "HELLO";:
name[0] → Hname[1] → Ename[4] → O
String: H E L L O \0
Index: 0 1 2 3 4 5
An individual character of a string can be accessed by using the string name and its index.
name[0] gives the first character of the string.
An individual character in a character array can be changed by assigning a new character to its index.
name[0] = 'R';, the first character changes to
R.
Definition: String traversal means visiting each
character one by one until the null character \0 is reached.
name[i] != '\0'.
for (int i = 0; name[i] != '\0'; i++)
{
printf("%c ", name[i]);
}
A string can be read from the user using functions such as
scanf() or fgets(). For a simple
space-free word, scanf("%s", name) can be used.
scanf("%s", name);
reads a word such as Rahul.
A string can be displayed using printf() with the
%s format specifier.
printf("%s", name);
displays the complete string stored in name.
Definition: String length is the number of characters
present in the string, excluding the terminating null character
\0.
\0.
C provides commonly used string functions through the
string.h header file.
| Function | Purpose | Example |
|---|---|---|
| strlen() | Finds the length of a string. | strlen(name) |
| strcpy() | Copies one string into another. | strcpy(b, a) |
| strcat() | Joins two strings. | strcat(a, b) |
| strcmp() | Compares two strings. | strcmp(a, b) |
Write a C program to accept a student's name and display the name using a character array.
printf().
Declare a character array and use scanf("%s", name)
to read a simple name without spaces.
A C string is stored in a character array and ends with the
null character \0. The %s format
specifier is used with printf() to display a string.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter your name: ");
scanf("%29s", name);
// display the entered string
printf("Name = %s
", name);
return 0;
}
Enter your name: Rahul Name = Rahul
scanf("%s", name) reads a word until whitespace.
For input containing spaces, functions such as fgets()
are more suitable.
Write a C program to accept a string and find its length using
strlen().
string.h header file.strlen() function.
Include string.h and pass the string to
strlen().
The strlen() function returns the number of characters
in a string before the terminating null character.
#include <stdio.h>
#include <string.h>
int main()
{
char name[30];
int length;
printf("Enter a word: ");
scanf("%29s", name);
length = strlen(name); // find the number of characters
printf("Length = %d
", length);
return 0;
}
Enter a word: COMPUTER Length = 8
strlen() counts the characters in the string but
does not count the terminating \0.
| Mistake | Correct Understanding |
|---|---|
Forgetting \0 |
A C string must be null-terminated. |
| Starting index from 1 | String indexing starts from 0. |
Using %d for a string |
Use %s for a string. |
| Array too small | Leave space for the null character. |
Expecting scanf("%s") to read spaces |
Use fgets() for input containing spaces. |
char.\0.%s is commonly used to read or display strings.strlen() returns the string length excluding \0.string.h header provides common string functions.| Concept | Remember |
|---|---|
| Character Array | Array of char elements. |
| String | Character sequence ending with \0. |
| Index | Starts from 0. |
| %s | String input/output format specifier. |
| strlen() | Returns string length excluding \0. |
| string.h | Header file for common string functions. |
\0?\0?strlen()?string.h?Watch a beginner-friendly explanation of character arrays, strings and the null character in C.
A short handwritten-style revision sheet for character arrays, strings and string functions is covered in the study notes above.
Use the mind map for quick revision of character arrays, null character, indexing and string functions.