Hello everyone I am very new to c++ and I have a question regarding a rather random and unimportant program I have written. Here is the program itself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
int add() {
std::cout<<"hello";
enum {first,second,third};
return (first+second+third);
}
int main () {
short funadd;
short retval;
funadd=(add()+5);
retval=(funadd +5);
std::cout<<add();
std::cout<<funadd;
return retval;
}
Now my question is that the output of the program is 'hellohello38', but shouldn't it be 'hello3hello8'? I find if i move 'std::cout<<funadd;' right below the variable declaration of funadd itself it gives the output 'hello3hello8' but why is that so? Any answers would be greatly appreciated
This is because in line 12, you excecute the add() function before performing the cout. Therefore it'll print hello before 3.
Simplify the code to this:
1 2 3 4 5 6 7 8 9
int out() {
std::cout << "Hello";
return 1;
}
int main()
{
std::cout << out();
return 0;
Hello1
You'll see that out() returns 1 AFTER it outputs Hello.