C Programming • Arrays & Functions
C Programming / Character arrays and Strings

Character arrays and Strings

Notes 3 Arrays & Functions

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.

Notes

Character Arrays and Strings

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.

💡 Example: char letters[5] = {'A', 'B', 'C', 'D', 'E'}; stores five characters in a character array.

What is a String?

Definition: A string in C is a sequence of characters terminated by a special character called the null character \0.

💡 Example: The string "HELLO" is stored as:

'H' 'E' 'L' 'L' 'O' '\0'

Character Array vs String

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"

Null Character (\0)

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.

💡 Example: char name[] = "RAM"; is stored as:

'R' 'A' 'M' '\0'

Therefore, the array requires space for 4 characters.

Declaring a Character Array

Definition: A character array is declared using the char data type followed by the array name and size.

char name[20];
💡 Example: char name[20]; can hold a string of up to 19 characters plus the null character.

Initializing a String

A string can be initialized using a string literal enclosed in double quotation marks.

💡 Example: char name[] = "Amit";
char name[] = "Amit";

String Indexing

Definition: Each character in a string is accessed using its index. Like other C arrays, string indexing starts from 0.

💡 Example: For char name[] = "HELLO";:

name[0]H
name[1]E
name[4]O
String:   H   E   L   L   O   \0
Index:    0   1   2   3   4    5

Accessing a Character

An individual character of a string can be accessed by using the string name and its index.

💡 Example: name[0] gives the first character of the string.

Modifying a Character

An individual character in a character array can be changed by assigning a new character to its index.

💡 Example: If name[0] = 'R';, the first character changes to R.

String Traversal

Definition: String traversal means visiting each character one by one until the null character \0 is reached.

💡 Example: A loop can continue while name[i] != '\0'.
for (int i = 0; name[i] != '\0'; i++)
{
    printf("%c ", name[i]);
}

Input of a String

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.

💡 Example: scanf("%s", name); reads a word such as Rahul.

Output of a String

A string can be displayed using printf() with the %s format specifier.

💡 Example: printf("%s", name); displays the complete string stored in name.

String Length

Definition: String length is the number of characters present in the string, excluding the terminating null character \0.

💡 Example: The length of "HELLO" is 5, not 6. The sixth position contains \0.

String Library Functions

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)

Practical Example — Read and Display a String

Problem Statement

Write a C program to accept a student's name and display the name using a character array.

Learning Outcomes

  • Declare and use a character array.
  • Accept a string from the user.
  • Display a string using printf().

Hint

Declare a character array and use scanf("%s", name) to read a simple name without spaces.

Theory

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.

Program

#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;
}

Expected Output

Enter your name: Rahul
Name = Rahul

Note

scanf("%s", name) reads a word until whitespace. For input containing spaces, functions such as fgets() are more suitable.

Practical Example — Find String Length

Problem Statement

Write a C program to accept a string and find its length using strlen().

Learning Outcomes

  • Use the string.h header file.
  • Use the strlen() function.
  • Understand that the null character is not counted in string length.

Hint

Include string.h and pass the string to strlen().

Theory

The strlen() function returns the number of characters in a string before the terminating null character.

Program

#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;
}

Expected Output

Enter a word: COMPUTER
Length = 8

Note

strlen() counts the characters in the string but does not count the terminating \0.

Common Mistakes

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.

Important Points for Exam

  • A character array stores elements of type char.
  • A C string is a character sequence terminated by \0.
  • String indexing starts from 0.
  • %s is commonly used to read or display strings.
  • strlen() returns the string length excluding \0.
  • The string.h header provides common string functions.
🎯 Easy Rule:

String = Characters + \0

Quick Revision

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.

Important Exam Questions

Short Answer Questions

  1. What is a character array?
  2. What is a string in C?
  3. What is the null character \0?
  4. Why does a string require an extra position for \0?
  5. How are characters of a string accessed?
  6. What is the use of strlen()?
  7. What is the purpose of string.h?
  8. Differentiate between a character array and a string.

Long Answer Questions

  1. Define character arrays and strings. Explain their declaration, initialization and indexing with examples.
  2. Explain the role of the null character in C strings.
  3. Explain common string functions such as strlen(), strcpy(), strcat() and strcmp().
  4. Write a C program to read and display a string.
  5. Write a C program to find the length of a string.
🎥 Recommended Learning

Watch a beginner-friendly explanation of character arrays, strings and the null character in C.

▶ Watch: Character Arrays & Strings in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for character arrays, strings and string functions will be provided here.

🧠 Mind Map

Use the mind map for quick revision of character arrays, null character, indexing and string functions.