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
|
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
|
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
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
|
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
|
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