This program is designed to read an operator input from the user and determine what type of operator it is. It uses a switch
statement to analyze the input and identify single-character and double-character operators, such as:
- Greater than (
>
) and Greater than or equal to (>=
) - Less than (
<
) and Less than or equal to (<=
) - Assignment (
=
) and Equality (==
) - Logical AND (
&&
) and Bitwise AND (&
) - Logical OR (
||
) and Bitwise OR (|
) - Arithmetic operators like Addition (
+
), Subtraction (-
), Multiplication (*
), Division (/
), and Modulus (%
) - Also includes the NOT operator (
!
) and Bitwise NOT.
#include <stdio.h>
#include <conio.h>
void main()
{
char s[5]; // Array to store the operator input
clrscr(); // Clear the screen (Note: clrscr() is not standard in modern C)
printf("\n Enter any operator:");
gets(s); // Read the operator input from the user (Note: gets() is unsafe, use fgets() instead)
switch(s[0]) // Switch on the first character of the input
{
case '>':
if(s[1] == '=') // Check if the second character is '='
printf("\n Greater than or equal");
else
printf("\n Greater than");
break;
case '<':
if(s[1] == '=')
printf("\n Less than or equal");
else
printf("\n Less than");
break;
case '=':
if(s[1] == '=')
printf("\n Equal to");
else
printf("\n Assignment");
break;
case '!':
if(s[1] == '=')
printf("\n Not Equal");
else
printf("\n Bit Not");
break;
case '&':
if(s[1] == '&')
printf("\n Logical AND");
else
printf("\n Bitwise AND");
break;
case '|':
if(s[1] == '|')
printf("\n Logical OR");
else
printf("\n Bitwise OR");
break;
case '+':
printf("\n Addition");
break;
case '-':
printf("\n Subtraction");
break;
case '*':
printf("\n Multiplication");
break;
case '/':
printf("\n Division");
break;
case '%':
printf("\n Modulus");
break;
default:
printf("\n Not an operator");
}
getch(); // Wait for a key press before closing the console
}
Header Files:
#include <stdio.h>
: This library is included for standard input and output functions likeprintf
andgets
.#include <conio.h>
: This library is used for console input and output, such asclrscr()
andgetch()
. Note thatconio.h
is not part of the C standard library and may not be supported in some compilers.
Main Function:
- The program defines a
main()
function where the execution begins. - It declares a character array
s
of size 5 to store the operator input.
- The program defines a
Clear Screen:
clrscr()
is called to clear the console screen (this function may not work in all compilers).
User Input:
- The program prompts the user to enter an operator using
printf
. gets(s)
is used to read the user input into the arrays
. However,gets()
is unsafe as it can lead to buffer overflow. It is better to usefgets()
in modern C programming.
- The program prompts the user to enter an operator using
Switch Statement:
- The program uses a
switch
statement to evaluate the first character of the input (s[0]
). - Each
case
represents a different operator. If the operator requires a second character (like>=
,<=
, etc.), it checks the second character (s[1]
).
- The program uses a
Operator Identification:
- For each case:
- Single Character Operators: Operators like
+
,-
,*
,/
, and%
are directly matched. - Double Character Operators: Operators like
>=
,<=
,==
,!=
,&&
, and||
are checked by verifyings[1]
.
- Single Character Operators: Operators like
- For each case:
Default Case:
- If the input doesn’t match any known operators, the program prints “Not an operator.”
Ending the Program:
getch()
is called to pause the program until a key is pressed before it terminates.