Skip to content

Instantly share code, notes, and snippets.

@lyleunderwood
Created December 21, 2011 00:39
Show Gist options
  • Save lyleunderwood/1503973 to your computer and use it in GitHub Desktop.
Save lyleunderwood/1503973 to your computer and use it in GitHub Desktop.
program to test behavior of c strcmp
#include <string.h>
/*************************
* strcmp_test
*
* Outputs basic test of strcmp as defined in ascii c string.h by default. Pass
* exactly two parameter strings in order to test those two strings.
*
* Examples:
* $ ./strcmp_test
* (default test output)
*
* $ ./strcmp_test abc bcd
* Test result for "abc" VS. "bcd":
* -1
*
* $ ./strcmp_test "multi-word string" "and with spaces"
* Test result for "multi-word string" VS. "and with spaces":
* 1
*************************/
void compare_test(const char *a, const char *b)
{
printf("Test result for \"%s\" VS. \"%s\":\n", a, b);
int result = strcmp(a, b);
printf("%d\n\n", result);
}
void perform_default_test()
{
compare_test("abc", "bcd");
compare_test("bcd", "abc");
compare_test("abc", "abc");
compare_test("ABC", "bcd");
compare_test("abc", "BCD");
}
int main(int argc, char *argv[])
{
if (argc == 3) {
compare_test(argv[1], argv[2]);
}
else
{
perform_default_test();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment