. Advertisement .
..3..
. Advertisement .
..4..
Characters in C are used for storing letters and characters. However, since C only stores integers, the type of character is also an integer. Characters in C will be stored in a memory of 1 byte which has values ranging from -128 to 127 or 0 to 255. This post will provide you with guidance on how to make a char comparison in C. Scroll down to explore the useful information!
How to Make a Char Comparison in C
Using Comparison Operators
For representing characters in C, each integer must be mapped with a correlating character by utilizing a code of number. Accordingly, ASCII is the widely used number code.
In another word, a character in C can hold an ASCII value which indicates a number ranging from 0 to 127 instead of the character itself. Hence, this integer represents the character’s ASCII code and the character comparison in C is based on the ASCII value.
The below coding example will demonstrate how to make a char comparison in C by using comparison operators.
#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.
Using the strcmp() Method
The function strcmp() in C programming language is known as a built-in function. The prototyping and definition of this function are contained in the header file <string.h>
.
By using strcmp()
, strings will be taken as arguments and traversed for comparing and evaluating their equality. This technique will compare strings lexicographically, which means that each character will be checked at each index with both strings.
Hence, the function strcmp()
is performed to make a strings comparison. If there are 2 of the same strings then strcmp()
will return 0. If not, it will return a value except 0.
This function also uses ASCII values for comparing strings characters. The comparing implementation will happen until the characters of the 2 strings are different or reach the null character (\0
).
Syntax:
int strcmp (const char* firstStringValue, const char* secondStringValue);
The return of the function of strcmp()
will be an integer value which is calculated based on the first mismatch among characters of the 2 strings. There are 3 typical return values types by using the function strcmp()
:
- Zero (0): If 2 strings are the same, strcmp() will return 0. This means that the characters of both strings are equivalent.
- Larger than 0 ( >0 ): If the first unmatched character of string 1 has an ASCII value larger than the interrelating character of string 2, strcmp() will return “>0”.
- Lower than 0 ( <0 ): If the first unmatched character of string 1 has an ASCII value lower than the interrelating character of string 2, strcmp() will return “<0”.
The completed coding demonstration for how to make a char comparison in C is as the following:
#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;
}
Output:
strcmp(firstString, secondString) = 0
strcmp(firstString, thirdString) = 1
Summary
This blog has provided you with specific coding tutorials on how to make a char comparison in C by using the function strcmp()
and comparison operators. With the explanation above, hope that these 2 methods are easy for you to understand and practice. More amazing coding tutorials have been updated on our website. You can explore other posts for another useful guide.
Leave a comment