This question already has answers here:
why negative number is taken as true in condition statement? [duplicate]
(5 answers)
Closed 2 years ago.
In my current class I am seeing a lot of times where my teacher puts a number as the condition of an if statement then asks for the output. I'm confused as to how these statements are being evaluated.
Example:
if(-1) {
x = 35;
}
or
if(0) {
x = 35;
}
Why does it go inside the if statement instead of just ignoring it? What is making if(-1) evaluate true? And why does if(0) evaluate false?
This is an online class so I can't get help directly from the teacher usually. I have searched for a few hours on various websites about C for loops, but I haven't found anything relevant yet to my question.
C doesn't have a "real" boolean type. When evaluating integers, 0 is a "false", and anything else is a "true" value.
Related
This question already has answers here:
How do I use the conditional (ternary) operator?
(10 answers)
Closed 10 months ago.
Can someone explain this kind of statement(s)? I didn't get it.
data[i] = ((data[i] == div[i]) ? '0' : '1');
It's an if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
So , your expression can be visualized into if-else statement as:
#include <stdio.h>
int main(){
// Code ...
if(data[i]==div[i])
data[i]='0';
else
data[i]='1';
}
** ‘?:’ takes three operands to work, hence they are also called ternary operators.
This question already has answers here:
How the assignment statement in an if statement serves as a condition?
(4 answers)
Closed 2 years ago.
I am aware that when a non zero value is provided as a condition in an if statement, the condition is evaluated to be true.
Yet, I'm wondering how an assignment(=) is evaluated to be true here, instead of a comparison(==), and runs without any errors in the below C program.
int main(){
int a, b =5;
if(a=10) {
("This works");
}
if(b=1){
("This works too");
} }
The assignment operator always returns the value that was assigned. So in your case a=10 has the value 10 and the if statement is essentially if(10). Which is true.
a = 10 is an expression that assigns 10 to a and whose value is the result of the assignment (that is 10).
any value different from 0 is considered to be true
Try this:
if (b = 0) {
("This is never displayed");
}
This question already has answers here:
string comparison inside if condition malfunctioning [duplicate]
(2 answers)
Closed 6 years ago.
I am trying to implement an IF STATEMENT
in my C-Server.
read(client_fd,received_data,100);
printf("%s",received_data);
I want to print "ooo" if the received data equals to "test"
if (received_data == "test") {
printf('ooo');
}
?
The above example results in errors. (can't compile)
I don't know much C.
UPDATE :
Now I can compile fine ( after changing 'ooo' to "ooo" )
But although I am sending "test" to it.. if statement is not doing anything i think.
The == doesn't work for const char *.
Use strcmp instead.
This question already has answers here:
What is the difference between NULL, '\0' and 0?
(11 answers)
is NULL/false in C and C++ anything more than 0x0 / 0b0 / '\0' / 0 [duplicate]
(6 answers)
Closed 9 years ago.
While I was coding a program in C, I came up a question that I could not figure out. I was checking if a condition is met in an if statement but was wondering if there is any difference between the following:
if(ptr != NULL)
or
if(ptr)
To me, I feel like both of those are correct but in the C world, the second one would be used more and in the Java world, the first one is used more. Is one more correct then the other?
In C, anything that evaluates to 0 (zero) is "false", and anything non-zero is "true".
Thus when ptr is NULL, those two if conditions end up working the same way:
if (ptr != NULL) = if (0 != 0) = if (0)
and:
if (ptr) = if (0)
You'll get people debating which is better, but you'll see both in code. The first is more clear because it's more explicit. The second is shorter. Both are technically correct and equivalent.
Both are correct and equivalent.
A pointer alone evaluates to false if the pointer is NULL and to true otherwise.
This question already has answers here:
Why would you use the ternary operator without assigning a value for the "true" condition (x = x ?: 1)
(7 answers)
Closed 10 years ago.
I am attempting to use some code utilising a ternary statement in order to run a specific part, however, when I do this, I get the warning:
expression result unused
And the code in that particular section does not run.
The code in this case is:
i != a ?: printf("|%*s\\\n", i, "");
Why would that be? According to here, this form of the ternary operator, in which there is no alternative for the case, should function, however, it simple skips over it here. Any help is appreciated.
Your code is equivalent to
(i != a) ? (i != a) : printf(...);
Note that you won't end up using the i != a result, hence the warning. Best to write this as an if statement:
if(i==a) printf(...);