1

I am converting a character to a string by concatenating it with an empty string (""). But it results in undefined behaviour or garbage characters in the resultant string. Why so?

char c = 'a';
string s = ""+c;
cout<<s<<" "<<s.size()<<"\n";
12
  • 5
    "" is not a string Commented May 20, 2022 at 17:20
  • 1
    Try string s = ""s + c; (unless you're truly limited to C++11). Then read this. Commented May 20, 2022 at 17:25
  • 2
    It is a string literal, but the type of "" is const char[1], not std::string. The naming is a bit confusing, I admit. Commented May 20, 2022 at 17:26
  • 1
    Use +=, '+' does not work as such operator is not defined for char. Commented May 20, 2022 at 17:27
  • 4
    @AryanAgarwal Well, this is why you should never use other languages as a model in writing C++ code. Not only that, this looks totally weird to a C++ programmer (that line of code). That was the red flag -- this looks totally fine to a JavaScript programmer, but is totally alien to a C++ programmer who never used those other languages. Commented May 20, 2022 at 17:47

2 Answers 2

1

Let's look at your snippet, one statement or a line at a time.

char c = 'a';

This is valid, assigning a character literal to a variable of type char.
Note: since c is not changed after this statement, you may want to declare the definition as const.

string s = ""+c;

Let's refactor:
std::string s = ("" + c);

Let's add type casting to make the point more clear:
std::string s = ((const char *) "" + (char) c);

The order of operations is resolve all expressions on the right hand side (rhs) of the assignment operator before assigning to the string variable.

There is no operator+() in C++ that takes a const char * and a single character.
The compiler is looking for this function: operator+(const char *, char).
This is the primary reason for the compilation errors.

cout<<s<<" "<<s.size()<<"\n";
The string assignment and creation failed, thus s is empty and s.size() is zero.

Sign up to request clarification or add additional context in comments.

3 Comments

There is no operator+() in C++ that takes a const char * and a single character. but there is one that takes a pointer and integer. There are no compiler errors here, just UB.
So, the compiler tries to find the "best match" for the operator, but realizes that (char) c can be converted to an int and applies the operator+(char *, int).
@ThomasMatthews -- there's no "best match" involved; "best match" is the algorithm for choosing one of several viable overloads of a function. That's not what's going on here; it's straight C, with a pointer and an integer value.
0

An interesting issue, which looks like an issue which the compiler does not detect at compilation.
I have tried this with GNU C++ 10.2 on Cygwin and your code snippet generates this.

0200312 (Fedora Cygwin 9.3.0-1) 31

if you replace char c = 'c'; with char c = '\0'; the output is

 0

So looks like some kind of memory violation issue.

As you probably know, the standard method of appending a char is to use push_back

string s = "";
s.push_back(c);
cout << s << " " << s.size() << "\n";

Alternatively you can use the operator +=

string s = "";
s += c;
cout << s << " " << s.size() << "\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.