Lvalue required as increment operand

This is a discussion on Lvalue required as increment operand within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int iVar; //line 1 iVar = 20; //line 2 ++iVar; //line 3 printf( "\niVar ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    79

    Lvalue required as increment operand

    Code:
    #include <stdio.h>
    
    int main(void) {
    	
    	int iVar; //line 1
    	
    	iVar = 20; //line 2
    	
    	++iVar; //line 3
    	
    	printf( "\niVar : %d\n", iVar ); // line 4
    	
    	iVar--; // line 5
    	
    	printf( "\niVar : %d\n", iVar ); // line 6
    	
    	iVar + ++iVar--; //line 7
    	
    	printf( "\niVar : %d\n", iVar ); // line 8
    	
    	++( iVar + iVar ); // line 9
    	
    	printf( "\niVar : %d\n", iVar ); // line 10
    	
    	return 0; // line 11
    }
    What's the problem?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    What exactly are you trying to do with these two statements?
    Code:
    iVar + ++iVar--;
    Code:
    ++( iVar + iVar );
    The error message is basically saying that you are trying to increment something that cannot be assigned to, e.g., it is the temporary that resulted from another operation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    This code is pretty much simple arithmetic since my teacher is trying to teach us how to use operators. I don't get why it doesn't add together... Do those statements not work at all?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by .C-Man.
    This code is pretty much simple arithmetic since my teacher is trying to teach us how to use operators. I don't get why it doesn't add together... Do those statements not work at all?
    They do not work because the increment and decrement operators change the value of their operands. Hence, it is futile to try and use them on things that cannot change, including the result of operations like addition. It is akin to adding 3 and 4 to get 7, adding 1, and then trying to assign 8 to 7.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    That really helps, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes and objects
    By GoldenBoy in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2010, 07:28 AM
  2. Replies: 3
    Last Post: 06-01-2010, 06:22 AM
  3. lvalue required as left operand of assignment (??)
    By oospill in forum C Programming
    Replies: 3
    Last Post: 11-03-2009, 10:02 PM
  4. lvalue required as increment operand compile error
    By canadatom in forum C Programming
    Replies: 8
    Last Post: 06-13-2009, 11:49 AM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM

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