increment and decrement operator

This is a discussion on increment and decrement operator within the C Programming forums, part of the General Programming Boards category; i completely new to the world of c programming. i was reading the chapter of operators and expression. i got ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    6

    increment and decrement operator

    i completely new to the world of c programming. i was reading the chapter of operators and expression. i got confused with increment and decrement operators. as......


    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a,y;
    a=3;
    y=++a + ++a + ++a;
    printf("y=%d",y);
    }

    as i think it should give me the output

    y=15 ( because ........4 + 5 + 6=15)

    but it gives me 18

    why? please explain it giving more examples.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *sobs into hands*

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Go to the Search function and search the boards for the key words "order" and "evaluation" and browse a few of the results that pop up. There should be many hits that address this issue.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    It's undefined -- anything can happen when you change a variable two times within one statement. Don't do that.
    If you have any more questions about this, PM Mister C.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    *sobs into hands*
    You fight the flood with buckets. We need a dam. Preferably the one the old board had...
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    > why? please explain it giving more examples.
    Well an example of someone who "didn't get it" is here
    Side_effect: (++x + y*x + y*x) != (y*x + ++x + y*x) ?

    Oh, and while you're at it
    << !! Posting Code? Read this First !! >>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  2. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM
  3. Replies: 11
    Last Post: 08-30-2004, 04:56 PM
  4. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 05:57 AM
  5. int vs BIG INT
    By rumi_red in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21