Freelancer

Whether the input is a character or not


Whether the input is a character or not- C Programming Example

This is a code done by c programming as an example to detect whether the given input is a character or not.
C programming example
Characters

C Programming Code

#include<stdio.h>
#include<conio.h>
main()
{
    char i;
    printf("Input a character \n");
    scanf("%c",&i);
    if (i>='a'&&i<='z'||i>='A'&&i<='Z')
        printf("The character is an alphabet \n");
    else
        printf("The character is not an alphabet");
    getch();
}

Output of the code

C programming example
 Character or not

Explanation of the code

·        #include<stdio.h>
#include<conio.h>
Ø  This are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>.In the other hand <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.

·         main()
Ø  In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
·            char a;
Ø  char is a variable or data type. A variable declared to be of type char can be used for storing character. Here we declare “i”variable in char type.
·         printf() function
Ø  C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen. 
·         scanf() function
Ø  This function is used to get input from the user of the program. scanf(“%c”,&i”) is used here. So, this program will read in a char value for “i” variable that the user enters on the keyboard (%c is for char values. As there is “&i”, the number user enters will be the value of “i” variable).

printf("Input a character \n");
 scanf("%c",&i);
Firstly an input will be given, then the program will scan the character and save it in I variable.
For this program IF-ELSE loop is being used,.
if (i>='a'&&i<='z'||i>='A'&&i<='Z')
        printf("The character is an alphabet \n");
Firstly the condition for this loop is i>=’a’&&i<=’z’ that means if the character is an alphabet this loop will run in the program as the condition is set for alphabets only. Here the condition means alphabets from a to z. And if the condition is satisfied the statement in the loop after printf will be shown as output. 
    else
        printf("The character is not an alphabet");
If the first condition does not satisfy the program will run the else loop and show that the input character is not an alphabet.
And finally getch(); is written to end the program.

You may also like

No comments

Powered by Blogger.