A blog about learning C and C++

Differences between C and C++ - strings

January 26, 2025, by Slobodan Dmitrovic

In this article, we give an overview on how to define and manipulate strings in C++ as compared to the C way of handling character arrays.

Strings in C

In C, we represent strings through character arrays and pointers.

Strings in C++

In C++, the idiomatic way of representing and manipulating a string is through the std::string type declared inside a <string> header file.

The general syntax is:

std::string s = "Sample string value";

Strings as function arguments

When used as a function argument, the string is usually passed through const-reference.

void myfunction(const std::string& arg);

To pass a std::string argument to a function accepting a const char* parameter:

void myfunction(const char* arg);

We use the string's .c_str() member function:

myfunction(mystring.c_str());

String manipulation in C++

The std::string type is a class that implements many useful operators and member functions, allowing for easy string manipulation in C++.

Differences between C and C++ - Strings

We also have a dedicated training course called "C++ for C Developers." For more info, contact us at contact@cppsrc.com.

Download the Brochure