Using Macros Check For Uppercase / Lowercase and Alphabet or Not and Biggest of 2 Numbers: C Program


Problem State: Write down macro definitions for the following:
1. To test whether a character is a small case letter or not.
2. To test whether a character is an upper case letter or not.
3. To test whether a character is an alphabet or not. Make use of the macros you defined in 1 and 2 above.
4. To obtain the bigger of two numbers.

Related Read:
Switch Case Default In C Programming Language
Macros With Arguments: C Program
Biggest of Two Numbers Using Ternary Operator: C
C Program To Print Uppercase Alphabet(A-Z) using While loop
C Program To Print Lowercase Alphabet(a-z) using While loop
C Program To Check Whether a Character is an Alphabet or Not

Problem Statement Analysis

1. We need to write 4 Macro definitions.
2. We must write macros to find upper and lower case, and then make use of these two macros to find if user entered character is alphabet or not.
3. We can find biggest of 2 numbers by using Ternary / conditional operator in macro expansion.

Video Tutorial: Using Macros Check For Uppercase / Lowercase and Alphabet or Not and Biggest of 2 Numbers: C Program


[youtube https://www.youtube.com/watch?v=EEGLYe2buxk]

YouTube Link: https://www.youtube.com/watch?v=EEGLYe2buxk [Watch the Video In Full Screen.]

Source Code: Using Macros Check For Uppercase / Lowercase and Alphabet or Not and Biggest of 2 Numbers: C Program

#include<stdio.h>

#define isUPPER(ch) ( ch >= 'A' && ch <= 'Z' )
#define isLOWER(ch) ( ch >= 'a' && ch <= 'z' )
#define isALPHABET(ch) ( isUPPER(ch) || isLOWER(ch) )
#define BIGGEST(a, b) ( ( a > b ) ? \
                       printf("%d is the biggest\n", a) : \
                       printf("%d is the biggest\n", b) )

int main()
{
    int a, b, repeat;
    char ch, choice;

    do
    {
        printf("1. Check if entered character is upper or lower case\n");
        printf("2. Check if entered character is alphabet or not\n");
        printf("3. Find biggest of 2 numbers\n");

        printf("\nEnter your choice\n");
        scanf(" %c", &choice);

        switch(choice)
        {
            case '1': printf("\nEnter a character\n");
                      scanf(" %c", &ch);

                      if( isUPPER(ch) )
                      {
                          printf("Entered character is upper case letter\n");
                      }
                      else if( isLOWER(ch) )
                      {
                          printf("Entered character is lower case letter\n");
                      }
                      else
                      {
                          printf("Please enter a valid alphabet\n");
                      }

                      break;
            case '2': printf("\nEnter a character\n");
                      scanf(" %c", &ch);

                      if( isALPHABET(ch) )
                      {
                          printf("Entered character is an alphabet\n");
                      }
                      else
                      {
                          printf("Entered character is not an alphabet\n");
                      }
                      break;
            case '3': printf("\nEnter 2 numbers\n");
                      scanf("%d%d", &a, &b);

                      BIGGEST(a, b);

                      break;
            default:  printf("\nPlease enter valid choice\n");
        }

        printf("\n\nDo you want to continue? Ans: 0 or 1\n");
        scanf("%d", &repeat);

        printf("\n");
        fflush(stdin);

    }while(repeat);

    return 0;
}

Output:
1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
1

Enter a character
S
Entered character is upper case letter

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
1

Enter a character
s
Entered character is lower case letter

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
1

Enter a character
$
Please enter a valid alphabet

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
2

Enter a character
A
Entered character is an alphabet

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
2

Enter a character
&
Entered character is not an alphabet

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
3

Enter 2 numbers
14
50
50 is the biggest

Do you want to continue? Ans: 0 or 1
0

Here we are using do-while loop to repeatedly show the user choices: If user enters 1, the choices are shown once again. If the user enters 0, then control exits the do-while loop.

Choice 1: Upper or Lower Case Alphabet

All the characters have ASCII value associated with it in C programming. So internally it checks the ASCII value of user entered character against the ASCII values of “A” to “Z”.

ASCII value range of upper case alphabets:
ASCII value of A is 65.
ASCII value of B is 66.
ASCII value of C is 67.

and so on till Z ..

ASCII value of Z is 90.

So all the ASCII values between 65 and 90 (including 65 and 90) are Capital letter alphabets.

Similarly, below we’ve listed the ASCII values of lower case alphabets.

ASCII value range of lower case alphabets:
ASCII value of a is 97.
ASCII value of b is 98.
ASCII value of c is 99.

and so on till z ..

ASCII value of z is 122.

So all the ASCII values between 97 and 122 (including 97 and 122) are Lower case letter alphabets.

Related Read:
C Program To Print Uppercase Alphabet(A-Z) using While loop
C Program To Print Lowercase Alphabet(a-z) using While loop

Choice 2: Alphabet or Not

According to our problem statement we need to use the macros we defined for “Choice 1” to evaluate if the user entered character is alphabet or not. So if the user entered character is upper or lower case latter than its alphabet or else its not an alphabet.

Related Read:
C Program To Check Whether a Character is an Alphabet or Not

Choice 3: Biggest of Two Numbers

Inside macro expansion we make use of ternary / conditional operator to find biggest of 2 numbers.

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

Bug in accepting character as input

When you input some data via console window and hit enter key, the enter key or the new line character gets stored inside input buffer. If you’re accepting a single character from keyboard via scanf() function, often times it gets confused with the input buffer and thinks that the user has pressed the enter key as the input character. We can avoid it in 3 ways:

1. Use double scanf() function, as illustrated in above video tutorial.
2. Use a space before %c inside scanf() method.
3. Use fflush(stdin) before every scanf() method which accepts a single character value.

Note: Since we might start to input information from the keyboard repeatedly inside do-while block, scanf() method keeps checking the input buffer. And often times it gets confused with the input buffer and thinks that the user has pressed the enter key. To avoid that we flush out the previous buffer present in input device(ex: keyboard) using function fflush(). fflush takes stdin as argument, so that it can clear the buffer of standard input device. fflush(stdin);

#include<stdio.h>

#define isUPPER(ch) ( ch >= 'A' && ch <= 'Z' )
#define isLOWER(ch) ( ch >= 'a' && ch <= 'z' )
#define isALPHABET(ch) ( isUPPER(ch) || isLOWER(ch) )
#define BIGGEST(a, b) ( ( a > b ) ? \
                       printf("%d is the biggest\n", a) : \
                       printf("%d is the biggest\n", b) )

int main()
{
    int a, b, repeat;
    char ch, choice;

    do
    {
        printf("1. Check if entered character is upper or lower case\n");
        printf("2. Check if entered character is alphabet or not\n");
        printf("3. Find biggest of 2 numbers\n");

        printf("\nEnter your choice\n");
        scanf("%c", &choice);

        switch(choice)
        {
            case '1': printf("\nEnter a character\n");
                      fflush(stdin);
                      scanf("%c", &ch);

                      if( isUPPER(ch) )
                      {
                          printf("Entered character is upper case letter\n");
                      }
                      else if( isLOWER(ch) )
                      {
                          printf("Entered character is lower case letter\n");
                      }
                      else
                      {
                          printf("Please enter a valid alphabet\n");
                      }

                      break;
            case '2': printf("\nEnter a character\n");
                      fflush(stdin);
                      scanf("%c", &ch);

                      if( isALPHABET(ch) )
                      {
                          printf("Entered character is an alphabet\n");
                      }
                      else
                      {
                          printf("Entered character is not an alphabet\n");
                      }
                      break;
            case '3': printf("\nEnter 2 numbers\n");
                      scanf("%d%d", &a, &b);

                      BIGGEST(a, b);

                      break;
            default:  printf("\nPlease enter valid choice\n");
        }

        printf("\n\nDo you want to continue? Ans: 0 or 1\n");
        scanf("%d", &repeat);

        printf("\n");
        fflush(stdin);

    }while(repeat);

    return 0;
}

In above source code we’re making use of fflush(stdin) before every scanf() method which accepts single character. fflush(stdin) flushes out the previous buffer present in input device(ex: Keyboard).

Note: We can continue writing macro expansion in next line by making use of macro continuation operator(\). You can see that we’ve broken the line and written the code in next line inside macro expansion to find biggest of 2 numbers.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *