Comparing Strings

How to Write a C Program to Compare Two Strings

Write a C program that compares two strings with strcmp(), reads input with fgets(), and checks the result. Full code, with notes on common errors.

On this page
  1. A complete program
  2. Compiling and running
  3. Reading two strings from the keyboard
  4. Writing your own comparison function
  5. Mistakes to avoid

The short version is to call strcmp() with the two strings and check whether the result is 0. This article shows a complete program, a version that reads input from the keyboard, and a version without the library function, so you can see how the comparison works. If char arrays are new to you, start with what are strings in C with examples?

A complete program

#include <stdio.h>
#include <string.h>

int main(void) {
    char first[] = "orange";
    char second[] = "orange";

    if (strcmp(first, second) == 0) {
        printf("The strings are equal.\n");
    } else {
        printf("The strings are different.\n");
    }

    return 0;
}

Output:

The strings are equal.

strcmp() returns 0 here, so the first branch runs. If you change one array to "Orange", the program prints “different”, because 'O' and 'o' are different characters.

Compiling and running

Save the code as compare.c and build it with GCC:

gcc -Wall -Wextra -o compare compare.c
./compare

The -Wall and -Wextra flags ask the compiler to report common problems. Other C compilers work the same way for this program.

Reading two strings from the keyboard

This version reads each line with fgets(), then compares the two results:

#include <stdio.h>
#include <string.h>

int main(void) {
    char first[100];
    char second[100];

    printf("Enter the first string: ");
    fgets(first, sizeof(first), stdin);
    first[strcspn(first, "\n")] = '\0';

    printf("Enter the second string: ");
    fgets(second, sizeof(second), stdin);
    second[strcspn(second, "\n")] = '\0';

    int result = strcmp(first, second);

    if (result == 0) {
        printf("The strings are equal.\n");
    } else if (result < 0) {
        printf("The first string comes before the second.\n");
    } else {
        printf("The first string comes after the second.\n");
    }

    return 0;
}

Sample run:

Enter the first string: apple
Enter the second string: banana
The first string comes before the second.

fgets() keeps the newline at the end of the line. Without removing it, "apple\n" would not match "apple". The line that calls strcspn() finds the newline and replaces it with the terminator. If a line has no newline, because it was longer than the buffer, the same line leaves the string as it is.

This example skips error checks to stay short. In real code, check the value that fgets() returns, since it is NULL when reading fails. For more on reading text from the keyboard, see c input string.

Writing your own comparison function

You can compare two strings without the library by walking through both of them together:

#include <stdio.h>

int strings_equal(const char *a, const char *b) {
    while (*a != '\0' && *b != '\0') {
        if (*a != *b) {
            return 0;
        }
        a++;
        b++;
    }
    return *a == *b;
}

int main(void) {
    printf("%d\n", strings_equal("cat", "cat"));
    printf("%d\n", strings_equal("cat", "cats"));
    return 0;
}

Output:

1
0

The loop stops at the first mismatch, or when either string reaches its terminator. If the loop ends because one string is shorter, the terminator and the other character differ, so the function returns 0. The final line handles the case where both strings end at the same point. This version is for learning. strcmp() is the standard choice, and it also tells you which string sorts first.

Mistakes to avoid

  • Using == on two char arrays. The check compares their addresses, so two separate arrays with the same text are not equal this way.
  • Forgetting that strcmp() returns 0 for a match. A non-zero result means the strings differ.
  • Keeping the newline from fgets(). Input such as "apple\n" never matches "apple" until the newline is removed.
  • Using a buffer that is too small. fgets() stops at the buffer size, so a long line is split across reads.

Whether to use == at all is covered in should I use equals () or == for string compare?

The complete guide to comparing strings

How to Compare Two Strings in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

What Is long long in C++?

long long is a C++ integer type at least 64 bits wide. Learn its range, how to declare and print it, and when to use it instead of int, with examples.

long long 5 min read