Thread: lvalue required as left operand of assignment

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    23

    Question lvalue required as left operand of assignment

    I am just learning C programming and and working through 'The C Programming Language' by Dennis Ritchie.

    I am just starting to explore arrays and am working on a sample piece of code in the book which counts digits, whitespace and other characters input.

    I have recieved the error 'lvalue required as left operand of assignment on line 16 of the code which reads as follows:


    else if (c == ' ' || c == '\n' || c = '\t')


    The complete code for the program is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int c, i, nwhite, nother;
        int ndigit[10];
    
        nwhite = nother = 0;
        for (i = 0; i < 10; ++i)
            ndigit[i] = 0;
    
        while ((c = getchar())!= EOF)
            if (c >= '0' && c <= '9')
                ++ndigit[c-'0'];
            else if (c == ' ' || c == '\n' || c = '\t')
                ++nwhite;
            else
                ++nother;
    
        printf("digits =");
        for (i = 0; i < 10; ++i)
            printf(" %d", ndigit[i]);
        printf(", white space = %d, other = %d\n", nwhite, nother);
    
    }
    Can someone explain why this error is occuring, I can see nothing wrong with it nor any of the rest of the code in the program.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are missing the double equals sign in the last section...
    Code:
    else if ((c == ' ') || (c == '\n') || (c = '\t'))

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    23
    Wow, something so simple that I couldn't even see it.

    Thanks for the help.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Hmmmm.... interesting. I was looking at and thought there's nothing wrong with that, except that the '=' should be '==', but what if the OP wanted it that way? It's a strange error to get. Perhaps the compiler should have issued a warning about assignment inside an 'if' and then go away quietly.

    Then I realized it's all about operator precedence. Assignment is one of the lowest precedence things. So the expression is really:
    (c == ' ' || c == '\n' || c) = '\t'.... and you certainly can't assign something to a formula.
    C operator precedence never made much sense to me.

    C Operator Precedence Table

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by nonoob View Post
    Then I realized it's all about operator precedence. Assignment is one of the lowest precedence things. So the expression is really:
    (c == ' ' || c == '\n' || c) = '\t'.... and you certainly can't assign something to a formula.
    C operator precedence never made much sense to me.
    Nice catch! As for = having the second-lowest precedence, imagine it had the highest precedence. Then, something like c = 5 + 7; would be interpreted as (c = 5) + 7;. That would assign 5 to c, then basically turn into 5 + 7;, which means the addition is thrown out. Most of the ordering follows rules from arithmetic or boolean math, but I agree there are some strange things. Like why postfix is as high as function calls, etc, and higher than prefix and why shift operators lie between bitwise negation and bitwise and. I imagine there are really good reasons, but I haven't figured them out quite yet.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Do you want

    ret = a == 10;

    to be parsed as

    (ret = a) == 10;

    or

    ret = (a==10);
    ?

    I think most of the time, you want assignment operator lower precedence than relational operators.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I do a lot of assignment within 'if' statements... assignment followed by immediate testing. Or (ret = a) == 10 type stuff. I often have to check the precedence chart because things are never the way I expect.
    if (INVALID_HANDLE_VALUE == (lev->hSearch = FindFirstFile(bufw, &FileData))) {
    Seems every time I am forced to put in more parenthesis than I want.
    Maybe I'm just confused from other languages whose rules are different. Plus I get annoyed when the compiler tells me I don't know what I'm doing when I took great care to do something cool.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: lvalue required as left operand of assignment
    By owjian1987 in forum C Programming
    Replies: 5
    Last Post: 02-11-2011, 12:34 PM
  2. Replies: 3
    Last Post: 06-01-2010, 06:22 AM
  3. lvalue required as left operand of assignment
    By joeman in forum C++ Programming
    Replies: 11
    Last Post: 03-27-2010, 12:36 AM
  4. lvalue required as left operand of assignment (??)
    By oospill in forum C Programming
    Replies: 3
    Last Post: 11-03-2009, 11:02 PM
  5. !value required as left operand of assignment
    By Jasper in forum C Programming
    Replies: 4
    Last Post: 08-15-2009, 02:21 PM