01 / 20
H i ! \0 char[]

C-Style Strings vs.
std::string

Master the two fundamental approaches to handling text in C++: raw character arrays and the modern string class.

C++

What Is a String?

In programming, a string is a sequence of characters used to represent text: names, messages, file paths, and more. In C++, there are two fundamentally different ways to store strings.

C-Style String
A raw array of characters ending with a special null character '\0'. Inherited from the C language.
std::string
A class from the C++ Standard Library that wraps a character array and manages memory for you automatically.

C-Style Strings in Memory

A C-style string is simply a contiguous block of characters in memory, terminated by a null character ('\0'). The null terminator tells functions where the string ends.

'H' 'e' 'l' 'l' 'o' '\0' ? ? [0] [1] [2] [3] [4] [5] [6] [7] char greeting[8] = "Hello"; actual string data null unused

std::string Under the Hood

std::string is a class that internally manages a dynamic character array. It tracks both the length and the capacity of the buffer and resizes automatically when needed.

std::string s ptr size capacity → 5 15 Heap-allocated buffer H e l l o available capacity Stack object Heap buffer (fixed size, ~32 bytes) (grows automatically)

Declaring C-Style vs. std::string

// C-Style Strings char name1[] = "Alice"; // Array of 6 chars (5 + '\0') char name2[20] = "Bob"; // Fixed buffer, 20 bytes const char* name3 = "Carol"; // Pointer to string literal // std::string #include <string> std::string name4 = "Dave"; // Managed object std::string name5("Eve"); // Constructor syntax std::string name6; // Empty string (safe!)

The Core Rule

C-Style String You handle: ✕ Buffer size ✕ Null terminator ✕ Overflow checks ✕ Memory allocation ✕ Deallocation std::string Handled for you: ✓ Auto-resizes ✓ Always terminated ✓ Bounds-aware ✓ Automatic alloc ✓ Automatic dealloc →

Key Distinction: Operations

Operation C-Style std::string
Get length strlen(s) s.size()
Copy strcpy(dst, src) dst = src
Concatenate strcat(dst, src) s1 + s2
Compare strcmp(a, b) == 0 a == b
Find substring strstr(s, sub) s.find(sub)
Safety Buffer overflow risk Bounds-managed

How to Choose

1
Default to std::string. It is safer, easier to use, and less error-prone for almost all situations.
2
Use C-style only when required: interfacing with C libraries, embedded systems with no Standard Library, or legacy codebases.
3
Convert between them using s.c_str() (std::string to C-style) and the std::string constructor (C-style to std::string).
4
Never mix operations. Do not use strcmp on a std::string or the + operator on C-style arrays.

Example: Basic Operations

#include <iostream> #include <string> #include <cstring> int main() { // std::string: simple and safe std::string greeting = "Hello"; std::string name = "World"; std::string message = greeting + ", " + name + "!"; std::cout << message << "\n"; // Hello, World! // C-style: manual and error-prone char buf[50]; strcpy(buf, "Hello"); strcat(buf, ", "); strcat(buf, "World!"); std::cout << buf << "\n"; // Hello, World! }

Example: The Danger of Overflow

char name[5]; // Only 5 bytes: room for 4 chars + '\0' // User types "Alexander" (9 chars + '\0' = 10 bytes) strcpy(name, "Alexander"); // BUFFER OVERFLOW! // Writes past the array boundary // Undefined behavior: crash, corruption, or security exploit
A l e x a n d e r \0 allocated (5 bytes) overflow zone (not ours!) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Example: Converting Between Types

#include <string> #include <cstring> void legacyPrint(const char* msg) { // Some old C library function printf("%s\n", msg); } int main() { // std::string → C-style (read-only) std::string modern = "Hello from C++!"; legacyPrint(modern.c_str()); // .c_str() returns const char* // C-style → std::string const char* old = "Hello from C!"; std::string upgraded = old; // Implicit conversion std::string explicit_("Or explicit!"); }

Example: The Comparison Trap

// C-style comparison: == compares POINTERS, not content! char a[] = "hello"; char b[] = "hello"; if (a == b) { // FALSE! Compares memory addresses // This code never runs } if (strcmp(a, b) == 0) { // TRUE: compares content character by character // This works correctly } // std::string comparison: == compares CONTENT std::string x = "hello"; std::string y = "hello"; if (x == y) { // TRUE: compares the actual text // Works as expected! }

Common Mistakes

✕ Forgetting '\0'
Manually building a C-style string without adding the null terminator. Functions like strlen will read past the array into garbage memory.
✕ Buffer Overflow
Using strcpy or strcat without checking if the destination buffer is large enough. This corrupts adjacent memory.
✕ Using == on char[]
Writing if (a == b) when a and b are C-style strings. This compares addresses, not the string content.
✕ Modifying .c_str()
Casting away const from .c_str() to modify the internal buffer. This is undefined behavior and can corrupt the std::string object.

Summary Comparison

Feature char[] / char* std::string
Memory Manual Automatic
Resizable No Yes
Knows its length No (must scan) Yes (.size())
Comparison strcmp() ==
Concatenation strcat() +
Safety Overflow risk Bounds-checked
Performance Slight edge Near-identical
Use when C interop, embedded Everything else

Key Takeaways

1
C-style strings are character arrays terminated by '\0'. You manage the memory and must guard against overflow.
2
std::string is a class that manages a heap-allocated buffer internally. It resizes, copies, and deallocates automatically.
3
Prefer std::string in all new C++ code unless you need to interface with a C API or have strict memory constraints.
4
Use .c_str() to bridge from std::string to C-style, and assign directly to go from C-style to std::string.

Question 1 of 5

What character terminates a C-style string in memory?

Question 2 of 5

What does the == operator compare when used on two C-style string variables (e.g., char a[] and char b[])?

Question 3 of 5

How do you convert a std::string to a const char* for passing to a C library function?

Question 4 of 5

You declare char buf[5] = "Hello"; in your program. What happens?

Question 5 of 5

Which of the following is a key advantage of std::string over C-style strings?