small problem !!!

Discussion in 'C Programming' started by Rahul, May 23, 2007.

  1. Rahul

    Rahul Guest

    hi ,
    i cant understand whats wrong with the following piece of code:-

    main()
    {
    ..
    a=i++;
    ..
    ..
    }

    Well it works fine with my compiler. The old value of i is assigned to
    a and then it gets incremented.
    Can any one please help me out?
    thanks.
     
    Rahul, May 23, 2007
    #1
    1. Advertising

  2. Rahul

    Clark Cox Guest

    On 2007-05-23 07:25:50 -0700, Rahul <> said:

    > hi ,
    > i cant understand whats wrong with the following piece of code:-
    >
    > main()
    > {
    > .
    > a=i++;
    > .
    > .
    > }
    >
    > Well it works fine with my compiler. The old value of i is assigned to
    > a and then it gets incremented.


    It just happens to work fine on your platform. You have no guarantee
    whether the new value gets stored to i before or after the location of
    a is calculated. They could even happen simultaneously.

    Just don't do it.

    > Can any one please help me out?
    > thanks.



    --
    Clark S. Cox III
     
    Clark Cox, May 23, 2007
    #2
    1. Advertising

  3. Rahul <> wrote:

    > i cant understand whats wrong with the following piece of code:-


    > main()


    int main(void)

    > a=i++;


    You've managed to ask FAQ 3.1 verbatim.

    http://c-faq.com/expr/evalorder1.html

    Please check the FAQ before asking more tiresome questions.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, May 23, 2007
    #3
  4. Clark Cox <> writes:
    > On 2007-05-23 07:25:50 -0700, Rahul <> said:
    >> i cant understand whats wrong with the following piece of code:-
    >> main()
    >> {
    >> .
    >> a=i++;
    >> .
    >> .
    >> }
    >> Well it works fine with my compiler. The old value of i is assigned
    >> to
    >> a and then it gets incremented.

    >
    > It just happens to work fine on your platform. You have no guarantee
    > whether the new value gets stored to i before or after the location of
    > a is calculated. They could even happen simultaneously.


    It's worse than that. The statement invokes undefined behavior (as
    the FAQ explains; see 3.1 and 3.9).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 23, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learning SQL Server

    Re: DataGrid binding... small problem... help please

    Learning SQL Server, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    320
    Steven
    Aug 7, 2003
  2. Steven
    Replies:
    2
    Views:
    315
    Jeff Adkins
    Aug 7, 2003
  3. Simon Harvey

    A small problem with page validation

    Simon Harvey, Apr 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    338
    Simon Harvey
    Apr 6, 2004
  4. =?Utf-8?B?Q2hpV2hpdGVTb3g=?=

    small problem with ASP 1.1 version problem in IIS6

    =?Utf-8?B?Q2hpV2hpdGVTb3g=?=, Apr 2, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    395
    =?Utf-8?B?Q2hpV2hpdGVTb3g=?=
    Apr 2, 2007
  5. Singeo
    Replies:
    3
    Views:
    384
    Singeo
    May 27, 2008
Loading...

Share This Page