0

code :

#include <iostream> 
using namespace std;
int main() 
{ 
    int i = 1;
    cout << "hello " << i  << " end" ;
    return 0; 
} 

OUTPUT : hello 1 end
this works fine but

#include <iostream> 
using namespace std;
int main() 
{  
    int i = 1;
    cout << "hello " , i  , " end" ;
    return 0; 
} 

OUTPUT: hello
why the above terminates with printing hello only?

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82

1 Answers1

2
std::cout << "hello " , i  , " end" ;

Because , has a lower operator precedence than <<, this will be evaluate to:

(std::cout << "hello "), i  , " end" ;

First, (std::cout << "hello ") will be evaluate, printing hello .

Second, i would get evaluate. The value i will be return for the whole expression (std::cout << "hello "), i.

Third, " end" would get evaluate. The value " end" will be return for the whole expression ((std::cout << "hello "), i ), " end" ;