How to compare two strings in C
From ArticleWorld
The standard C library does not define a real string type. Instead, a C string is an array of elements that have the char data type, and defines a comparison function, strcmp.
strcmp
strcmp is the name of the function that deals with the comparison of two strings. The standard C library defines the prototype of the strcmp function:
int strcmp(const char *s1, const char *s2);
The function returns an 0 if the strings are the same. If the first string (s1) is lexicographically greater than the second one, it will (s2), the function returns a value greater than 0. It will return a value smaller than 0 otherwise.
Therefore, in order to test whether the two strings are identical, you should use:
if (strcmp(s1,s2) == 0) {...}
strncmp
For greater flexibility, the C standard library also contains a function called strncmp:
int strncmp(const char *s1, const char *s2, size_t len);
This function has the same return values as strcmp and essentially behaves just like it. The main difference though is that strncmp will limit the comparison to the first len characters. Therefore, to check if the first 8 characters of s1 and s2 are identical, we could use:
if(strncmp(s1,s2,(8 * sizeof(char)))) == 0) {...}
Portability
The strcmp function is a standard POSIX, ISO and ANSI one, so chances are you will find it in any decent C compiler.