Thread: Increment / Decrement Operators - Help

  1. #1
    ..................
    Join Date
    Oct 2005
    Posts
    11

    Exclamation Increment / Decrement Operators - Help

    Hi All,


    I have an small doubt in using Increment / Decrement operators....

    Program is :
    Code:
    1)   #include<stdio.h>
    2)   main()
    3)  {
    4)     int i=10;
    5)     printf("\nValue of I is :%d",++(--i));
    6)     getch();
    7)  }
    Compilation Error:
    at line 5, Error prompting stating "L value required".

    Is the syntax wrong or the usage of the Increment / Decrement operators is worng....

    Best Regards,
    Shyamlal SVS.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    The usage is wrong, incrementing and decrementing returns an R value.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Since the incrementing and recrementing are implemented as single assembly instructions(inc/dec), you cannot use them at the same time. Also, ++(i-2) will not work!

    See the assembly output of an increment statement.

    Code:
            movl    $10, -4(%ebp) <-- i=10
            leal    -4(%ebp), %eax 
            incl    (%eax)  <-- here is the i++

  4. #4
    ..................
    Join Date
    Oct 2005
    Posts
    11
    Code:
    The usage is wrong, incrementing and decrementing returns an R value.
    Cud u explain me what is R value?????

  5. #5
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by shyam168
    Cud u explain me what is R value?????
    L-values are values that have addresses, meaning they are variables or dereferenced references to a certain place.
    R-value is either l-value or non-l-value — a term only used to distinguish from l-value.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    An l-value is a value that can be used on the left side of an assigment operator. An r-value is a value that can be used on the right side of an assignment operator:
    Code:
    l-value = r-value;
    Code:
    ++(--i)
    The expression highlighted in red yields the value 9. Obviously, this can not be used as as an l-value.

    The ++ operator, highlighted in blue, requires an l-value because it assigns a value to the value it is used on. Trying to use it on the value 9 would look like this:
    Code:
    /* 9 is not an l-value. This code won't work. */
    9 = 9 + 1;

  7. #7
    ..................
    Join Date
    Oct 2005
    Posts
    11
    Thanks to one and all forn clearing my doubt.....


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Does Increment Operators Work...??
    By ajayd in forum C Programming
    Replies: 37
    Last Post: 12-31-2008, 10:01 AM
  2. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  4. Replies: 11
    Last Post: 08-30-2004, 03:56 PM
  5. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM