0
What is the output of this code? cout<< 1+"hello"???
Can somebody tell me the answer of this!!
2 Answers
+ 3
That will trigger error being written as it is. Missing semicolon, and the 3 question marks definitely will fail the code.
However, having the code with no question marks and a semicolon, is valid (as follows).
std::cout << 1 + "hello";
String literal such as the "hello" here, is a constant char pointer. Because it is a pointer, we can do pointer arithmetic on it, like the line above, will output 'ello' on screen. Hopefully this figure can help to illustrate.
pointer + 0 1 2 3 4
h e l l o
When you do:
1 + "hello", output => 'ello'
2 + "hello", output => 'llo'
3 + "hello", output => 'lo'
4 + "hello", output => 'o'
Valid index usable for something like this is between zero (first character) and string length - 1 (last character).
Hth, cmiiw
+ 2
Hi. You could try it in code playground. I think it will raise an error.