pre-decrement and post-increment issue [duplicate]










0
















This question already has an answer here:



  • Undefined behavior and sequence points

    5 answers



  • Unexpected order of evaluation (compiler bug?) [duplicate]

    3 answers



Hi I am currently studying for my first exam in C++ programming and I am having this issue:



int x = 12;

int y = 2;

cout << x++ <<" "<< --x; //Output: 11 12


I expected the output to be: 11 11, because 'cout' goes from right to left so the pre-decrement on x (--x) should decrement the value of x, and x++ wouldn't print out 12 because it is post-decrement. Is this an example of undefined behaviour?



cout << x++ << " " << --y; //Output: 12 1


While this line outputs the expected value of x.



Thanks in advance.










share|improve this question













marked as duplicate by CoryKramer c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 15:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    "because 'cout' goes from right to left" nope, because you don't have any sequence points between those operations

    – CoryKramer
    Nov 15 '18 at 15:25















0
















This question already has an answer here:



  • Undefined behavior and sequence points

    5 answers



  • Unexpected order of evaluation (compiler bug?) [duplicate]

    3 answers



Hi I am currently studying for my first exam in C++ programming and I am having this issue:



int x = 12;

int y = 2;

cout << x++ <<" "<< --x; //Output: 11 12


I expected the output to be: 11 11, because 'cout' goes from right to left so the pre-decrement on x (--x) should decrement the value of x, and x++ wouldn't print out 12 because it is post-decrement. Is this an example of undefined behaviour?



cout << x++ << " " << --y; //Output: 12 1


While this line outputs the expected value of x.



Thanks in advance.










share|improve this question













marked as duplicate by CoryKramer c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 15:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    "because 'cout' goes from right to left" nope, because you don't have any sequence points between those operations

    – CoryKramer
    Nov 15 '18 at 15:25













0












0








0









This question already has an answer here:



  • Undefined behavior and sequence points

    5 answers



  • Unexpected order of evaluation (compiler bug?) [duplicate]

    3 answers



Hi I am currently studying for my first exam in C++ programming and I am having this issue:



int x = 12;

int y = 2;

cout << x++ <<" "<< --x; //Output: 11 12


I expected the output to be: 11 11, because 'cout' goes from right to left so the pre-decrement on x (--x) should decrement the value of x, and x++ wouldn't print out 12 because it is post-decrement. Is this an example of undefined behaviour?



cout << x++ << " " << --y; //Output: 12 1


While this line outputs the expected value of x.



Thanks in advance.










share|improve this question















This question already has an answer here:



  • Undefined behavior and sequence points

    5 answers



  • Unexpected order of evaluation (compiler bug?) [duplicate]

    3 answers



Hi I am currently studying for my first exam in C++ programming and I am having this issue:



int x = 12;

int y = 2;

cout << x++ <<" "<< --x; //Output: 11 12


I expected the output to be: 11 11, because 'cout' goes from right to left so the pre-decrement on x (--x) should decrement the value of x, and x++ wouldn't print out 12 because it is post-decrement. Is this an example of undefined behaviour?



cout << x++ << " " << --y; //Output: 12 1


While this line outputs the expected value of x.



Thanks in advance.





This question already has an answer here:



  • Undefined behavior and sequence points

    5 answers



  • Unexpected order of evaluation (compiler bug?) [duplicate]

    3 answers







c++ increment post-increment decrement pre-increment






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 15:24









Mirsad HasicMirsad Hasic

1




1




marked as duplicate by CoryKramer c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 15:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by CoryKramer c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 15:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    "because 'cout' goes from right to left" nope, because you don't have any sequence points between those operations

    – CoryKramer
    Nov 15 '18 at 15:25












  • 1





    "because 'cout' goes from right to left" nope, because you don't have any sequence points between those operations

    – CoryKramer
    Nov 15 '18 at 15:25







1




1





"because 'cout' goes from right to left" nope, because you don't have any sequence points between those operations

– CoryKramer
Nov 15 '18 at 15:25





"because 'cout' goes from right to left" nope, because you don't have any sequence points between those operations

– CoryKramer
Nov 15 '18 at 15:25












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto