asghar-zamani
New Member
در زبان c++ عملکرد strcmp مقدار منفی یا مثبت را چه چیزی تعیین میکند
strcmp
Syntax
#include <string.h>
int strcmp(const char *s1, const char *s2);
Description
Compares one string to another.
strcmp performs an unsigned comparison of s1 to s2, starting with the first character in each string and
continuing with subsequent characters until the corresponding characters differ or until the end of the strings is
reached.
Return Value
If s1 is... strcmp returns a value that is...
less than s2 < 0
the same as s2 == 0
greater than s2 > 0
/* strcmp example */
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");
ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");
return 0;
}
buffer 2 is greater than buffer 1
buffer 2 is less than buffer 3