Switching on Strings

What Is a String in C Programming?

C has no string type. A C string is a char array ending in a null character. Learn how it's stored, how to measure it and how to use it safely.

On this page
  1. Strings in C are character arrays
  2. Measuring a string
  3. Two ways to declare a string
  4. Buffer size
  5. Working with strings
  6. How this differs from C++
  7. Summary

Strings in C are character arrays

C has no built-in string type. A string in C is a sequence of char values stored in an array, with a special null character ('\0') after the last real character. The null character marks the end of the text, and the standard library functions rely on it to know where a string stops.

Take this declaration:

char name[] = "Ada";

The array holds four values: 'A', 'd', 'a' and the null character. The null character takes one byte, which is why the array needs room for it.

Index 0 1 2 3
Value 'A' 'd' 'a' '\0'

Measuring a string

strlen() returns the number of characters before the null character. sizeof returns the total size of the array, including the null character:

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

int main(void) {
    char name[] = "Ada";

    printf("Characters: %zu\n", strlen(name));
    printf("Bytes in array: %zu\n", sizeof(name));

    return 0;
}

Output:

Characters: 3
Bytes in array: 4

sizeof gives the array size here because name is an array. If name were a pointer, sizeof would give the size of the pointer instead, so the two can differ.

Two ways to declare a string

  • char name[] = "Ada"; creates an array that the compiler sizes for you. You can change its characters.
  • char *name = "Ada"; creates a pointer to a string literal. Changing the characters through this pointer is undefined behavior, so treat the text as read-only.

In C++, a string literal is an array of const char, so the pointer form should be written as const char *name = "Ada";. Leaving out const is ill-formed in C++, and many compilers warn about it.

Buffer size

When you store text in an array, the array must have room for the null character as well as the visible characters. Writing past the end of an array, such as copying a longer string into a small array, is a common bug. It can crash the program or corrupt other data, and the compiler may not catch it.

Working with strings

The <string.h> header provides common functions for C strings, including these:

  • strlen() returns the length, not counting the null character.
  • strcpy() copies one string into another.
  • strcat() appends one string to the end of another.
  • strcmp() compares two strings and returns 0 when they match.

Each of these expects the strings to end with a null character. strcpy() and strcat() do not check the size of the destination, so make sure it is large enough before you call them.

Comparing two C strings with == checks their addresses, not their text. That is why how to compare two strings in C usually means calling strcmp().

How this differs from C++

In C++, std::string is a class that stores its length and manages its own memory. It is the usual choice in C++ code. C-style strings still appear in older code and in some library interfaces, so it helps to recognize them.

Summary

A string in C is a char array that ends with a null character. Keep that terminator in mind when you size arrays, and use the standard library functions for common work, with care about the size of each destination.

The complete guide to switching on strings

How to Switch on a String in C++
Read the guide

Keep exploring

Guides from other topics

All topics →