lvalue required??

Discussion in 'C' started by IndiraP, Nov 17, 2012.

  1. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    Code:
    # define TWICE(i) 2*i 
    # define TWO(i) i+i 
    main () 
    { 
         int no, sum, product; 
         no = 1; 
         sum = -- TWICE(no); 
         --sum; 
         product = --TWo(no); 
         printf (ā€œ%d%d\nā€, sum, product); 
    } 
    
    y am i getting an error stating lvalue required at --TWICE(no); above??
    wat does it mean???
     
    Last edited by a moderator: Nov 18, 2012
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    It means you aren't using #defines and the predecrement operator correctly.

    --TWICE(no) expands to --2*no and you can't predecrement a constant.
    However, in the case of TWICE, even brackets don't fix the problem because --(2*no) still won't work because 2*no is not an lvalue. An lvalue ("left value") is something you can put on the left of an assign, so in i=5, i is the lvalue and this is valid because you can assign a value to it. 5=i is invalid because you can't assign a value to 5. (2*no)=7 is invalid because you can't assign a value to (2*no).

    --TWO(no) will "work" - in the sense that it won't throw an error, but you're still abusing the -- operator. This will expand to --i+i which gives undefined results.
     
  3. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    Thank u for giving me a clear view..:)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice