Nick Gz
Nick Gz

Reputation: 17

lvalue required as left operand of assignment in c for an if statement in c language

i was writing a c code for my college assignment and this error keeps popping up which i don't understand why. The error is "lvalue required as left operand of assignment in c"

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE* readFile;
    FILE* resultFile;

    int charCount, wordCount, spaceCount;
    char readFileName[100];
    char ch;

    printf("enter file name here:");
    scanf("%s", readFileName);
    readFile = fopen(readFileName, "r");

    if(readFile = NULL)
    {
        printf("could not open file %s make sure the name and path of the is correct");

    }
    else
    {
        while((ch = fgetc(readFile)) != EOF)
        {
            charCount++;

            if(ch = ' ' || ch = '\n' || ch = '\t' || ch = '\0')  /* error is shown on this line*/
            {
                wordCount++;
            }
            if(ch = ' ')
            {
                spaceCount++;
            }
        }
    }
    fclose(readFile);

    resultFile = fopen("result.txt", "w");
    fprintf("numbers of characters:%d\nnumbers of words:%d\nnumbers of spaces:%d\n", charCount, wordCount, spaceCount);
    fclose(resultFile);

    printf("your result result has been delivered into ""result.txt"" file");
    return 0;
}

I never faced this error before and i'm completely clueless. and the program fails to compile

Upvotes: 0

Views: 237

Answers (3)

Claudio Rizzi
Claudio Rizzi

Reputation: 29

When you`re doing a comparation you have to use two equals simbol like

if (ch == ' '){
    then do something...
}

you can use

ch = ' ';

to produce a empty char that contains ' ' but in your conditional that will show you an error!

Upvotes: 0

4386427
4386427

Reputation: 44274

While the real fix for your problem is to use == (comparison) instead of = (assignment), it's worth to notice the reason for the error message.

In principle there is nothing wrong by doing an assignment in the if condition. Doing:

if(ch = ' ') {...};

is perfectly legal.

The reason you get that error message is operator precedence, i.e. || has higher precedence than =.

So

if(ch = ' ' || ch = '\n') {...};

will be evaluated like:

if(ch = (' ' || ch) = '\n') {...};
        ^^^^^^^^^^^
        Evaluated first

        ^^^^^^^^^^^^^^^^^^
        Evaluated secondly (as boolean-result = '\n')

and the result of the first expression is 0 or 1 which isn't an lvalue. So the code tries to assign '\n' to the result of a boolean expression.

Upvotes: 2

Lundin
Lundin

Reputation: 213892

How to read the error:

  • Left operand of assignment is clear enough: something = .... Where = is the assignment operator.
  • "lvalue" basically means a value which has a place in memory and may get modified. Plain read/write access of variables is called "lvalue access". The term originates from "left value", as in left of =

So now we have to go check your code for fishy assignments:

  • readFile = NULL, ch = ' ' and so on. ch is actually a "lvalue" so that part is valid C. Any decent compiler should however warn you against these. "Assignment inside condition", "Possible incorrect assignment" etc. It's the silly old = instead of == bug.

    = has lower operator precedence than ||, which in turn has lower operator precedence than ==. Because of that, the compiler stumbles over something like '\t' || ch = '\0' which it reads as ('\t' || ch) = '\0'. Where ('\t' || ch) is not a "lvalue". Hence the error.

Upvotes: 1

Related Questions