logo
Course thumb

The C programming language made simple

Comparing strings in C

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { char str1[] = "abcd1"; char str2[] = "abcd4"; printf("%d\n", strcmp(str1, str2)); printf("%d\n", strncmp(str1, str2, 4)); char arr1[] = {1, 2, 3}; char arr2[] = {1, 2, 4}; printf("%d\n", memcmp(arr1, arr2, 3 * sizeof(int))); return 0; }