I have a little confusion about this statement.
if( Ql_StrPrefixMatch(strURC, urcHead) )
{
callback_NTPCMD((char*)strURC);
}
The source code of Ql_StrPrefixMatch is:
s32 Ql_StrPrefixMatch(const char* str, const char *prefix)
{
for ( ; *str != '\0' && *prefix != '\0' ; str++, prefix++)
{
if (*str != *prefix) {
return 0;
}
}
return *prefix == '\0';
}
How is the if-statement evaluated to True or False based on the returns of Ql_StrPrefixMatch function, because it either returns 0 or *prefix == '\0'?
Does return of 0 mean False?
Please help me to explain this point.
Thank you in advance
....because it either returns 0 or '\0'?
No, the second return statement uses a comparison operator, not an assignment. That returns either 0 or 1. That return value is used to evaluate the if statement.
s32 is a signed integer. The function returns eigther 0 or 1. C then interprets this as a bool.
Why does the function return 1?
*prefix == '\0' gives a bool, which then is converted to s32.
Yes, indeed, you are current. 0 in c means false, and also, on the other hand, NULL or '\0' also evaluates to false. You can refer to this answer for your query. So as you can guess, if(0) and if('\0') will be evaluated to the false branch of the if statement. And also, additional information: Anything non-zero value will be evaluated as true.
Related
This question already has answers here:
Using Exclamation Marks '!' in C
(5 answers)
Closed 2 years ago.
I am learning C from a textbook, and I stumbled onto the code of a function, where the following part had little explanation to what it does. It looked something like this:
int func(char *a, char *b) {
if(!a || !b)
return 0;
return 1;
}
My understanding is it checks that a and b are not null? Is this correct? Any help is welcome for this beginner :)
! is the logical not operator.
The if condition !a || !b is true if either a or b are zero, i.e. NULL pointers. Then the func returns 0 in that case, 1 otherwise:
a b condition func
null null true 0
null non-null true 0
non-null null true 0
non-null non-null false 1
It is easier to understand if you negate the condition and swap the return values:
int func(char *a, char *b) {
if(a && b)
return 1;
return 0;
}
Since in C the relational operators give their result as an int, i.e. 0 for false and 1 for true, you can simplify further to:
int func(char *a, char *b) {
return a && b;
}
Not exactly. When a is null or b is null then the logical NOT operator will cause the if conditional to evaluate as true and the return value is zero, otherwise the code returns 1 indicating that either a or b is not null.
The ! operator in most programming languages reverses the logic(or the not operator). i.e.(From True to False or vice versa)
For example:
a=True;
if(!a) {
return 1;
}else {
return 2;
}
Output is 2. So in this case, you are saying if not A OR not B, return 0.
I've seen the operator ! being used in multiple places differently and I still don't get how it actually works. My basic understanding is it reverses the value from true to false and vice versa. If it reversed to true the statement triggers. Let's take an example.
int main(void)
{
int a = 5;
if (!(a == 6))
{
printf("unlike\n");
}
if (!(a == 5))
{
printf("like\n");
}
}
In the code above since a is 5 it ends up printing "unlike" because the false statement that a is 6 got reversed. Now let's take another example.
int main(void)
{
string i = "abc";
string j = "cab";
string k = "abc";
if (!strcmp(i, j))
{
printf("unlike\n");
}
if (!strcmp(i, k))
{
printf("like\n");
}
}
The string type has been taken from the cs50.h header and strcmp from string.h. strcmp returns value 0 if the two strings are alike and if unlike depending on the alphabetical order returns a positive or negative value. Now if we follow the logic in the previous example, since i and j are unlike, and false it should be reversed to true and unlike should be the output. But I tried running the code and the result was like.
I am confused. Can anyone please explain this to me clearly? Feel free to use other examples too. I could always get away with not using ! but I just want to learn what it is and how to properly use it.
A boolean in C is an integer with zero for false and non-zero for true.
strcmp returns 0 when the compared strings are identical and a non-zero value depending on the difference otherwise. Therefore, strcmp(i,k) is seen as "false". The ! then changes this to "true", which leads to your current output.
In the first case a = 5. then if (!(a == 6)); here a = 6 is not true (false), so it's something like this. if (!(false)) it means if (true). That's why it prints "unlike".
strcmp(i, j) returns 0 if the strings i and j match; otherwise, it will return a non-zero value. In your case,
(!strcmp(i, j))
Here i and j are not equal so strcmp will return a non-zero value because i != j. So !(1) means not(1) means 0, so the if condition is false because of zero. Therefore it'll not execute the printf("unlike\n") line.
(!strcmp(i, k))
Here i and k are same so strcmp will return 0. !(0) means not(0) = 1 so the if condition is true. It will execute the printf("like\n") line.
Can someone explain what the exclamation point in the if statement does (i.e. !strmcp)?
string names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};
// Search for EMMA
for (int i = 0; i < 4; i++)
{
if (!strcmp(names[i], "EMMA"))
{
printf("Found\n");
return 0;
}
}
printf("Not found\n");
return 1;
For an if statement, if the expression evaluates to 0, then the block of code following the if statement is not executed. Any other value (positive or negative), will result in executing the code block. The function strcmp uses 0 to say that strings are equal because less than 0 is used to differ from greater than 0.
So in this code, we want printf("Found\n"); to be executed when the strings are equal. Since strcmp results in 0, we need to negate the value so that it becomes 1 which will result in executing that code block.
strcmp() returns 0 if the strings are identical, so you need to negate it, if you use it in an if clause to assert a true statement.
If your clause is if(0), the code inside the condition will not be executed.
For completion, it returns negative if the first different character found is lower in the first string, for instance:
first parameter string: "abca"
second parameter string :abcd"
This will return negative. If it's the other way arround it will return positive.
Also, string is not usually used in C (I refer you to Jonathan Leffler's commment), you can use char*:
char *names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};
Unary operator ! is called the logical NOT operator (cf., for example, this definition at cppreference.com). ! expression returns 1 if expression evaluates to 0, and it returns 0 if expression evaluates to anything else but 0.
So the condition in if (!0) gives 1; this means, the condition is met and the if-block is entered. It has the same meaning as if(0==0)
Consequently, the meaning of
if(!strcmp(names[i], "EMMA"))
in your code is exactly the same as
if(0==strcmp(names[i], "EMMA"))
And you already know when strcmp returns 0...
The exclamation point is the C's boolean negation character.
It means give me the boolean opposite of the value. A boolean is either true or false, which are 1 or 0 in the C Language.
In C, if statements execute their conditional statements if the argument is true.
if (a) means if a is true (i.e. non-zero)
if (!a) means if a is false (i.e. 0)
Therefore:
if (a) is the same as if (a != 0)
if (!a) is the same as if (a == 0)
Sometimes you'll see code that uses two exclamation points in a row "!!"
For example:
int a = !!b;
That ensures a will be ONLY 0 or 1, regardless of what the value of b is.
If b is ANY non-zero value, the ! operator will treat it as though it is true true, which it treats as being the same as 1
So:
!0 == 1
!1 == 0
!52 == 0
!25692 == 0
The second ! does the boolean inversion again, so:
!!0 == 0
!!1 == 1
!!52 == 1
!!25692 == 1
In C any non zero value is considered ad the logical truth, zero i considered as logical false. ! is a logical negation. So !0 (not false) will be the truth and if(!strcmp(str1,str2)) {statements} statements will be executed when str1 will be same as str2
I know what strcmp returns but I don't know what it returns in this code.
firstly I have this function:
static int match_str(const void *str1, const void *str2)
{
return !strcmp((const char *)str1, (const char *)str2);
//if not equal return 1.
}
and then I have this
if (match_str) return 1; // not equal so return
else{ my code goes on} // equal so continue
What I don't understand is if str1 equals to str2, the match_str should returns 0. and then the code moves on.
but it adds a '!' operater. it seems like if they are equal strcmp=0, it returns to "!0 " which means"!0=1 " . but if returns to 1. it seems like if they are equal, it will stop.
Actually, it continues if they are equal.
I really confused why " return !strcmp " works rather than "return strcmp", what is the purpose of using '!' here.
thanks
From C Standard##6.5.3.3p5
5 The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).
>> What I don't understand is if str1 equals to str2, the match_str should returns 0. and then the code moves on.
Because of use of ! operator with strcmp in return statement (return !strcmp(.....)), the match_str() will return 1 if string matches and 0 if they don't match.
>> I really confused why " return !strcmp " works rather than "return strcmp", what is the purpose of using '!' here.
The strcmp() returns 0 if string matches and non zero value if they do not match because it compares two null-terminated strings lexicographically. The author of function match_str() actually wanted to return 1 if string matches and seems that, having this thought in mind, s/he has given name (match_str) to the function. The caller of match_str() function should be aware of this while using match_str() function.
if (match_str(str1, str2)) {
//strings are equal
} else {
//strings are not equal
}
I came across this line of code written in C that confuses me coming from a JavaScript background.
short s;
if ((s = data[q]))
return s;
Is this assigning s to data[q], and if it equals true/1, return s?
Yes, an assignment...well assigns...but it's also an expression. Any value not equalling zero will be evaluated as true and zero as false.
it would be the same as
if ((s = data[q]) != 0) return s;
Your code is assigning data[q] to s and then returns s to the if statement. In the case when s is not equal to 0 your code returns s otherwise it goes to the next instruction.
Or better said it would expand to the following:
short s;
s = data[q];
if (s != 0)
return s;
Basically C evaluates expressions. In
s = data[q]
The value of data[q] is the the value of expression here and the condition is evaluated based on that.
The assignment
s <- data[q]
is just a side-effect.
Read this [ article ] on sequence points and side-effects