Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4020842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:16:42+00:00 2026-05-20T10:16:42+00:00

Possible Duplicate: Undefined Behavior and Sequence Points I’m not sure if this is a

  • 0

Possible Duplicate:
Undefined Behavior and Sequence Points

I’m not sure if this is a gcc bug or not, so I’ll ask:

unsigned int n = 0;
std::cout << n++ << n << ++n;

gcc gives the extremely strange result:
“122” which AFAICT is impossible. Because << is left associative, it should be the same as:

operator<<(operator<<(operator<<(std::cout, n++), n), ++n)

and because there is a sequence point before and after evaluating arguments, n is never modified twice (or even accessed) between two sequence points — so it shouldn’t be undefined behaviour, just the order of evaluation unspecified.

So AFAICT valid results would be:
111
012
002
101

and nothing else

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:16:43+00:00Added an answer on May 20, 2026 at 10:16 am

    There is a sequence point between evaluating arguments and calling a function. There is no sequence point between evaluating different arguments.

    Let’s look at the outermost function call:

    operator<<(operator<<(operator<<(std::cout, n++), n), ++n)
    

    The arguments are

    • operator<<(operator<<(std::cout, n++), n)

    and

    • ++n

    It is unspecified which of these is evaluated first. It’s also allowed that the first argument is partially evaluated when the second argument is evaluated.

    From the standard, section [intro.execution] (wording from draft 3225):

    • If A is not sequenced before
      B and B is not sequenced before A, then A and B are unsequenced. [ Note: The execution of unsequenced
      evaluations can overlap. — end note ]

    • Except where noted, evaluations of operands of individual operators and of subexpressions of individual
      expressions are unsequenced. [ Note: In an expression that is evaluated more than once during the execution
      of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be
      performed consistently in different evaluations. — end note ] The value computations of the operands of an
      operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar
      object is unsequenced relative to either another side effect on the same scalar object or a value computation
      using the value of the same scalar object, the behavior is undefined.

    Because you have multiple operations with side effects on the same scalar object which are unsequenced with respect to each other, you’re in that realm of undefined behavior, and even 999 would be a permissible output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: FAQ : Undefined Behavior and Sequence Points Different outputs on different compiler?
Possible Duplicate: Undefined Behavior and Sequence Points In C++ on a machine code level,
Possible Duplicate: Undefined behavior and sequence points What is the value of x after
Possible Duplicate: Detecting an undefined object property in JavaScript Would it be like this?
Possible Duplicate: Undefined, unspecified and implementation-defined behavior I'm trying to deepen my understanding of
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.