Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strcmp.c File Reference
#include <string.h>
Include dependency graph for strcmp.c:

Go to the source code of this file.

Functions

int strcmp (const char *s1, const char *s2)
 Compares two null-terminated byte strings lexicographically. More...
 

Function Documentation

◆ strcmp()

int strcmp ( const char *  s1,
const char *  s2 
)

Compares two null-terminated byte strings lexicographically.

Compares two null-terminated byte strings lexicographically.

The behavior is undefined if s1 or s2 are not pointers to null-terminated byte strings.

Parameters
s1pointers to the null-terminated byte strings to compare
s2pointers to the null-terminated byte strings to compare
Returns
Negative value if s1 appears before s2 in lexicographical order. Zero if s1 and s2 compare equal, or if n is zero. Positive value if s1 appears after s2 in lexicographical order.

Definition at line 12 of file strcmp.c.

13 {
14  int r = -1;
15 
16  if(s1 == s2)
17  {
18  // short circuit - same string
19  return 0;
20  }
21 
22  // I don't want to panic with a NULL ptr - we'll fall through and fail w/ -1
23  if(s1 != NULL && s2 != NULL)
24  {
25  // iterate through strings until they don't match or s1 ends (null-term)
26  for(; *s1 == *s2; ++s1, ++s2)
27  {
28  if(*s1 == 0)
29  {
30  r = 0;
31  break;
32  }
33  }
34 
35  // handle case where we didn't break early - set return code.
36  if(r != 0)
37  {
38  r = *(const char*)s1 - *(const char*)s2;
39  }
40  }
41 
42  return r;
43 }
#define NULL
Definition: stddef.h:15

References NULL.

Referenced by strcoll(), wctrans(), and wctype().

Here is the caller graph for this function: