Open In App
Related Articles

Char Comparison in C

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Char is a keyword used for representing characters in C. Character size in C is 1 byte.

There are two methods to compare characters in C and these are:

  • Using ASCII values
  • Using strcmp( ) .

1. Using ASCII values to compare characters

The first method is pretty simple, we all know that each character can be in uppercase or lowercase and has a different ASCII value. So, we can directly compare their ASCII values to see which character is greater or lesser than the other.

  • if diff between first character and second character is >0 -> First character is Greater between the two.
  • if diff between first character and second character is <0 -> Second character is Greater between the two.
  • if diff between first character and second character is =0 -> Both Characters are equal

Example:

C

// C Program to compare
// characters using ASCII values
#include <stdio.h>
 
// Function to compare char
void compare(char a, char b)
{
    if (a == b)
        printf("Both are equal %c and %c\n", a, b);
    else
        printf("%c and %c are not equal\n", a, b);
}
 
int main()
{
 
    // char declared
    char x = 'g';
    char y = 'G';
    char z = 'g';
 
    // characters compared
    compare(x, y);
    compare(y, z);
    compare(x, z);
 
    return 0;
}

                    

Output
g and G are not equal
G and g are not equal
Both are equal g and g

2. Using strcmp( ) to compare characters

The second method that can be used is the strcmp() function defined in the string header file in the C library. The strcmp() function compares two strings character by character. The first character in both the strings is compared followed by the subsequent characters. 

Return type:

if diff between first unmatched character and second unmatched character is >0 -> First character is Greater between the two.
if diff between first unmatched character and second unmatched character is <0 -> Second character is Greater between the two.
if diff between first character and second character is =0 -> Both Characters are equal

Syntax:

int strcmp (const char* String1, const char* String2);

Example: 

C

#include <stdio.h>
#include <string.h>
 
int main()
{
 
    // code
    char a[] = "g";
    char b[] = "G";
    char c[] = "g";
    int output;
 
    output = strcmp(a, b);
    printf("The Comparison value between %s and %s is %d\n",
           a, b, output);
 
    output = strcmp(b, c);
    printf("The Comparison value between %s and %s is %d\n",
           b, c, output);
 
    output = strcmp(a, c);
    printf("The Comparison value between %s and %s is %d\n",
           a, c, output);
 
    return 0;
}

                    

Output
The Comparison value between g and G is 32
The Comparison value between G and g is -32
The Comparison value between g and g is 0


Last Updated : 12 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads