Implementation of strcmp

Uhm.. way too complicated. Go for this one: int strCmp(const char* s1, const char* s2) { while(*s1 && (*s1 == *s2)) { s1++; s2++; } return *(const unsigned char*)s1 – *(const unsigned char*)s2; } It returns <0, 0 or >0 as expected You can’t do it without pointers. In C, indexing an array is using … Read more

Inconsistent strcmp() return value when passing strings as pointers or as literals

TL:DR: Use gcc -fno-builtin-strcmp so strcmp() isn’t treated as equivalent to __builtin_strcmp(). With optimization disabled, GCC will only be able to do constant-propagation within a single statement, not across statements. The actual library version subtracts the differing character; the compile-time eval probably normalizes the result to 1 / 0 / -1, which isn’t required or … Read more

strcmp() return values in C [duplicate]

In this sense, “less than” for strings means lexicographic (alphabetical) order. So cat is less than dog because cat is alphabetically before dog. Lexicographic order is, in some sense, an extension of alphabetical order to all ASCII (and UNICODE) characters.

What does strcmp() exactly return in C?

From the cppreference.com documentation int strcmp( const char *lhs, const char *rhs ); Return value Negative value if lhs appears before rhs in lexicographical order. Zero if lhs and rhs compare equal. Positive value if lhs appears after rhs in lexicographical order. As you can see it just says negative, zero or positive. You can’t … Read more

Why does strcmp() return 0 when its inputs are equal?

strcmp returns a lexical difference (or should i call it “short-circuit serial byte comparator” ? 🙂 ) of the two strings you have given as parameters. 0 means that both strings are equal A positive value means that s1 would be after s2 in a dictionary. A negative value means that s1 would be before … Read more

How do I properly compare strings in C?

You can’t (usefully) compare strings using != or ==, you need to use strcmp: while (strcmp(check,input) != 0) The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.

strcmp refuses to work [closed]

First off, you’re allocating a lot of arrays in local variables in main(). That could lead to a stack overflow. You should probably use malloc() to allocate them instead. Second, this line is an assignment, not a comparison, which is a bug: if (succ = 0) Change it to this, if you want it to … Read more