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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:40:33+00:00 2026-06-11T15:40:33+00:00

In C, when there are variables (assume both as int ) i less than

  • 0

In C, when there are variables (assume both as int) i less than j, we can use the equation

i^=j^=i^=j

to exchange the value of the two variables. For example, let int i = 3, j = 5; after computed i^=j^=i^=j, I have i = 5, j = 3.

However, if I use two int pointers to re-do this, with *i^=*j^=*i^=*j, using the example above, what I have will be i = 0 and j = 3.


In C

1

    int i=3, j=5;
    i^=j^=i^=j; // after this i = 5, j=3

2

    int i = 3, j= 5;
    int *pi = &i, *pj = &j;
    *pi^=*pj^=*pi^=*pj; // after this, $pi = 0, *pj = 5

In JavaScript

    var i=3, j=5;
    i^=j^=i^=j; // after this, i = 0, j= 3

the result in JavaScript makes this more interesting to me

my sample code , on ubuntu server 11.0 & gcc

    #include <stdio.h>
    int main(){
        int i=7, j=9;
        int *pi=&i, *pj=&j;
        i^=j^=i^=j;
        printf("i=%d j=%d\n", i, j);
        i=7, j=9;
        *pi^=*pj^=*pi^=*pj
        printf("i=%d j=%d\n", *pi, *pj);
    }

undefined behavior in c

Will the undefined behavior in c be the real reason leads to this question?

1

code compiled use visual studio 2005 on windows 7 produce the expected result ( Output i = 7, j = 9 twice.)

2

code compiled use gcc on ubuntu ( gcc test.c ) produce the unexpected result ( Output i = 7, j = 9 then i = 0, j = 9 )

3

code compiled use gcc on ubuntu ( gcc -O test.c ) produce the expected result ( Output i = 7,j = 9 twice. )

  • 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-06-11T15:40:35+00:00Added an answer on June 11, 2026 at 3:40 pm

    i^=j^=i^=j is undefined behavior in C.

    You are violating sequence points rules by modifying i two times between two sequence points.

    It means the implementation is free to assign any value or even make your program crash.

    For the same reason, *i^=*j^=*i^=*j is also undefined behavior.

    (C99, 6.5p2) “Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.”

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

Sidebar

Related Questions

Let's say I have two variables, protected_var1 and protected_var2 . Let's further assume that
There are two variables which are required in the tracking pixel which needs to
There are two variables in our database called OB and B2B_OB . There are
Assume that there is a PHP variable: $variable = 'username'; cookie value = 'username'
I have several classes which use static variables. I assume that the classes are
I have a program, and in that program there is some variables (username and
As per the question. In the debug view, there's the Variables frame. It shows
There were many questions about C++ template classes which contain static member variables, as
is there a way to create custom post variables when a user presses submit,
Is there a way to clear all persistent variables in MATLAB functions, while keeping

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.