This C program compares two strings and reports whether they are equal, or which one comes first in dictionary (lexicographic) order. Comparison works character by character: walk both strings together until two characters differ or a string ends, then decide from that first difference.
This page shows a manual character-by-character implementation and the standard library strcmp() shortcut, plus strncmp() for length-limited comparison.
C Program to Compare Two Strings — Manual Method
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[100], str2[100];
int i = 0, result;
printf("Enter the first string : ");
fgets(str1, sizeof str1, stdin);
printf("Enter the second string : ");
fgets(str2, sizeof str2, stdin);
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
while (str1[i] != '\0' && str2[i] != '\0' && str1[i] == str2[i])
i++;
result = (unsigned char)str1[i] - (unsigned char)str2[i];
if (result == 0)
printf("The two strings are equal.\n");
else if (result < 0)
printf("\"%s\" comes before \"%s\".\n", str1, str2);
else
printf("\"%s\" comes after \"%s\".\n", str1, str2);
return 0;
}
How to Compile and Run
gcc -Wall -o comparestr comparestr.c
./comparestr
Sample Output
Enter the first string : apple Enter the second string : apricot "apple" comes before "apricot". Enter the first string : hello Enter the second string : hello The two strings are equal. Enter the first string : zebra Enter the second string : ant "zebra" comes after "ant".
How the Program Works
fgets+strcspn:fgetsreads a whole line safely without buffer overflow. It keeps the trailing\n, sostrcspn(s, "\n")finds it and replaces it with\0.- The loop: Advances index
iwhile both strings have characters that match. It stops at the first difference, or when either string ends. (unsigned char)cast: The subtractionstr1[i] - str2[i]needs unsigned chars to avoid undefined behavior on platforms wherecharis signed and characters above ASCII 127 appear.- Result zero: Both strings matched all the way to their null terminators — they are equal.
This replaces the common bug seen in older textbook code: str1[i] != '' — an empty character constant '' is invalid C. The correct null terminator is '\0'.
Using strcmp() — The Standard Library Way
In real code, use strcmp() from <string.h>. It does the same comparison and returns a negative, zero, or positive value:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[100], str2[100];
printf("Enter first string : ");
fgets(str1, sizeof str1, stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter second string : ");
fgets(str2, sizeof str2, stdin);
str2[strcspn(str2, "\n")] = '\0';
int r = strcmp(str1, str2);
if (r == 0) printf("Equal\n");
else if (r < 0) printf("\"%s\" comes first\n", str1);
else printf("\"%s\" comes first\n", str2);
return 0;
}
strncmp() — Compare Only the First N Characters
strncmp(s1, s2, n) compares at most n characters. Useful when you only care about a prefix:
/* Check if both strings start with "pre" */
if (strncmp(str1, "pre", 3) == 0)
printf("str1 starts with 'pre'\n");
Key strcmp() Rules
- Return value: negative if s1 < s2, zero if equal, positive if s1 > s2. The exact non-zero values are implementation-defined — only the sign matters.
- Never use
==to compare strings in C.str1 == str2compares pointers (addresses), not the string content. - Case-sensitive:
strcmp("Hello", "hello")returns non-zero. For case-insensitive comparison, usestrcasecmp()(POSIX) or convert both strings to lowercase first.
What This Program Teaches
- Why
'\0'(null terminator) is the right sentinel for string loops — empty character literals''are illegal in C - The
(unsigned char)cast pattern when doing arithmetic oncharvalues - Why pointer equality (
==) does not compare string content — always usestrcmp() - How
fgets+strcspnreplaces the dangerousgets()for safe line input
Related Programs
- Anagram Program in C
- C Program to Check String Palindrome
- C Program to Sort a String
- C Aptitude Questions and Answers
As an Amazon Associate we earn from qualifying purchases.
Recommended Book
Strings, pointers, and the standard library are covered in chapters 5 and 6 of The C Programming Language by Kernighan & Ritchie. Also on Amazon.com.