PREDICT THE OUTPUT

# include<iostream>
using namespace std;


int main()
{
    int a=5;
   
    cout<<++a<<" "<<a++<<endl;
}

How o/p is 7 5?

1 Like

its is 7 and 5 because when u do cout<<++a<<" " <<a++<<endl;
it starts reading characters from right side i.e so first it will encounter endl and like we do string concatenation it will take the command of next line then as we are using <<(left shift operator) after that it will encounter a++ so accordingly it will read this and push 5 to left and update a to 6 then it will similarly when it comes to ++a the previous value was 6 which should be updated and it becomes 7 then the whole thing i.e β€œ7 5” is pushes to the console output

3 Likes

<< -> this is insertion operator

Who says it is? :slight_smile:

[simon@simon-laptop][17:01:50]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling rioji-blah.cpp
+ g++ -std=c++14 rioji-blah.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
rioji-blah.cpp: In function β€˜int main()’:
rioji-blah.cpp:9:22: warning: operation on β€˜a’ may be undefined [-Wsequence-point]
     cout<<++a<<" "<<a++<<endl;
                     ~^~
+ set +x
Successful
[simon@simon-laptop][17:01:52]
[~/devel/hackerrank/otherpeoples]>./a.out 
6 6

Further reading:

5 Likes

due to a++ having heigher precedence a++ it will first get executed making the total value 6 and the ++a will get executed making overall value 7 .while printing the output since the value assigned to a++ is still 5 it will print 5 and the value to ++a is 7 it will print 7

I think you have written something wrong. It should have been a++ << " " << ++a
Then the output will come 5 7. Else 6 6.
Thank you…

 I am getting output  6 6!
           can anyone explain me?

it is an overloaded operator of left shift(<<)