How to compare ASCII values in C

How to Compare ASCII Values in C Programming

ASCII values are the numeric representations of characters in the C programming language. One of the most common tasks for programming is comparing these values, often if this work involves operations on the strings or characters. Thus, in this tutorial, we will find out how to efficiently compare ASCII values in C and learn more about how characters are manipulated.

Understanding ASCII Values in C

Remember, every character in C (letter or symbol) has an ASCII value (which is an integer that represents the letter/symbol/). For example, the ASCII value of ‘A’ is 65 whereas the ASCII value of ‘a’ is 97. You can simply use relational operators like <, >, ==, etc to compare two ASCII values in C.

Using Relational Operators to Compare ASCII Values

C allows you to compare two characters as if they were filled with two integers. Every character in JAVA has been derived from the type int (ASCII value) so you can compare or even perform any other operations on them using relational operators. Here’s an example:

#include <stdio.h>

int main() {
    char char1 = 'A';
    char char2 = 'B';

    if (char1 < char2) {
        printf("Character %c has a smaller ASCII value than %c\n", char1, char2);
    } else {
        printf("Character %c has a larger or equal ASCII value compared to %c\n", char1, char2);
    }

    return 0;
}

Here the program checks that the ASCII value of ‘A’ and ‘B’ is. ‘ A’ would have a smaller ASCII value hence the same will be reflected in the output.

Comparing ASCII Values in Strings

When you compare two strings, C will compare ASCII values of the characters in the strings one by one. Comparison ends at either finding a difference, or end of the string. How to Compare Strings using strcmp

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Apple";
    char str2[] = "Banana";

    int result = strcmp(str1, str2);

    if (result < 0) {
        printf("%s comes before %s\n", str1, str2);
    } else if (result > 0) {
        printf("%s comes after %s\n", str1, str2);
    } else {
        printf("%s and %s are equal\n", str1, str2);
    }

    return 0;
}

In this case, strcmp compares the ASCII values of the characters in the str1 and str2 string values. If the first one has a lesser ASCII value, it returns negative, or else the positive value is returned.

ASCII Comparison in a Loop

A case for using — This solution is useful when there are longer strings or more comparisons that may need to be made. For example iterating a string and comparing ASCII values like so:

#include <stdio.h>

int main() {
    char str[] = "Hello";
    int i;

    for (i = 0; str[i] != '\0'; i++) {
        printf("Character: %c, ASCII Value: %d\n", str[i], str[i]);
    }

    return 0;
}

This code will output the ASCII values of the letters inside the “Hello” string. You can adjust this tactic and make comparisons as method based on your needs.

Conclusion

Leave a Comment