Char Comparison in C Last Updated : 12 Dec, 2022 Comments Improve Suggest changes Like Article Like 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 valuesUsing 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; } Outputg and G are not equal G and g are not equal Both are equal g and g2. 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; } OutputThe 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 Comment More infoAdvertise with us Next Article Char Comparison in C R raj2002 Follow Improve Article Tags : Technical Scripter C Language Technical Scripter 2022 C-Data Types Similar Reads How to Compare Characters in C++? char in c is a keyword used for representing character data type. The memory size of char is 1 byte containing numbers, alphabets, and alphanumeric characters. We can compare the characters in C using 2 different ways: Comparison using ASCII values.Using the built-in function.1. Using ASCII Values A 3 min read What is a Character (CHAR)? In progrаmmiÕ¸g, dаtа is stored аոd mаnupulаted ÑÕ¸ vаrious forms, kÕ¸owÕ¸ аs dаtа types. OÕ¸e fuÕ¸dаmeÕ¸tаl dаtа type is the chаrаcter, ofteÕ¸ly аbbreviаted аs chаr. UÕ¸derstаոdiÕ¸g chаrаcters аոd their use ÑÕ¸ progrаmmiÕ¸g is crucÑаl for hаոdliÕ¸g text, symbols, аոd more complex dаtа structures effectively. Wh 3 min read CHAR_BIT in C CHAR_BIT : It is the number of bits in char. These days, almost all architectures use 8 bits per byte (But it is not the case always, some older machines used to have 7-bit byte). It can be found in Let us see an application of it. Suppose we wish to print byte by byte representation of an integer. 1 min read unsigned char in C with Examples char is the most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. Now character datatype can be divided into 2 types: signed char unsigned char unsigned char is a character datatype where the variable consumes all the 8 bits of the memo 2 min read How to Empty a Char Array in C? Prerequisite: Char Array in C Char arrays are used for storing multiple character elements in a continuous memory allocated. In a few questions, we have the necessity to empty the char array but You canât clear a char array by setting the individual members to some value indicating that they donât c 4 min read Like