Strcmp explained/how does it work internally. -
Sinner - 23.04.2011
I've seen many people asking "how can I create a command with strcmp? (other command processors aside). Fact is, if you know exactly how strcmp works you wouldn't need to ask such a question in the first place, because it would all be so logical. This is true for all functions, if you know how they work it can really help you while you're programming. So here's a little 'noob' tutorial on how strcmp works
1. What is strcmp?
- StrCmp stands for String Compare, and all it does is compare 2 strings with eachother.
2. How does it NOT work?
- At first it seems logical to think that, if 2 given strings are equal to eachother, strcmp will return "1" as a result, and if the 2 strings are not equal to eachother, it will return "0". This is wrong! Exactly here is where most badly informed scripters go wrong.
3. How does it work
- strcmp works by taking each characters of a string, and substract it's ASCII-Code with the ASCII-Code of the character in the other string. So lets say you want to compare the string "hello" with the string "hello" (which is the same). This is what strcmp will do:
- Take the first character ('h') and substract it with the character from the second string ('h')
- If the characters are the same, the result of the substraction will be 0 (null)
- Do this for all characters of the strings
Now, if the 2 strings are the same, the total of all the characters substracted from eachother will be 0.
This is why to compare 2 strings you need to check if the result is 0, not 1.
Example:
pawn Код:
if(strcmp("Hello", "Hello") == 0)
{
// Do something if they the same
}
else
{
// Do something if they are different from another
}
If the 2 strings are not equal to eachother, strcmp will return the result from the substractions.
~I hope this helped in any way
Re: Strcmp explained/how does it work exactly. -
Vince - 23.04.2011
At first I thought this was another strcmp/strtok command tutorial, but I'm glad it isn't. I actually never knew
how strcmp actually works, just that it works. I learnt something new today.
Re: Strcmp explained/how does it work exactly. -
Ash. - 23.04.2011
Very Nice

I knew how it worked, i just didn't see
WHY it returned 0 - but now i see
Re: Strcmp explained/how does it work exactly. -
Hiddos - 23.04.2011
I did see why it returned 0 on success, but I was wondering why it could either return
-1 or
1. Could you explain that as well?
Re: Strcmp explained/how does it work exactly. -
Ash. - 23.04.2011
Quote:
Originally Posted by Hiddos
I did see why it returned 0 on success, but I was wondering why it could either return -1 or 1. Could you explain that as well?
|
Isn't it -1 if the strings don't match but the first string comes before the second? and vice-versa, 1 for the strings being the other way round?
Re: Strcmp explained/how does it work exactly. -
Davz*|*Criss - 23.04.2011
Nice tutorial.
I have learnt something new today.
Re: Strcmp explained/how does it work exactly. -
Sinner - 23.04.2011
Quote:
Originally Posted by Hiddos
I did see why it returned 0 on success, but I was wondering why it could either return -1 or 1. Could you explain that as well?
|
Yes, ASCII-Codes are like numbers, every character has a number. If you compare a character that has a lower ASCII value then the one you're comparing it to has (for example: 5 - 7 = -2), it will return a negative result. Same applies for whole strings. I think PAWN just rounds any values under 0 to -1 and any values above it to 1.
Quote:
At first I thought this was another strcmp/strtok command tutorial, but I'm glad it isn't. I actually never knew how strcmp actually works, just that it works. I learnt something new today.
|
Thank you, glad I could help
Re: Strcmp explained/how does it work exactly. -
Hiddos - 23.04.2011
Quote:
Originally Posted by Sinner
Yes, ASCII-Codes are like numbers, every character has a number. If you compare a character that has a lower ASCII value then the one you're comparing it to has (for example: 5 - 7 = -2), it will return a negative result. Same applies for whole strings. I think PAWN just rounds any values under 0 to -1 and any values above it to 1.
|
Already though that would be it after reading it, but thanks for confirming it
Re: Strcmp explained/how does it work exactly. -
Tee - 29.04.2011
Sinner thanks very much. I own 2 well scripted servers, and I did not know how strcmp works. But today I do thanks.
Re: Strcmp explained/how does it work exactly. -
LiamM - 29.04.2011
I dont really know much about strcmp and so far my servers came along very well without it but now I have hit a deadend where I need to learn it, I understand this part but I need a more in depth tutorial before understanding this perfectly
Re: Strcmp explained/how does it work exactly. -
SchurmanCQC - 29.04.2011
Mind if I add on to this?
Re: Strcmp explained/how does it work exactly. -
Sinner - 30.04.2011
Quote:
Originally Posted by Schurman
Mind if I add on to this?
|
Ofcourse not, be my guest.
Re: Strcmp explained/how does it work exactly. -
Aoi_Rikuto - 02.06.2011
Simple, but taught me alot.
Re: Strcmp explained/how does it work exactly. -
MdeRooy - 19.07.2011
Very good explanation! I now understand that when comparing passwords, i have to make sure the result is 0 to pass the check, and that either 1 or more, or -1 or less is a wrong password. Thanks a lot!
Re: Strcmp explained/how does it work exactly. -
Jack_Rocker - 20.07.2011
GREAT! 10/10
Re: Strcmp explained/how does it work exactly. -
iggy1 - 20.07.2011
Just to make it a little more clear strcmp returns -1, if the first string precedes the second string alphabetically. The wiki doesn't realy explain it (return values) clear enough IMO.
pawn Code:
strcmp("aardvark", "badman");//will return -1
strcmp("aardvark", "aardvark");//will return 0
strcmp("badman", "aardvark");//will return 1
Re: Strcmp explained/how does it work exactly. - Astralis - 19.11.2011
Nice tutorial. Now everyone knows how to do it.
5/5!!
Re: Strcmp explained/how does it work exactly. -
CSSI - 19.11.2011
Very Nice

I also didn't knew Why it returned 0. Thanks!
Re: Strcmp explained/how does it work exactly. -
JaKe Elite - 21.08.2012
I learned something today about strcmp.
Thank you for this tutorial.
Re: Strcmp explained/how does it work exactly. -
ddnbb - 22.08.2012
Thanks, this helped alot!