+ 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.
4 Answers
+ 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".
+ 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.
+ 1
is it means cout expect a char pointer as input?
+ 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.