C program to check lowercase or uppercase using macro

Quick links

Write a C program to check whether a character is uppercase or lowercase using macro. Logic to check uppercase or lowercase character using macro in C. How to check whether a character is uppercase or lowercase using macro in C program.

During the course of this macro exercises, in last post we discussed how easily we can add conditions to our macro. We learned to find maximum or minimum between two numbers using macro.

In this post we will continue further with string operation. I will explain how easily you can transform logic to check uppercase and lowercase character to macro.

Required knowledge

Basic C programming, Macros, Conditional operator, String

How to check uppercase and lowercase character using macro?

Before moving ahead, I assume that you are aware with macro syntax, how to define and use

Lets define two macro that accepts an arguments say IS_UPPER(x) and IS_LOWER(x). Both the macro should return boolean true (1) or false (0) based on their characteristics.

Example:

#define IS_UPPER(x) (x >= 'A' && x <= 'Z')
#define LOWER(x) (x >= 'a' && x <= 'z')

Program to check uppercase and lowercase using macro

/**
 * C program to check uppercase and lowercase using macro
 */

#include <stdio.h>

// Macro definitions
#define IS_UPPER(x) (x >= 'A' && x <= 'Z')
#define IS_LOWER(x) (x >= 'a' && x <= 'z')

int main()
{
    char ch;

    // Input a character from user
    printf("Enter any character: ");
    ch = getchar();

    if (IS_UPPER(ch))
        printf("'%c' is uppercase\n", ch);
    else if (IS_LOWER(ch))
        printf("'%c' is lowercase\n", ch);
    else 
        printf("Entered character is not alphabet");

    return 0;
}

Output

Enter any character: C
'C' is uppercase

You can extend the logic further to check alphabets, digits, alphanumeric, vowels, consonants, special characters etc. Below is the list of macro definitions to check all.

#define IS_UPPER(x) (x >= 'A' && x <= 'Z')
#define IS_LOWER(x) (x >= 'a' && x <= 'z')
#define IS_ALPHABET(x) (IS_LOWER(x) || IS_UPPER(x))

#define IS_VOWEL_LOWER(x) (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
#define IS_VOWEL_UPPER(x) (x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')
#define IS_VOWEL(x) (IS_VOWEL_LOWER(x) || IS_VOWEL_UPPER(x))

#define IS_DIGIT(x) (x >= '0' && x <= '9')
#define IS_ALPHANUMERIC(x) (IS_ALPHABET(x) || IS_DIGIT(x))

#define IS_WHITE_SPACE(x) (x == ' ' || x == '\t' || x == '\r' || x == '\n' || x == '\0')

#define IS_SPECIAL_CHARACTERS(x) (x >= 32 && x <= 127 && !IS_ALPHABET(x) && !IS_DIGIT(x) && !IS_WHITE_SPACE(x))

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols. as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>