strcmp - Online Linux Manual PageSection : 3
Updated : 2023-02-05
Source : Linux man-pages 6.03
NAMEstrcmp, strncmp − compare two strings
LIBRARYStandard C library (libc, −lc)
SYNOPSIS#include <string.h>int strcmp(const char *s1, const char *s2);
int strncmp(const char s1[.n], const char s2[.n], size_t n);
DESCRIPTIONThe strcmp() function compares the two strings s1 and s2. The locale is not taken into account (for a locale-aware comparison, see strcoll(3)). The comparison is done using unsigned characters. strcmp() returns an integer indicating the result of the comparison, as follows: • 0, if the s1 and s2 are equal; • a negative value if s1 is less than s2; • a positive value if s1 is greater than s2. The strncmp() function is similar, except it compares only the first (at most) n bytes of s1 and s2.
RETURN VALUEThe strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.
ATTRIBUTESFor an explanation of the terms used in this section, see attributes(7). InterfaceAttributeValue strcmp(), strncmp() Thread safetyMT-Safe
STANDARDSPOSIX.1-2001, POSIX.1-2008, C99, SVr4, 4.3BSD.
NOTESPOSIX.1 specifies only that: The sign of a nonzero return value shall be determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared. In glibc, as in most other implementations, the return value is the arithmetic result of subtracting the last compared byte in s2 from the last compared byte in s1. (If the two characters are equal, this difference is 0.)
EXAMPLESThe program below can be used to demonstrate the operation of strcmp() (when given two arguments) and strncmp() (when given three arguments). First, some examples using strcmp(): $ ./string_comp ABC ABC
<str1> and <str2> are equal
$ ./string_comp ABC AB # 'C' is ASCII 67; 'C' − '\0' = 67
<str1> is greater than <str2> (67)
$ ./string_comp ABA ABZ # 'A' is ASCII 65; 'Z' is ASCII 90
<str1> is less than <str2> (−25)
$ ./string_comp ABJ ABC
<str1> is greater than <str2> (7)
$ ./string_comp $'\201' A # 0201 − 0101 = 0100 (or 64 decimal)
<str1> is greater than <str2> (64)The last example uses bash(1)-specific syntax to produce a string containing an 8-bit ASCII code; the result demonstrates that the string comparison uses unsigned characters. And then some examples using strncmp(): $ ./string_comp ABC AB 3
<str1> is greater than <str2> (67)
$ ./string_comp ABC AB 2
<str1> and <str2> are equal in the first 2 bytes
Program source /* string_comp.c
Licensed under GNU General Public License v2 or later.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
int res;
if (argc < 3) {
fprintf(stderr, "Usage: %s <str1> <str2> [<len>]\n", argv[0]);
exit(EXIT_FAILURE);
}
if (argc == 3)
res = strcmp(argv[1], argv[2]);
else
res = strncmp(argv[1], argv[2], atoi(argv[3]));
if (res == 0) {
printf("<str1> and <str2> are equal");
if (argc > 3)
printf(" in the first %d bytes\n", atoi(argv[3]));
printf("\n");
} else if (res < 0) {
printf("<str1> is less than <str2> (%d)\n", res);
} else {
printf("<str1> is greater than <str2> (%d)\n", res);
}
exit(EXIT_SUCCESS);
}
SEE ALSOmemcmp(3), strcasecmp(3), strcoll(3), string(3), strncasecmp(3), strverscmp(3), wcscmp(3), wcsncmp(3), ascii(7) 0
Johanes Gumabo
Data Size : 18,265 byte
man-strncmp.3Build : 2024-12-05, 20:55 :
Visitor Screen : x
Visitor Counter ( page / site ) : 3 / 236,320
Visitor ID : :
Visitor IP : 3.141.41.109 :
Visitor Provider : AMAZON-02 :
Provider Position ( lat x lon ) : 39.962500 x -83.006100 : x
Provider Accuracy Radius ( km ) : 1000 :
Provider City : Columbus :
Provider Province : Ohio , : ,
Provider Country : United States :
Provider Continent : North America :
Visitor Recorder : Version :
Visitor Recorder : Library :
Online Linux Manual Page : Version : Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page : Library : lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page : Library : lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base : Version : Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base : Library : lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36
Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.