strcmp() Compare two complete strings

strcmp() Compare two complete strings

strcmp() compares unsigned char values and returns negative, zero, or positive. Only the sign matters; it is case-sensitive and ignores locale collation.

Categorystring.h strings
KindLibrary function
Header<string.h>
C standardC89 / C90

📝 Syntax

int strcmp(const char *str1, const char *str2);

⚙️ Parameters

str1 RequiredThe first null-terminated string.
str2 RequiredThe second null-terminated string.

Returns: Returns a negative value, zero, or a positive value when str1 is less than, equal to, or greater than str2, respectively.

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

Equal result: 0
apple before apply: yes
⚠️ Warning Rely only on the sign of the result; implementations need not return exactly -1, 0, or 1.
💡 Tip Comparison is case-sensitive and uses byte values rather than locale-specific natural collation rules.

📚 Related Tutorials

❓ FAQ

QDoes strcmp always return -1, 0, or 1?
ANo. Equality produces zero, but unequal strings are guaranteed only a positive or negative result, not a particular magnitude.
QIs strcmp case-sensitive?
AYes. For example, "Apple" and "apple" differ. Case-insensitive comparison requires an explicit choice of character and language rules.
QCan strcmp compare strings using natural collation rules for Chinese or other languages?
ANo. strcmp compares unsigned char byte values one by one, with no understanding of linguistic ordering, accent combinations, or localization rules, and it is case-sensitive. Consider strcoll() when you need the current locale's collation rules. For full Unicode text, also define the encoding and use a library that supports the appropriate collation.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏