質問

Hi I made a program which integer divides a number by 50 and shows the mod of that number also but the compiler tells me that "value requires left operand of assignment" for"/" I'm not sure what to do. Here's the code:

#include <stdio.h>

int main()
{
    int num;
    int i;
    int m;

    printf("enter number: ");
    scanf("%d", &num);

    num / 50 = i;
    num % 50 = m;

    printf("the division is: %d\n", i);
    printf("the remainder is: %d", m);

return(0); }

役に立ちましたか?

解決 2

This:

num / 50 = i;  
num % 50 = m;  

Is seriously illegal in C. You have to do this:

i = num / 50;
m = num % 50;

Why? Because the equals sign in C doesn't work the way it does in math. In math, an equals sign establishes that two expressions are already equal. It tells you something new about them, but doesn't change them.

In C, it's expressly used to change a variable, something you can't really do in traditional math notation. It copies the value on the right into the variable address on the left. That's why some people call it "gets" instead of "equals," as in "i gets num / 50." It makes sense when you read it that way.

他のヒント

your assignment statement is backwards

use:

i = num / 50;
m = num % 50;

replace num / 50 = i; and num % 50 = m; with i = num / 50; and m = num % 50;

In C , while using assignment operator L.H.S should be L-value not r-value(i.e. it should have address associated with it ) hence use i = num / 50; m = num % 50;

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top