Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does C++ interpret a cout with a '+' in it?

Tags:

c++

cout

I've been moving back and forth with Java/C++ so I messed up with my console output and accidentally wrote lines like:

cout << "num" + numSamples << endl;
cout << "max" + maxSampleValue << endl;

Each of which gave me bits and pieces of other strings I had in my program. I realize my mistake now, but what was C++ interpreting those lines as so that they output different parts of different strings in my program?

like image 247
GuitarStrum Avatar asked Apr 17 '15 22:04

GuitarStrum


People also ask

What does the C in cout mean?

The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

What is the meaning of << in C++?

Author has 130 answers and 402.2K answer views 3y. << have two meaning one is left shift operator used in most of programming language which shift the bit left in specified number of time. and other << is use C++ for output insertion operator with cout object this mechanism called operator overloading in C++. 22.

What does cout and cin mean in C?

cin is an object of the input stream and is used to take input from input streams like files, console, etc. cout is an object of the output stream that is used to show output. Basically, cin is an input statement while cout is an output statement. They also use different operators.

What is the difference between printf and cout in C++?

Printf is a function which needs parameters and format specifiers (%d for int). Cout is stream oriented output stream object where you do not need any format specifiers. printf returns an integer value (the number of characters actually printed) and cout does not return anything.


4 Answers

There is a simple reason for this:

"num" and "max" are string literal. Their type is const char *. Assuming numSamples is an integer, what you are doing is is pointer arithmetic.

You are basically printing a string that points to "num" + numSamples bytes.

If you did cout << "num" + 1 << endl this would print "um".

You probably figured it out, but the correct way to do this is: cout << "num" << numSamples << endl;

Also, you asked:

But what was C++ interpreting those lines as so that they output different parts of different strings in my program?

As stated before, "num" is a string literal. Generally string literals sits in the same place in the binary program: .rodata. All other string literals sits in this region, so when you are advancing your pointer by a certain amount of bytes, you will likely points to some other string literals, thus printing (part of) them.

like image 61
Xaqq Avatar answered Oct 17 '22 03:10

Xaqq


It is just pointer arithmetic. For instance,

cout << "num" + 1 << endl;

would print a string staring from ((address of)"num" + 1), i.e. "um".

If the value we add exceeds the length of the string, then we have undefined behavior.

like image 26
AlexD Avatar answered Oct 17 '22 03:10

AlexD


Keep in mind that although iostreams overload << (and >>) to do I/O, these were originally defined as bit-shift operators. When the compiler's parsing the expression, it's basically seeing "operand, operator, operand", then checking whether the operands are of types to which that operator can be applied.

In this case, we have a string literal and (apparently) some sort of integer, and the compiler knows how to do math on those (after converting the string literal to a pointer).

From the compiler's viewpoint, however, this isn't a whole lot different from something like:

int a, b=1, c=2, d = 3;

a = b << c + d;

The difference is that with operands of type int, the meaning is fairly obvious: it's doing addition and bit-shifting. With iostreams, the meaning attached to << and >> change, but the syntax allowed for them in an expression is unchanged.

From the compiler's "viewpoint" the only question is whether the operands to + are of types for which + is allowed--in this case, you have pointer to char + integer, so that's allowed. The result is a pointer to char, and it has an overload of << that takes a left-hand operand of type ostream and a right-hand operator of type pointer to char, so the expression as a whole is fine (as far as it cares).

like image 3
Jerry Coffin Avatar answered Oct 17 '22 03:10

Jerry Coffin


"num" type is char const [4]. If numSamples is integer, "num" + numSamples type is const char *. So you call operator << cor std::cout, that overloaded for const char * and prints string that starts from address addres("num") + numSamples in pointer arithmetic.

Try this:

cout << "num" + std::to_string(numSamples) << endl;
cout << "max" + std::to_string(maxSampleValue) << endl;

std::to_string() function you can find in <string>

like image 2
gomons Avatar answered Oct 17 '22 03:10

gomons