Compare characters in c

  1. HowTo
  2. C Howtos
  3. Compare Char in C

Created: December-15, 2020 | Updated: October-17, 2021

  1. Compare Char in C Using the Comparison Operators
  2. Compare Char in C Using the strcmp() Function in C

This tutorial introduces how to compare char in C. A char variable is an 8-bit integral value, from 0 to 255. Here, 0 stands for the C-null character, and 255 stands for an empty symbol.

Compare Char in C Using the Comparison Operators

A char variable has its own ASCII value. So the characters are compared according to the ASCII values. The complete program is as below:

#include<stdio.h> int main(void) { char firstCharValue='m'; char secondCharValue='n'; if(firstCharValue < secondCharValue) printf("%c is smaller than %c.", firstCharValue, secondCharValue); if(firstCharValue > secondCharValue) printf("%c is smaller than %c.", firstCharValue, secondCharValue); if(firstCharValue == secondCharValue) printf("%c is equal to %c.", firstCharValue, secondCharValue); return 0; }

Output:

m is smaller than n.

Compare Char in C Using the strcmp() Function in C

The strcmp() function is defined in the string header file and used to compare two strings character by character.

If both strings’ first characters are equal, the next character of the two strings will be compared. It continues till the corresponding characters of both strings are either different or a null character '\0' is reached.

The syntax for strcmp() function is as below.

int strcmp (const char* firstStringValue, const char* secondStringValue);
  • If two strings are equal or identical, it returns 0.
  • If the ASCII value of the first unmatched character is greater than the second, It returns a positive integer value
  • If the ASCII value of the first unmatched character is less than the second,it returns a negative integer value.

The complete program to compare two strings is as below:

#include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { char firstString= "b", secondString= "b",thirdString= "B"; int result; result= strcmp(&firstString, &secondString); printf("strcmp(firstString, secondString) = %d\n", result); result = strcmp(&firstString, &thirdString); printf("strcmp(firstString,thirdString) = %d\n", result); return 0; }

The output is:

strcmp(firstString, secondString) = 0 strcmp(firstString, thirdString) = 1

Write for us

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C Char

  • Get Length of Char Array in C
  • Use the getchar Function in C
  • Convert Integer to Char in C
  • Compare characters in c

    Can you compare characters in C?

    Compare Char in C Using the strcmp() Function in C The strcmp() function is defined in the string header file and used to compare two strings character by character. If both strings' first characters are equal, the next character of the two strings will be compared.

    Can I use == to compare characters?

    Yes, char is just like any other primitive type, you can just compare them by == .

    How do you compare characters?

    You can compare Character class objects:.
    Using Character.compare(char x, char y) Using Character class constructor, we can convert the char primitive value to the Character object. ... .
    Using equals() method. Using equals() method also we can compare the Character class objects..

    Do you use == or .equals for char?

    equals() is a method of all Java Objects. But char is not an Object type in Java, it is a primitive type, it does not have any method or properties, so to check equality they can just use the == equals operator.