+ 3

What happens when we add number to string and pass to the cout?

I am adding 4 to the string "abcde" and printing it. like cout<<"abcde"+4; it is giving output as e. I don't understand what is happening. please explain me.

14th Mar 2017, 2:44 PM
Shankaranarayana Barre
Shankaranarayana Barre - avatar
4 ответов
+ 12
When you do that, you are telling the program to print the string starting from the character with index 4, i.e. 'e'. (Recall that string is character array and arrays start with index 0). If you replace the number with 3, you will get output "de".
14th Mar 2017, 2:47 PM
Hatsy Rei
Hatsy Rei - avatar
+ 12
"abcde" type is const char*, and is treated as dealing with pointers. While strings are character arrays, strings with double quotations are a tad different in the sense that they are pointers to constant character sequences.
14th Mar 2017, 3:36 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
is it means cout expect a char pointer as input?
14th Mar 2017, 3:04 PM
Shankaranarayana Barre
Shankaranarayana Barre - avatar
+ 1
When you print a string + x (a numeral) the string from the xth position is printed. (Remember that the indices of the array start from 0.
27th Mar 2017, 7:52 AM
Vishnu ks
Vishnu ks - avatar