1 /*** 2 *strcmp.c - routine to compare two strings (for equal, less, or greater) 3 * 4 * Copyright (c) Microsoft Corporation. All rights reserved. 5 * 6 *Purpose: 7 * Compares two string, determining their ordinal order. 8 * 9 *******************************************************************************/ 10 11 #include <string.h> 12 13 #pragma function(strcmp) 14 15 /*** 16 *strcmp - compare two strings, returning less than, equal to, or greater than 17 * 18 *Purpose: 19 * STRCMP compares two strings and returns an integer 20 * to indicate whether the first is less than the second, the two are 21 * equal, or whether the first is greater than the second. 22 * 23 * Comparison is done byte by byte on an UNSIGNED basis, which is to 24 * say that Null (0) is less than any other character (1-255). 25 * 26 *Entry: 27 * const char * src - string for left-hand side of comparison 28 * const char * dst - string for right-hand side of comparison 29 * 30 *Exit: 31 * returns -1 if src < dst 32 * returns 0 if src == dst 33 * returns +1 if src > dst 34 * 35 *Exceptions: 36 * 37 *******************************************************************************/ 38 39 int __cdecl strcmp ( 40 const char * src, 41 const char * dst 42 ) 43 { 44 int ret = 0 ; 45 46 while((ret = *(unsigned char *)src - *(unsigned char *)dst) == 0 && *dst) 47 { 48 ++src, ++dst; 49 } 50 51 return ((-ret) < 0) - (ret < 0); // (if positive) - (if negative) generates branchless code 52 } 53