-2

My following code for testing strcmp is as follows:

char s1[10] = "racecar";
char *s2 = "raceCar";     //yes, a capital 'C'
int diff;
diff = strcmp(s1,s2);
printf(" %d\n", diff);

So I am confused on why the output is 32. What exactly is it comparing to get that result? I appreciate your time and help.

CodeFreak
  • 90
  • 1
  • 2
  • 15
  • 1
    Yes, that's strange, as The Answer is known to be [42](https://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker's_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe.2C_and_Everything_.2842.29). Do you know the question? – too honest for this site Oct 27 '16 at 00:27
  • So, what from the documentation of `strcmp` was unclear? Which value do you expect? – too honest for this site Oct 27 '16 at 00:28
  • Possible duplicate of [What does strcmp() exactly returns in c?](http://stackoverflow.com/questions/34824838/what-does-strcmp-exactly-returns-in-c) – hobbs Oct 27 '16 at 00:30
  • You can look at [**Apple's strcmp here**](http://opensource.apple.com//source/Libc/Libc-262/ppc/gen/strcmp.c) and [**glibc's strcmp here**](https://github.com/zerovm/glibc/blob/master/string/strcmp.c) – David C. Rankin Oct 27 '16 at 00:36

6 Answers6

1

It is not specified. According to the standard:

7.24.4.2 The strcmp function

#include <string.h>
int strcmp(const char *s1, const char *s2);

Description

The strcmp function compares the string pointed to by s1 to the string pointed to by s2.

Returns

The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.

AlexD
  • 32,156
  • 3
  • 71
  • 65
1

Whatever it wants. In this case, it looks like the value you're getting is 'c' - 'C' (the difference between the two characters at the first point where the strings differ), which is equal to 32 on many systems, but you shouldn't by any means count on that. The only thing that you can count on is that the return will be 0 if the two strings are equal, negative if s1 comes before s2, and positive if s1 comes after s2.

hobbs
  • 223,387
  • 19
  • 210
  • 288
1

The man pages states that the output will be greater than 0 or less than 0 if the strings are not the same. It doesn't say anything else regarding the exact value (if not 0).

That being said, the ASCII codes for c and C differ by 32. That's probably where the result is coming from. You can't depend on this behavior being identical in any two given implementations however.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

According to the C standard (N1570 7.24.4.2):

The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.

It says nothing about which positive or negative value it will return if the strings are unequal, and portable code should only check whether the result is less than, equal to, or greater than zero.

Having said that, a straightforward implementation of strcmp would likely return the numeric difference in the values of the first characters that don't match. In your case, the first non-matching characters are 'c' and 'C', which happen to differ by 32 in ASCII.

Don't count on this.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

"strcmp" compares strings and when it reaches a different character, it will return the difference between them.

In your case, it reaches 'c' in your first string, and 'C' in your second string. 'c' in hex is 0x63 while 'C' is 0x43. Subtract and you get 0x20, which is 32 in decimal.

We use strcmp to check if strings are equal if the function returns 0.

0

strcmp compares the strings character by character until it reaches characters that don't match or the terminating null-character.

so the strcmp function sees that c (which is 99 in ASCII) is greater than C (which is 67 in ascii), so it returns a positive integer. Whatever positive integer it returns is I think usually defined by your system or whatever version of c you are compiling.

MoMoe0
  • 65
  • 8