Error: invalid lvalue in assignment [in c]

10,131

Solution 1

If you have closer look to your compiler error you can see that, it say's

error: invalid lvalue in assignment

So to better understand this error you have to understand what is lvalue and rvalue. An lvalue refers to an object that persists beyond a single expression. You can think of an lvalue as an object that has a name. On the other side an rvalue is a temporary value that does not persist beyond the expression that uses it.

So if you look code line which causing this issue

width * length = totalArea_single;

So while storing any value/data you need to have lvalue on left hand side of = operator. While width * lenght generates a rvalue which can't store any value, in simple words you can say that to store any value you need lvalue. That's why you need to change this statement to

totalArea_single = width * length;

For more details about lvalue and rvalue please refer to this SO Link.

Solution 2

Should be

totalArea_single = width * length;

instead of

width * length = totalArea_single;
Share:
10,131
thunpisit
Author by

thunpisit

Updated on June 04, 2022

Comments

  • thunpisit
    thunpisit almost 2 years

    CAN'T FIND WHAT'S WRONG...

    I have been programming and trying to fix a problem here but I can't find a bug. Could you guys help me advice what did I code wrong in this program? It will be thankful :)

    This is what complier said after I complied the program:

    In function 'main':
    Line 40: error: invalid lvalue in assignment
    

    which is:

    /*Calculate area for single room*/
    width * length = totalArea_single;
    

    And here's the whole code:

    #include <stdio.h>
    #include <stdlib.h>
    
    /*Main function of this program*/
    int main()
    {
    char input[512];/*buffer*/
    char roomInput[512];/*buffer*/
    int roomCount =0;/*Store number of room*/
    int width =0;/*width integer*/
    int length =0;/*length integer*/
    int room =0;/*To make sure roomCount have to be more than 0*/
    int totalArea_single =0;
    int totalArea_all =0;  
    
    /*Ask how many room in the house*/
    printf("\nHow many rooms in the house?: ");
    fgets(input,sizeof(input),stdin);
    sscanf(input,"%d",&roomCount);
    
    /*For loop for calculating room area by number of room entered*/
    for(room=0;room<roomCount;room++)
    {
        /*Input for width and have to be more than 0*/
        while(width>0)
        {
            printf("Width in meters for room %d: ",room+1);
            fgets(roomInput,sizeof(roomInput),stdin);
            sscanf(roomInput,"%d",&width);
        }
    
        /*Input for length and have to be more than 0*/
        while(length>0)
        {
            printf("Length in meters for room %d: ",room+1);
            fgets(roomInput,sizeof(roomInput),stdin);
            sscanf(roomInput,"%d",&length);
        }
    
        /*Calculate area for single room*/
        width * length = totalArea_single;
    
        /*Store area of all rooms*/
        totalArea_all = totalArea_single + totalArea_all;
    
        width = -1;
        length = -1;
    }
    
    /*Print out total areas of the house*/
    printf("\nTotal areas of the house is %d square meters",totalArea_all);
    
    return 0;
    }
    

    I'm not sure what did I do wrong... Thanks for your help :)