Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #26617

Re: Swapping logic

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: Swapping logic
Date 2012-09-23 18:19 +0000
Organization Aioe.org NNTP Server
Message-ID <20120923111150.32@kylheku.com> (permalink)
References <49f36538-d5ca-411c-bc58-1f17b55911b6@s8g2000vbc.googlegroups.com>

Show all headers | View raw


On 2012-09-23, ZUBAIR TRALI <bhat.zubair789@gmail.com> wrote:
> #include<stdio.h>
> /*new logic for swapping*/
> #include<conio.h>
> void main()
>  {
>    int x=20,y=30;
> printf("nos before swapping are %d and %d\n",x,y);
>     x=x*y/(y=x);

The problem here is that you're accessing y in an expression where y is
modified.  That is undefined behavior.

More precisely, your accessing y for a purpose other than determining a new
value to be stored into y. 

The following also accesses y in an expression where it is modified:

  y = y + 1;

However, the access in "y + 1" is a necessary operand for computing the value
stored in the assignment. This imposes an order, which makes it well defined.

This one is not defined:

  y + (y = 3);

The y on the left of the + is accessed, but that access is not necessary for
the y = 3 assignment on the right. Because it is not necessary, it does not
have to be ordered relative to the assignment, and so we do not know when that
access occurs: before, during or after the completion of assignment to y?

If you write such ambiguous expressions in your C programs, they will be
nonportable between compilers and possibly unreliable even on one compiler.
For instance, if you change some code-generation options such as optimization
settings, the behavior might change.

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Swapping logic ZUBAIR TRALI <bhat.zubair789@gmail.com> - 2012-09-23 05:48 -0700
  Re: Swapping logic Richard Damon <news.x.richarddamon@xoxy.net> - 2012-09-23 09:04 -0400
  Re: Swapping logic Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-23 09:15 -0400
    Re: Swapping logic Noob <root@127.0.0.1> - 2012-09-24 12:56 +0200
  Re: Swapping logic Casey Carter <Casey_at_Carter_dot_net@dontspam.me.invalid> - 2012-09-23 11:35 -0500
    Re: Swapping logic Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-23 14:19 -0400
      Re: Swapping logic Nobody <nobody@nowhere.com> - 2012-09-24 10:57 +0100
      Re: Swapping logic Andrew Smallshaw <andrews@sdf.lonestar.org> - 2012-09-26 05:20 +0000
        Re: Swapping logic Gareth Owen <gwowen@gmail.com> - 2012-09-26 06:46 +0100
          Re: Swapping logic David Brown <david@westcontrol.removethisbit.com> - 2012-09-26 08:16 +0200
            Re: Swapping logic Gareth Owen <gwowen@gmail.com> - 2012-09-26 17:51 +0100
  Re: Swapping logic Kaz Kylheku <kaz@kylheku.com> - 2012-09-23 18:19 +0000

csiph-web