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 6540797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:01:28+00:00 2026-05-25T11:01:28+00:00

Possible Duplicate: Compilers and argument order of evaluation in C++ cout << order of

  • 0

Possible Duplicate:
Compilers and argument order of evaluation in C++
cout << order of call to functions it prints?

  1. This:

    int k=3;
    printf("%d %d %d",k++,k,++k);
    

    Gives output as 4 4 4 because they are pushed into the stack as:

    %d%d%d
    
    4  -- for k++    
    4  --for k    
    4  --for ++k
    

    Right?

  2. This:

    int k = 3;
    cout << k++ << k << ++k;
    

    Is actually repeated function calls, so it’s equivalent to:

    ( ( (cout << k++) << k) << ++k);
    

    So, I suppose first of all k++ then k and then ++k must always be executed in this order, right? I believe a function call is a sequence point, but the outputs vary on different implementations. Why is this so?

  • 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-25T11:01:28+00:00Added an answer on May 25, 2026 at 11:01 am

    This is undefined because there is no sequence point between the , in the printf statement. Without a sequence point the compiler is free to order writes to the memory location k as it wills.

    Now you may be wondering ‘what the hell is a seqeunce point’ and why is it relevant? Basically a sequence point is a point in the code where the memory location in question, in this case k has been modified at most once. There is a fuller description here:https://isocpp.org/wiki/faq/misc-technical-issues#double-mod-betw-seq-pt

    As you can see from the FAQ, the , in the printf does not introduce a sequence point.

    In the case of cout this is different because there are 3 function calls to operator >>. A function call introduces a sequence point therefore the modifications to memory location k have a defined order. However (and this was a point I missed but Cubbi pointed out) because C/C++ doesn’t define the order of evaluation of function arguments those arguments, even if they are functions, can be evaluated in any order the compiler defines. So in the expression:

    f(h(), g())

    Whether h() or g() is evaluated first is undefined: http://www.stroustrup.com/bs_faq2.html#undefined. So this is the reason why even in the case of cout you are getting different results from different compilers, basically because the cout << k++ << k << ++k translates to cout.operator<<(k++).operator<<(k).operator(++k) which is effectively an expression like this: f(h(g(cout, k++), k), ++k) and each function argument is evaluated in an unspecified order.

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

Sidebar

Related Questions

Possible Duplicate: Unexpected order of evaluation (compiler bug?) I couldn't predict the output for
Possible Duplicate: Plugging in to Java compilers Edit - this appears to be a
Possible Duplicate: Inline functions in C++ Modern compilers are better than programmers at deciding
Possible Duplicate: Virtual Functions and Performance C++ Is this correct, that class member function
Possible Duplicate: order of evaluation of function parameters Is it safe to use the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Difference between void main and int main? Alright, so I'm using bloodshed
Possible Duplicate: JIT compiler vs offline compilers So until a few minutes ago I
Possible Duplicate: Inadvertent use of = instead of == C++ compilers let you know
Possible Duplicate: Why is this “min” template of cpp-next at fault? From another question

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.