Make sure you use strncmp and not strcmp. strcmp is deeply insecure .
BSD manpages (any nix will give you this information):
man strncmp int strncmp(const char *s1, const char *s2, size_t n);
The strcmp () and strncmp () functions compare lexicographically strings s1 and s2 with zero completion.
The strncmp () function compares no more than n characters. Since strncmp () is intended to compare strings, not binary data, the characters that appear after the `\ 0 'character are not compared.
strcmp () and strncmp () return an integer greater than, equal to, or less than 0, in accordance with the fact that the string s1 is greater than, equal to, or less than the string s2. Comparison is performed using unsigned characters, so \200' is greater than \ 0'.
From: http://www.codecogs.com/reference/c/string.h/strcmp.php?alias=strncmp
#include <stdio.h> #include <string.h> int main() { // define two strings s, t and initialize s char s[10] = "testing", t[10]; // copy s to t strcpy(t, s); // test if s is identical to t if (!strcmp(s, t)) printf("The strings are identical.\n"); else printf("The strings are different.\n"); return 0; }
禪 師 無 source share