+ 13

About cout

Cout<< 1+"hello"; Why the output is "ello"? Cout<<2+ "hello"; Why the output is "llo"? Why do we see only the pieces of the word "hello"? What does cout return with numbers?

9th Feb 2019, 4:52 PM
Белка которая держит цветок
Белка которая держит цветок - avatar
20 Respostas
+ 22
Strings are arrays of characters. And arrays are implemented with pointers. Which means that what you do here is add 1 or 2 to the address of the array, and since sizeof(char) is 1, you will skip as many characters.
9th Feb 2019, 5:23 PM
Zen
Zen - avatar
+ 5
Additionally, this approach will not work if you use it directly on a std::string object, because it is more than a simple char array. However, the same effect is possible through the use of the `c_str` or `data` member of the std::string object, which exposes the internal buffer content.
10th Feb 2019, 1:32 AM
Ipang
+ 5
~ swim ~ That's right 👍 But we can't do that on the string object, only with the .c_str or .data which exposes the internal buffer : )
11th Feb 2019, 6:02 PM
Ipang
+ 5
Though I could've been more clear if I said "this will not work if you use a std::string object directly, ..." I will make adjustment on my first post, Thanks ~ swim ~ for the note 😁
11th Feb 2019, 6:20 PM
Ipang
+ 3
If you added the 1+ at the beginning or end of the string (ex. cout << 1+"hello"), it returns "ello" because the 1+ removes a letter from the beginning of the string. The second experiment you made has removed 2 letters from the beginning of the string. So, the output will be a whole string, but a number of characters at the beginning of the string will be removed.
9th Feb 2019, 5:05 PM
EoflaOE
EoflaOE - avatar
0
What is string object
8th Mar 2019, 8:00 AM
SMSajid
SMSajid - avatar
0
Привет
30th Aug 2019, 10:42 AM
Rahim pro
0
Есть русский
30th Aug 2019, 10:43 AM
Rahim pro
0
I think you need to write " in front of 1, i try it and works
24th Sep 2019, 8:38 AM
Islam-borcha
Islam-borcha - avatar
0
Hello
24th Sep 2019, 6:55 PM
Rubal J Suarez
Rubal J Suarez - avatar
0
Разобрался?
29th Sep 2019, 3:44 PM
Геннадий Кузнецов
Геннадий Кузнецов - avatar
0
Right answer: cout<<1<<"hello";
24th Jan 2020, 5:03 PM
Софьян Валерий
0
Hello
17th May 2020, 11:24 AM
Amir
Amir - avatar
0
По
19th Nov 2020, 3:12 PM
Kazakbaeva Sabina
0
Hlo sir
10th Sep 2021, 11:07 AM
UnGraduate highlites
0
Hey how to learn programming may i ask anyone who can me
13th Oct 2021, 2:47 PM
Petermafugesarne Ryan
0
Helo . How to make game and launch on playstore
8th Jan 2022, 10:33 AM
Rai star
0
cout <<"my name is mohamed Mostafa I need learn C++" ;
29th Jan 2022, 12:39 PM
mohamed mostafa
0
I love C++ programeble
29th Jan 2022, 12:41 PM
mohamed mostafa
0
Gay
31st Jan 2022, 6:25 PM
Marco Ayala
Marco Ayala - avatar