Comparing Strings

What Are Strings in C? With Examples

In C, a string is a char array that ends with a null terminator. Learn how C strings are stored, printed and measured, with short, clear examples.

On this page
  1. How a C string is stored
  2. A first example
  3. Length: strlen and sizeof
  4. Making room for the text
  5. String literals
  6. Walking through the characters
  7. What goes wrong
  8. Comparing C strings
  9. C++ note

In C, a string is not a built-in type. It is an array of char values that ends with a special null character, written '\0'. That terminator marks where the text stops, and the standard string functions depend on it to know where to stop reading. You will still meet these C-style strings in older code and in C libraries, even though modern C++ code usually uses std::string.

How a C string is stored

When you write char greeting[] = "Hello";, the compiler makes an array of six char values: the five letters and the terminator.

index:  0   1   2   3   4   5
char:   H   e   l   l   o   \0

The terminator takes up a slot, so the array must have room for it.

A first example

#include <stdio.h>

int main(void) {
    char greeting[] = "Hello";

    printf("%s\n", greeting);
    printf("First letter: %c\n", greeting[0]);

    return 0;
}

Output:

Hello
First letter: H

The %s format prints characters until it reaches the terminator.

Length: strlen and sizeof

strlen comes from <string.h>. It counts the characters before the terminator and does not count the terminator itself. sizeof reports the size of the array, which includes the terminator.

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

int main(void) {
    char greeting[] = "Hello";

    printf("strlen: %zu\n", strlen(greeting));
    printf("sizeof: %zu\n", sizeof(greeting));

    return 0;
}

Output:

strlen: 5
sizeof: 6

sizeof gives the size of the whole array only where the array is declared. If you pass the string to a function as a pointer, sizeof returns the size of the pointer instead. For that reason, use strlen or pass the length separately.

Making room for the text

The declaration char name[10] = "Ann"; creates an array of 10 characters. The text uses four of them (three letters and the terminator), and the remaining slots are set to zero.

The array must always be large enough for the text plus the terminator. If you write past the end of an array, the result is undefined behavior, which can crash a program or corrupt other data.

String literals

Text in double quotes, such as "Hello", is a string literal. You can point a pointer at one:

const char *message = "Hello";

Treat literal text as read-only, because changing its characters is undefined behavior. Declaring the pointer as const char * makes the compiler report accidental changes.

Walking through the characters

Because each string ends with '\0', a loop can stop when it reaches that character:

#include <stdio.h>

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

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

    return 0;
}

Output:

C
a
t

For more on this pattern, see how to iterate through a string in c.

What goes wrong

Most string bugs in C come from the terminator or from the array size:

  • A missing terminator. The declaration char bad[3] = {'C', 'a', 't'}; has no '\0'. Functions such as printf("%s", bad) and strlen() keep reading past the end of the array, which is undefined behavior.
  • An array that is too small. Copying a longer string into it writes past the end.
  • Joining strings. The destination needs room for both parts plus the terminator. The careful way to join them is covered in how to concatenate strings in c.

Comparing C strings

You cannot check whether two C strings hold the same text with ==, because == compares the addresses of the arrays. The standard function for comparing text is strcmp, and its return values are explained in what does strcmp do in C?

C++ note

If you are learning C++, std::string keeps track of its own length and memory, so you rarely have to manage a terminator yourself. You still need <cstring> for functions such as std::strlen and std::strcmp when you work with C-style strings.

In short, a C string is a char array that ends with '\0'. Keep the array large enough for the text and the terminator, and use the library functions that understand that terminator.

The complete guide to comparing strings

How to Compare Two Strings in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

How to Stringify a Macro in C++

Learn how the # operator turns a C++ macro argument into a string literal, with examples, the two-level trick for expanded macros and common pitfalls.

Macros 3 min read