C++20 sections · 1024 units
Open in Course

C-style vs C++ Strings

Comparing approaches

C++ inherited C-style strings: character arrays ending with null ('\0'). These work but require manual memory management and are prone to buffer overflows. You track sizes yourself. Modern C++ provides the string class from <string>.

It manages memory automatically, grows as needed, and provides convenient methods. No null terminators or manual size tracking required. Use string for most work. It's safer and more convenient.

C-style strings appear in legacy code and system interfaces. For new code, the string class is almost always the right choice.