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.
Related
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.
This question already has answers here:
Correct way of defining NULL and NULL_POINTER?
(2 answers)
What is the difference between NULL, '\0' and 0?
(11 answers)
Closed 4 years ago.
If these 3 lines are the only time NULL is mentioned in stdio.h, how is NULL different from 0 and most importantly what tells the compiler NULL is actually an invalid address?
Talking about C, maybe in other libraries, like in iostream, NULL is defined differently.
0085 #ifndef NULL
0086 #define NULL 0
0087 #endif
To the compiler, NULL is indistinguishable from 0 when used as a pointer. The standard actually defines 0 to be a null pointer value, and the standard library just introduces a convenient macro NULL defined to a null pointer value (usually 0 or ((void*)0)), so that you can use it in code for better readability and expressing intent. But there's nothing special about NULL itself; it's the 0 that is relevant.
To the compiler, all of 0, NULL and '\0' map to the same value. However, from the readability perspective, you may want to use the appropriate value depending on the context, to make it clear what you're trying to do and to improve the efficacy of code auditing and reviews.
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:
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(...);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
Is this instruction correct in c :
5["abcdef"]
If yes, what does it mean ?
I had this question in a c test.
Yes, it is correct, and means the same as "abcdef"[5], which evaluates to 'f'.
It is because a[b] == *(a+b) == *(b+a) == b[a] by definition.
Yes.
The [] operator is commutative; that's the same as "abcdef"[5], which returns 'f'.