wleci
AboutProjectsBlogContact
Contact
Back to blog
C++CMemory

C-Style Strings: Char Arrays and Null Termination

Understanding low-level text handling: how character arrays terminated by a null byte work.

January 28, 20262 min read
Share:

What are C-Style Strings?

In C and low-level C++, a string is not a dedicated object but an array of char elements that ends with a special character with a value of zero: \0 (the null terminator).

Declaration and Initialization

When you write a string in double quotes, the compiler automatically adds \0 at the end:

cpp
char str[] = "Hey"; // In memory: ['H', 'e', 'y', '\0'] // Array size is 4 bytes, even though the text has 3 letters.

Why is the \0 character important?

Functions operating on such strings (e.g., those from the <cstring> library) do not know how long the array is. They scan it character by character until they hit a byte with value 0. If it's missing, the program will continue reading memory, leading to Undefined Behavior.

Essential functions from <cstring>

To manipulate these strings, we use:

  • strlen(str): Returns the length of the string (excluding the null character).
  • strcpy(dest, src): Copies a string (dangerous, can cause buffer overflows!).
  • strcmp(s1, s2): Compares two strings (returns 0 if they are identical).

std::string vs C-style

In modern C++, we almost always use the std::string class. However, C-style strings are still present in:

  1. System interfaces (e.g., WinAPI, Linux syscalls).
  2. The main(int argc, char* argv[]) function arguments.
  3. Embedded systems optimization.

Important: If you have a std::string and need a C-style version (e.g., for an old library), use the .c_str() method.

You might also like

C++Algorithms

Basic C++ Algorithms: Sorting, Searching, and Generating

Harness the power of the <algorithm> library. A guide to std::sort, std::find, std::copy, and sequence generation.

1 min read
C++Math

The <cmath> Library in C++ – Math Essentials

An overview of essential math functions in C++: from powers and roots to advanced trigonometry.

1 min read
C++Programming

Understanding the #include Directive in C++

A guide to the #include directive: learn how the preprocessor links files and why the choice of brackets matters.

1 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland