C-Style Strings in C++
A C-style string is just a char array terminated by a '\0' null character: "hi" occupies 3 bytes (h, i, \0). The terminator is how <cstring> functions (strlen, strcpy, strcmp) know where text ends — and forgetting to reserve room for it is the original buffer overflow.
C-style strings still surface at C library boundaries, argv, and embedded code, so you must read them fluently — but new C++ code should live in std::string and convert (c_str()) only where APIs demand it.
Null terminator rule
Every C string ends with '\0'; a 5-letter word needs a char[6]. strlen counts characters excluding the terminator.
<cstring> toolkit
strcpy/strcat copy and append (unsafe, unbounded — prefer strncpy or spans), strcmp orders, strchr/strstr search.
Literals are read-only
char* p = "hi"; points at immutable storage — modifying it is undefined behavior. Use const char* or char[] copies.
Bridge to std::string
std::string s = cstr; wraps safely; s.c_str() hands a null-terminated pointer back to C APIs.
#include <iostream>#include <cstring> int main() { char word[16] = "hello"; std::cout << std::strlen(word) << std::endl; std::strcat(word, "!"); std::cout << word << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.