0
cout<<"ccccc"+2;

Output:

ccc

I tried searching for it online and I know it is a very dumb question but couldn't find anything anywhere. Please if someone could help me out.

VLL
  • 9,634
  • 1
  • 29
  • 54
  • duplicates: [Why in the code "456"+1, output is "56"](https://stackoverflow.com/q/28957950/995714), [Why can you add an integer to a string literal?](https://stackoverflow.com/q/25299156/995714) – phuclv Jan 31 '22 at 10:09
  • 2
    Does this answer your question? [Why in the code "456"+1, output is "56"](https://stackoverflow.com/questions/28957950/why-in-the-code-4561-output-is-56) – BoP Jan 31 '22 at 10:30

2 Answers2

7
"ccccc"+2;

"ccccc" decays to the const char * pointer referencing the first character of the string literal "ccccc". When you add 2 to it, the result references the third element of the string literal.

It is the same as:

const char *cptr = "ccccc";

cptr += 2;

cout << cptr;
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 2
    In C++ `"ccccc"` decays to a `const char *`. `char *cptr = "ccccc";` should give a compile time error. – mch Jan 31 '22 at 09:57
  • there is no need to use a pointer. `"cccc"` is a `char[5]`. I suggest to use `const char c[] = "cccc";`. This would also more closely resemble what is happing in OPs code where the decay to pointer happens due to `+ 2` – 463035818_is_not_an_ai Jan 31 '22 at 10:03
  • @463035818_is_not_a_number The code is only to illustrate what is going on. – 0___________ Jan 31 '22 at 10:06
  • yes and showing `const char c[] = "cccc"; const char* cptr = c+2;` is more closely resembling what is going on, the decay is triggered by `+2` in OPs code – 463035818_is_not_an_ai Jan 31 '22 at 10:07
  • @463035818_is_not_a_number not exactly as it creates the new array and it does not happen in the original code. cptr represents decayed pointer referencing directly the string literal – 0___________ Jan 31 '22 at 10:10
  • @463035818_is_not_a_number that's not true, `"ccccc"` decays to a pointer even without the `+ 2`. There is no `operator<<(const char (&)[N])` function, only a `operator<<(const char *)` function. – mch Jan 31 '22 at 10:20
  • 1
    @mch indeed, but in OPs code is does decay due to `+2`, thats all im trying to say – 463035818_is_not_an_ai Jan 31 '22 at 10:20
1

When you wrote:

cout<<"ccccc"+2;

The following things happen(to note here):

  1. "ccccc" is a string literal. In particular, it is of type const char[6].

  2. Now, this string literal decays to a pointer to const char which is nothing but const char* due to type decay. Note that the decayed const char* that we have now is pointing to the first character of the string literal.

  3. Next, 2 is added to that decayed pointer's value. This means that now, after adding 2, the const char* is pointing to the third character of the string literal.

  4. The suitable overloaded operator<< is called using this const char*. And since this const char* is pointing to the third character of the string literal, you get the output you observe.

Jason
  • 36,170
  • 5
  • 26
  • 60