Skip to main content

Strings in C – Arrays of Characters

Updated 4 min readMarkdown
Part of seriesC Programming

In the previous lessons, you learned about:

  • Arrays

  • Pointers

  • How arrays and pointers are related

Now we will apply that knowledge to something very common in programming: strings.

In C, strings are not a special data type like in some other languages.
Instead, strings are simply arrays of characters.

Understanding this will help you work with text in C programs.

1. What Is a String in C?

A string in C is:

A sequence of characters stored in an array and terminated by a special character called the null terminator ('\0').

Example:

char name[6] = "Hello";

This string contains:

H  e  l  l  o  \0

There are 6 characters in memory:

  • 5 letters

  • 1 null terminator

The null terminator marks the end of the string.

2. Why the Null Terminator Is Important

The null character ('\0') tells C where the string ends.

Without it, C would keep reading memory until it accidentally finds a zero.

Example in memory:

Index   Value
0       H
1       e
2       l
3       l
4       o
5       \0

That \0 is essential.

3. Declaring Strings

Method 1: Using a String Literal

char name[6] = "Hello";

C automatically adds the null terminator.

Method 2: Using Character List

char name[6] = {'H','e','l','l','o','\0'};

You must include '\0' yourself.

Method 3: Letting C Determine Size

char name[] = "Hello";

C automatically allocates the correct size.

4. Printing Strings

You can print strings using %s.

Example:

#include <stdio.h>

int main() {
    char name[] = "Hello";
    printf("%s\n", name);
    return 0;
}

Output:

Hello

%s tells printf to print characters until it reaches '\0'.

5. Looping Through a String

Since strings are arrays, we can use loops.

Example:

#include <stdio.h>

int main() {
    char word[] = "School";

    for (int i = 0; word[i] != '\0'; i++) {
        printf("%c\n", word[i]);
    }

    return 0;
}

This prints each character one by one.

The loop stops when it reaches the null terminator.

6. Common String Functions

C provides useful string functions in <string.h>.

1. strlen – String Length

strlen(word);

Returns number of characters excluding '\0'.

Example:

strlen("Hello") → 5

2. strcpy – Copy String

strcpy(dest, source);

Example:

char a[10];
strcpy(a, "Hello");

Copies "Hello" into a.

3. strcat – Join Strings

strcat(dest, source);

Example:

char a[20] = "Hello ";
strcat(a, "World");

Result:

Hello World

4. strcmp – Compare Strings

strcmp(a, b);

Returns:

  • 0 → strings equal

  • <0 → first string smaller

  • 0 → first string larger

Example:

strcmp("cat", "cat") → 0

7. Strings and Pointers

Because strings are arrays, they are closely related to pointers.

Example:

char *s = "Hello";

Here:

  • s points to the first character of the string

  • "Hello" is stored in memory

However, this string is usually read-only.

Trying to modify it may cause an error.

Example:

s[0] = 'Y';  // Dangerous

Instead, use a character array if you need to modify the string.

8. Example Program

#include <stdio.h>
#include <string.h>

int main() {
    char name[20] = "C Programming";

    printf("String: %s\n", name);
    printf("Length: %lu\n", strlen(name));

    return 0;
}

Output:

String: C Programming
Length: 13

9. Common Beginner Mistakes

❌ Forgetting the Null Terminator

If you manually create a string and forget '\0', functions may behave incorrectly.

❌ Array Too Small

char name[5] = "Hello"; // Wrong

You need space for \0.

Correct:

char name[6] = "Hello";

❌ Modifying String Literals

char *s = "Hello";
s[0] = 'J';  // unsafe

Use a character array instead.

10. Practice Exercises

  1. Create a string "Programming" and print each character using a loop.

  2. Ask the user for a name and print it.

  3. Write a program that:

    • copies one string into another

    • prints both.

  4. Compare two strings using strcmp.

Final Thoughts

Strings in C are simply arrays of characters.

Key ideas to remember:

  • Strings end with '\0'

  • %s prints strings

  • Strings can be looped through like arrays

  • <string.h> provides helpful functions

Understanding strings is important because text processing appears in almost every program.

In the next lesson, we will step back and look at data structures, where arrays, pointers, and structures combine to organize complex data.

About the author

  • Heya! 👋 I love helping people, and one of the best ways I do this is by sharing what I learn and build. I started as a pharmacist at a tertiary hospital in Northern Ghana, where I saw how much technology could improve healthcare. That sparked my interest in digital health. I taught myself to code, then joined the ALX Software Engineering program, which became a turning point. I went on to complete a Master of Health Informatics at the University of Toronto. Today I am a software and AI engineer and a health informatician, and I practise remotely as a Digital Health Pharmacist. I combine clinical experience with technical skills to build tools that solve real problems in health and beyond, and I write here about what I build and what I learn.

No comments yet. Be the first to comment.

C Programming

Part 22 of 40

From today, I will be starting lessons on C programming in my ALX Software Engineering class and I look forward to sharing with you everything I learn through this series.

Up nextIntroduction to Data Structures in CUp to this point, you have learned several important tools in C: Variables store individual values Arrays store collections of values Pointers give access to memory addresses Strings allow us to w

More from this blog