Code::Blocks Forums
User forums => Help => Topic started by: modeezy23 on March 28, 2019, 03:03:15 pm
-
New to C programming.
How come this code is giving me a warning:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char answer;
if(answer = ‘y’ || ‘Y’){ WARNING ON THIS LINE
printf(“Yes”);
}
else if(answer = ‘n’ || ‘N’){ WARNING ON THIS LINE
printf(“No”);
}
else if(answer != ‘y’ || ‘Y’ || ‘n’ || ‘N’){ WARNING ON THIS LINE
printf(“Invalid Option\n”)
}
return 0;
}
-
Find a website to ask your programming; this website does not teach programming.
http://forums.codeblocks.org/index.php/topic,9996.0.html (http://forums.codeblocks.org/index.php/topic,9996.0.html)
Tim S.
-
Noobs teaching noobs programming... what comes next?
Bad post, use code tags.
Bad forum, use entry level programmer forum.
-
Great, you fixed his code :). But sad, you didnt answer his question :(.
The answer is, each line except last (there only the second warning) should cause two warnings, one because of assignment in a conditional and one because of precedence of the || operator.
Because of the precedence the || operator gets evaluated first, the chars are interpreted in a boolean context, in every if the result is 1 and gets assigned to answer (except last if), so the result is always Yes ;D