"lvalue required as left operand of assignment " error - c

The following code produces a "lvalue required as left operand of assignment"
if( c >= 'A' && c <= 'Z' || c = " " || c = ",") {
I assume I'm writing this wrong, what is wrong? and how would I write it correctly?

You should use single quotes for chars and do double equals for equality (otherwise it changes the value of c)
if( c >= 'A' && c <= 'Z' || c == ' ' || c == ',') {
Furthermore, you might consider something like this to make your boolean logic more clear:
if( (c >= 'A' && c <= 'Z') || c == ' ' || c == ',') {
Although your boolean logic structure works equivalently (&& takes precedence over ||), things like this might trip you up in the future.

equality is ==, = is assignment. You want to use ==. Also "" is a char*, single quotes do a character.
Also, adding some parens to your condition would make your code much easier to read. Like so
((x == 'c' && y == 'b') || (z == ',') || (z == ' '))

= is the assigning operator, not the comparing operator. You are looking for ==.

Personally, I prefer the minimalist style:
((x == 'c' && y == 'b') || (z == ',') || (z == ' '))
( x == 'c' && y == 'b' || z == ',' || z == ' ' )
or
( x == 'c' && y == 'b' ? z == ',' : z == ' ' )
against
( x == 'c' && y == 'b' ? z == ',' : z == ' ')

Related

Error after breaking long line of code in C

code:
for(int i = 0; *(str + i) != '\0'; i++){
if(*(str + i) != 'a' ||
*(str + i) != 'e' ||
*(str + i) != 'i' ||
*(str + i) != 'o' ||
*(str + i) != 'u' ||
*(str + i) != 'A' ||
*(str + i) != 'E' ||
*(str + i) != 'I' ||
*(str + i) != 'O' ||
*(str + i) != 'U' ||) {
new_str[index] = *(str + i);
index ++;
}
else{
*(vowels + vow_index) = *(str + i);
vow_index ++;
}
}
Error:
ex3.c:81:28: error: expected expression
*(str + i) != 'U' ||) {
^
1 error generated.
Why is this happening? I thought I could break long lines of code in C without worrying about indentation.
Get rid of the || just before the last parenthesis. That will fix your compile error.
The logic of what you are trying to do, however, is not going to work. The condition is always going to evaluate to true. (Try changing || to &&)
There is a trailing "||" at the end of your statement, after 'U'. Just remove it and it should be OK :)
About your code : you should rather store the vowels into a char array and compare your pointer to each increment of the array, this would improve readability.
|| is a logical OR operator, not a line break. You have a trailing operator in your last line, which should be *(str + i) != 'U') {

Simplifying (and make it more legible) logic condition

I am looking for help in making this logic more legible. Assume each alphabet letter is a compare statement (e.g TRUE == a.foo). Each alphabet is about 30 char long statements.
if ( ((a || b)
&& (c || d)) ||
((e || f)
&& (g || h)) )
Any suggestions?
Decompose it.
int ab = a || b,
cd = c || d,
ef = e || f,
gh = g || h,
firstThing = ab && cd,
secondThing = ef && gh;
if (firstThing || secondThing)
Try lining up the subexpressions in groups, lining up the parenthesis:
if (((a || b) && (c || d)) ||
((e || f) && (g || h)))
In order for the conditions to align properly and the logic operator to stand out, I would use this style:
if (((a || b) && (c || d))
|| ((e || f) && (g || h))
...
|| ((u || v) && (w || x))) {
/* handle the successful test */
} else {
/* handle the other cases */
}
There are a few variations of this, but here's one way that I think is pretty clear:
if
(
(
(a || b)
&&
(c || d)
)
||
(
(e || f)
&&
(g || h)
)
)

Why does this following program print "Yes" instead of "No"?

Why does this following program print "Yes" instead of "No"?
None of the variables is initialized to 2.
bool hello = 0;
int a = 1;
int b = 3;
int c = 4;
int d = 5;
if (a || b || c || d == 2) {
hello = 1;
}
if (hello == 1) {
printf("Yes");
}
if (hello == 0) {
printf("No");
}
return 0;
}
The statement
if (a || b || c || d == 2)
is equivalent to:
if (a != 0 || b != 0 || c != 0 || d == 2)
The equality comparison does not automatically distribute across all the variables. If you want to do that, you need to perform all the comparisons explicitly:
if (a == 2 || b == 2 || c ==2 || d == 2)
The expression (a || b || c || d == 2) evalutates to true because it treats a, b, c as booleans, and any non-zero integer is true.
You have given logical operator in the expression It means that if non zero value came then the expression is true. Then hello=1 is set and in next f statement it prints YES
You just meet the short circuit behavior of logical expressions OR.
The order of evaluation of logical OR || is left to right.
So in the following expression:
left || right
if left = true then right will never going to be executed (short circuit). In your code exactly same happened.
As you know, any non zero value treated as true in C, hence, a which is 1 is true. So, take a look:
if (a || b || c || d == 2)
if (true || bla bla bla) //rights are not even checked!
if (true)
hello = 1;
Tada! So the program print "Yes"!
None of the variables is initialized to 2.
Yes of course! But your if condition is not going to check that. To do so, try this:
if (a == 2 || b == 2 || c ==2 || d == 2) {
//...
because if judge num is not zero , if think this is true. so your code
if (a || b || c || d == 2)
like
if ( true || true || true || false)
the result is true, programe print "YES"

Trying to assign specific chars as a variable in C...giving me some difficulty

So I am trying to assign a char variable with the value "#" or "%" or "!" and if the variable does not have that value, I am prompting the user with an error. While compiling, I am getting the error "error: comparison between pointer and integer". Now, my code where the error is happening is this segment:
if (((a == !) || (a == %) || (a == #)) && (w > 0 && h > 0)) {
//do something
}
I can't figure out for the life of me why an error is coming up here. Any thoughts would be greatly appreciated.
Chars in C must be surrounded by single quotes:
if (((a == '!') || (a == '%') || (a == '#')) && (w > 0 && h > 0))

HCL three input odd parity

I have three inputs: a,b, and c. If my output is 1 then there are odd number of inputs with 1. Otherwise it is 0.
I have tried so far (a && b && c) || (!a && !b && !c), (a && b && c) || (!a && b && c), (a && c) || (b&& !c) and quite a few others. How can I do this?
How about a ^ b ^ c?
If only basic logic operators are allowed, you can use this
((a && b || !c) || (!a && !b || !c)) && (!a || !b || c) && (a || b || c)
as dbaupp commented, just equivalent transformation.

Resources