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

C Program To Find Lowercase Alphabet or Not using Conditional Operator

Using Conditional Operator / Ternary Operator determine, whether the character entered through the keyboard is a lower case English alphabet or not.

Also Check:
C Program To Find Special Symbol or Not using Conditional Operator

Related Read:
Relational Operators In C
Logical Operators In C
Ternary Operator / Conditional Operator In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a character
a

Output:
Character entered is a lowercase English alphabet

Logic To Find Lowercase Alphabet or Not using Conditional Operator

Using Conditional Operator we write the condition, if user entered character is greater than or equal to ASCII Value 97(which corresponds to lowercase character a) and less than or equal to ASCII Value 122(which corresponds to lowercase character z).

If the condition is true, then user entered character is lower case English alphabet, if not, then its not a lowercase English alphabet.

Video Tutorial: C Program To Find Lowercase Alphabet or Not using Conditional Operator


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

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

Source Code: C Program To Find Lowercase Alphabet or Not using Conditional Operator

#include < stdio.h >

int main()
{
    char ch;

    printf("Enter a character\n");
    scanf("%c", &ch);

    (ch >= 97 && ch <= 122) ?
    printf("Character entered is a lowercase English alphabet\n") :
    printf("Character entered is not a lowercase English alphabet\n");

    return 0;
}

Output 1:
Enter a character
$
Character entered is not a lowercase English alphabet

Output 2:
Enter a character
A
Character entered is not a lowercase English alphabet

Output 3:
Enter a character
5
Character entered is not a lowercase English alphabet

Output 4:
Enter a character
a
Character entered is a lowercase English alphabet

Output 5:
Enter a character
z
Character entered is a lowercase English alphabet

ASCII Values of Lowercase English Alphabets

ASCII value of a is 97

ASCII value of b is 98

ASCII value of c is 99

ASCII value of d is 100

ASCII value of e is 101

ASCII value of f is 102

ASCII value of g is 103

ASCII value of h is 104

ASCII value of i is 105

ASCII value of j is 106

ASCII value of k is 107

ASCII value of l is 108

ASCII value of m is 109

ASCII value of n is 110

ASCII value of o is 111

ASCII value of p is 112

ASCII value of q is 113

ASCII value of r is 114

ASCII value of s is 115

ASCII value of t is 116

ASCII value of u is 117

ASCII value of v is 118

ASCII value of w is 119

ASCII value of x is 120

ASCII value of y is 121

ASCII value of z is 122

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

C Program To Check For Alphabet, Number and Special Symbol

Any character is entered through the keyboard, write a C program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.

The following table shows the range of ASCII values for various characters:
Character A – Z : ASCII Value 65 – 90
Character a – z : ASCII Value 97 – 122
Character 0 – 9 : ASCII Value 48 – 57
Special Symbol : ASCII Value 0 – 47, 58 – 64, 91 – 96, 123 – 127

ascii codes

Related Read:
else if statement in C
Relational Operators In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a Character
$

Output:
$ is a Special Character

Video Tutorial: C Program To Check For Alphabet, Number or Special Symbol


[youtube https://www.youtube.com/watch?v=NBcG-r0P9P8]

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

Source Code: C Program To Check For Alphabet, Number and Special Symbol

#include<stdio.h>

int main()
{
    char ch;

    printf("Enter a Character\n");
    scanf("%c", &ch);

    if(ch >= 65 && ch <= 90)
    {
        printf("%c is an Uppercase Alphabet\n", ch);
    }
    else if(ch >= 97 && ch <= 122)
    {
        printf("%c is an lowercase Alphabet\n", ch);
    }
    else if(ch >= 48 && ch <= 57)
    {
        printf("%c is a Number\n", ch);
    }
    else if( (ch >= 0  && ch <= 47) ||
             (ch >= 58 && ch <= 64) ||
             (ch >= 91 && ch <= 96) ||
             (ch >= 123 && ch <= 127))
    {
        printf("%c is a Special Character\n", ch);
    }

    return 0;
}

Output 1:
Enter a Character
A
A is an Uppercase Alphabet

Output 2:
Enter a Character
i
i is an lowercase Alphabet

Output 3:
Enter a Character
8
8 is a Number

Output 4:
Enter a Character
$
$ is a Special Character

Logic To Check For Alphabet, Number and Special Symbol

We use &&(AND) operator check check for range. i.e., for number, we check from the range 48 to 57. To check if the user entered character lies in this range we use (ch >= 48 && ch <= 57). To check for multiple ranges we use ||(OR) operator. For example, for special symbol:


(ch >= 0 && ch <= 47) ||
(ch >= 58 && ch <= 64) ||
(ch >= 91 && ch <= 96) ||
(ch >= 123 && ch <= 127)

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

C Program To Check Whether a Character is Vowel or Consonant

Lets write a C program to check whether user entered character is a vowel or a consonant.

Related Read:
if else statement in C
Logical Operators In C

Note: Lowercase English alphabets a, e, i, o, u and uppercase English alphabets A, E, I, O, U are called Vowels. All other alphabets are called Consonants.

We assume that the user enters only alphabets as input for this program.

Source Code: C Program To Check Whether a Character is Vowel or Consonant

#include < stdio.h >

int main()
{
    char ch;

    printf("Enter a character\n");
    scanf("%c", &ch);

    if( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
    {
        printf("%c is vowel\n", ch);
    }
    else
    {
        printf("%c is consonant\n", ch);
    }

    return 0;
}

Output 1:
Enter a character
A
A is vowel

Output 2:
Enter a character
B
B is consonant

Output 3:
Enter a character
e
e is vowel

Output 4:
Enter a character
f
f is consonant

Logic To Check Whether a Character is Vowel or Consonant

We ask the user to enter an alphabet and store it inside character variable ch. Using if else construct we check if the user entered alphabet is vowel or consonant. Inside if condition, we check if alphabet present in variable ch is equal to a or e or i or o or u or A or E or I or O or U. If any of these conditions are true, then it’ll return true and block inside if executes orelse else block gets executed.

In the background, c program checks the ASCII value present in variable ch with the ASCII values of a, e, i, o, u, A, E, I, O, U.

Video Tutorial: C Program To Check Whether a Character is Vowel or Consonant


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

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

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

C Program To Print Lowercase Alphabet(a-z) using While loop

In this video tutorial we show you how to write C program to print all the lower case alphabets(a-z) using simple while loop.

Related Read:
while loop in C programming
C Program To Print Uppercase Alphabet(A-Z) using While loop

Note: In C programming language, every character variable holds an ASCII value rather than the character itself. You can check ASCII value of all the characters here: C Program To Print All ASCII Characters and Code

Note:
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.

Source Code: C Program To Print Lowercase Alphabet(a-z) using While loop

#include < stdio.h >

int main()
{
    char ch = 'a';

    printf("Lowercase English Alphabets:\n");
    while(ch <= 'z')
    {
        printf("%c ", ch);
        ch++;
    }
    printf("\n");

    return 0;
}

Output:
Lowercase English Alphabets:
a b c d e f g h i j k l m n o p q r s t u v w x y z

Logic To Print Lowercase Alphabet(a-z) using While loop

Each alphabet has it’s own(unique) ASCII value. We initialize the character variable ch to a. Alphabet a has a ASCII value of 97. 97 gets stored in ch. We iterate through the while loop until value of ch is less than or equal to z(i.e., upto ASCII value of z, which is 122). We print character value(%c) of the variable ch, which has the character and not the ASCII value. We keep incrementing the value of ch by 1 for each iteration of while loop.

Video Tutorial: C Program To Print Lowercase Alphabet(a-z) using While loop


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

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

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