Why it is giving different output? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Why it is giving different output?

Hello world program in c++ #include <iostream> using namespace std; int main() { cout << "Hello world!"; >>>>Hello world return 0; } #include <iostream> using namespace std; int main() { cout << 'Hello world!'; >>>>>>1919706145 return 0; }

27th Sep 2019, 3:17 PM
Amarnath
Amarnath - avatar
3 Réponses
+ 1
It's giving different output because On 1st you are printing hello world in double quotes so it will print you message as "hello world" But in 2nd case you are not printing hello world you are getting address on which hello world string is written because you put hello world in single quotes, that's why.
27th Sep 2019, 4:13 PM
Chirag Kumar
Chirag Kumar - avatar
+ 1
I don't know anything I am just playing by python influence on c++
27th Sep 2019, 4:29 PM
Amarnath
Amarnath - avatar
+ 1
As the others have mentioned, double quotes are used for string literals while single quotes are used for character literals. As per the C99 standard 6.4.4.4 p10 states that it is implementation defined: "An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int." http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf It appears that the sololearn implementation does something along the lines of: sum of 256^(l-i)*c[i] for i = l to 1 step -1 where l = length of literal c[i] = integer representation of char at position i in the literal but because the result is an unsigned int, overflow can happen if the length of the literal > 4. It can also be seen as a base 256 number!
27th Sep 2019, 4:48 PM
jtrh
jtrh - avatar