The Difference Between !strcmp & strcmp
#1

I've spent time figuring out why my function didn't work. After trying !strcmp instead of strcmp, it worked. I'm clueless to why though. If someone could explain the difference to me, I'd really appreciate it.
Reply
#2

https://sampwiki.blast.hk/wiki/Strcmp
Reply
#3

Quote:
Originally Posted by Jefff
View Post
That doesn't explain anything about the explanation point.
Reply
#4

Strcmp

Compares two strings to see if they are the same.

Parameters:

(const string1[], const string2[], bool:ignorecase, length)

string1 The first string to compare.
string2 The second string to compare.
ignorecase (optional) When set to true, the case doesn't matter - HeLLo is the same as Hello. When false, they're not the same.
length (optional) When this length is set, the first x chars will be compared - doing "Hello" and "Hell No" with a length of 4 will say it's the same string.

Returns -1 if string1 comes before string2
1 if string1 comes after string2
0 if the strings are the same (for the matched length).




when you use !strcmp, in fact you are checking if the sentence return 0(if the strings are the same)

Sorry possibles errors in English because i don't speak english very good
Reply
#5

Strcmp returns 0 when the strings match and 1 when the strings don't match. When you check a statement with !, you're checking if it returns 0, otherwise you're checking if it doesn't return 0.

pawn Code:
if(something())
is a check for the function something() returning something other than 0.

pawn Code:
if(!something())
is a check for the function something() returning 0.


If you're confused by the way strcmp works, use this macro:

pawn Code:
#define strsame(%0,%1,%2) (!strcmp(%0, %1, %2))
pawn Code:
if(strsame("Hello", "hello", true))
{
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)