You are on page 1of 3

Experiment 3

Aim : Write a program to count the number of operators in a file.


Theory :
Operators are the foundation of any programming language. Thus the functionality of C/C++
programming language is incomplete without the use of operators. We can define operators as symbols
that help us to perform specific mathematical and logical computations on operands. In other words, we
can say that an operator operates the operands.

C/C++ has many built-in operator types and they are classified as follows:
1. Arithmetic Operators: These are the operators used to perform arithmetic/mathematical
operations on operands. Examples: (+, -, *, /, %,++,–). Arithmetic operator are of two types:
I. Unary Operators: Operators that operates or works with a single operand are unary
operators. For example: (++ , –)
II. Binary Operators: Operators that operates or works with two operands are binary operators.
For example: (+ , – , * , /)
To learn Arithmetic Operators in details visit this link.
2. Relational Operators: These are used for comparison of the values of two operands.

3. Logical Operators: Logical Operators are used to combine two or more conditions/constraints or
to complement the evaluation of the original condition in consideration.

4. Bitwise Operators: The Bitwise operators is used to perform bit-level operations on the operands.
The operators are first converted to bit-level and then the calculation is performed on the operands.

5. Assignment Operators: Assignment operators are used to assign value to a variable.

Code :

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void main()
{
//int line_count = 0, comm = 0, multcomm = 0;
FILE *f;
char ch;
f = fopen("operators.txt", "r");

while ((ch = fgetc(f)) != EOF)


{
if(ch=='<')
{
char ch1 = fgetc(f);
if(ch1 == '=')
printf("<= Less Than or Equal To operator is present\n");
else if(ch1 == '<')
printf("<< Left Shift Bitwise Operator is present\n");
else
printf("< Strictly Less Than Operator is present\n");
}
if(ch=='>')
{
char ch1 = fgetc(f);
if(ch1 == '=')
printf(">= Greater Than or Equal To operator is present\n");
else if(ch1 == '>')
printf(">> Right Shift Bitwise Operator is present\n");
else
printf("> Strictly Greater Than Operator is present\n");
}
if(ch=='=')
{
char ch1 = fgetc(f);
if(ch1 == '=')
printf("== Equal to Comparison operator is present\n");
else
printf("= Assignment Operator is present\n");
}
if(ch=='+')
{
char ch1 = fgetc(f);
if(ch1 == '+')
printf("++ Increment operator is present\n");
else
printf("+ Addition Operator is present\n");
}
}

Input :
Output :

Learning :

In this experiment, we learn the rules to identify operators and file handling.

You might also like