3

This is my program! I want to know the reason behind such output.

#include <iostream>
using namespace std;

class A{
  public:
      void fun(int i){
          cout<<"Hello World" + i<<endl;
      }
};

int main()
{
  A obj1;
  obj1.fun(2);

  return 0;
}

Expected Output : Hello World2

Actual Output : llo World

PS:To print "HelloWorld2", I can also code cout<<"Hello World"<< i

reverie_ss
  • 2,396
  • 23
  • 30
  • *Expected Output : Hello World2* -- You're looking for Java, not C++. -- *I can also code cout<<"Hello World"<< i* -- Which is the proper way to do this in C++. – PaulMcKenzie Dec 04 '16 at 14:36
  • since C++14 you can use [`"Hello World"s + i`](http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s). In C++11 similar thing can be done with [user-defined literal](http://en.cppreference.com/w/cpp/language/user_literal) – phuclv Dec 04 '16 at 14:37
  • 1
    @LưuVĩnhPhúc it should be `"Hello Worls"s + std::to_string(i)` to not be UB. – StoryTeller - Unslander Monica Dec 04 '16 at 14:39

4 Answers4

6

"Hello World" is not std::string, it's a string literal so it's type is const char[]. When adding an integer like you're doing with i here you're actually creating a temporary const char* that first points to the first element of the const char[] which is 'H', then you move it 2 spots (because i is 2) so it points to 'l', then you pass that pointer to cout and thus cout starts printing from 'l'. Doing binary operations on such types is called Pointer Arithmetic.

To get a better understanding using an example char array, your code under the hood is similar to:

const char myHello[] = "Hello World";
const char* pointer = myHello; // make pointer point to the start of `myHello`
pointer += i; // move the pointer i spots
cout<< pointer <<endl; // let cout print out the "string" starting at the character pointed to by the new pointer.

Note that if you try to "move" the pointer too much so that it's pointing to something out of the string and then try to access this pointer you get Undefined Behaviour. Same as how accessing an array out of bounds is UB. Just make sure index < size.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
2

"Hello World" + i doesn't do string concatenation. It does pointer arithmetic.

It takes the address of the c-string literal "Hello World", let's call it a. And then adds to it a + i. Dereferencing the resulting pointer is undefined behavior when i is larger than the length of the c-string literal.

If it's within bounds, however, you'd get an address inside the literal, which will appear as a suffix when printed. However, since a c-string literal is const, attempting to write into that address is UB again.

Long story short, don't do it.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
1

"Hello World" is compiled to an array of characters and is passed to operator<< as a pointer to those characters. Then you add i to the pointer, which moves the pointer on that many characters.

IanM_Matrix1
  • 1,564
  • 10
  • 8
1

Pointer Arithmetic.

"Hello World" is a string litteral, pointed to implicitly by a const char*.

Adding an integer i to a pointer will move it i positions forward (or backward if i<0).

Hence the result of "Hello World" + 2 is a const pointer (const char*) indicating the address of the third letter in the string.

A.S.H
  • 29,101
  • 5
  • 23
  • 50