I am having some difficulty locating information of comparing C strings. I understand that unlike C++, C does not support operator overloading, so I’m wondering if there is any way to check if one string is greater/less than another (e.g. str1 > str2)?
Thanks ahead of time for your responses. This is honestly one of the first times I have actually had to ask a question because I could not find a related post.
There are several, each serving different purposes (omitting wide character variants for now).
strcmp– compares two strings, character by character (with the C notion of what strings are equal or not – that doesn’t need to coincide with how humans think – seestrcoll). There’s a variant for comparing only the first at most n characters,strncmp.strcasecmp– compares two strings, ignoring case. There’s a variant for comparing only the first at most n characters,strncasecmp.strcoll– compares two strings, observing the currently set locale (which is why it’s called collation, not comparing in this case). If you wantssandßto compare equal for a German audience, then this is what you should use.Where you might write
in a language, you have to write
in C. Essentially you move both operands into the function call, retain the comparison operator and compare with
0instead.