The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Strcmp() returns wrong value
Discuss Strcmp() returns wrong value in the C Programming forum on Dev Shed. Strcmp() returns wrong value C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 11th, 2013, 11:06 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 111
Time spent in forums: 1 Day 4 h 5 m 15 sec
Reputation Power: 1
|
|
|
Strcmp() returns wrong value
here's the code for example:
Code:
int main()
{
char str1[15], str2[15];
printf("Enter first string:");
scanf("%s", str1);
printf("Enter second string:");
scanf("%s", str2);
printf("%d", strcmp(str1,str2));
}
here's the output:
Code:
Enter first string: hello
Enter second string: world
-15
what's wrong with it?
why wouldn't it return 1 as it should?
|

January 11th, 2013, 11:56 AM
|
 |
Contributed User
|
|
|
|
|
Why would you expect it to return 1?
The spec for strcmp reads
<0 if a is before b
>0 if a is after b
== 0 if they're the same
You have -15, indicating "hello" is before "world" (which it is).
|

January 12th, 2013, 04:27 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 111
Time spent in forums: 1 Day 4 h 5 m 15 sec
Reputation Power: 1
|
|
for some reason (maybe i read it somwhere) i thought that if it reurns 1 then first string is before second string, 0 if they're the same, and -1 if second string is before the first string...
thank you!
|

January 13th, 2013, 12:00 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Some languages have a comparison function like strcmp but specifically return -1, 0, or 1. The C function returns the difference between the two strings (as you may have realized) and thus merely guarantees a negative, zero, or positive number.
As for returning 1/positive if the first string < second string, I'm not sure where you got that from. No offense but you might just be remembering it backwards.
|

January 13th, 2013, 03:25 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by so.very.tired for some reason (maybe i read it somwhere) i thought that if it reurns 1 then first string is before second string, 0 if they're the same, and -1 if second string is before the first string... |
Reading it somewhere is good; but perhaps you should have checked before posting a question. There are many sources for documentation of teh standard library; here's one. If the documentation only guarantees <0, 0, or >0, even if you observe -1, 0 and 1 in any particular implementation, you cannot assume that that will always be the case.
In this case -15 is the result of performing 'w' - 'h' which gives a clue as to how strcmp() is working in this case. It is simply performing a numerical comparison of the character codes, and returns the difference between the first two non-matching characters.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|