3 Operators Expressions and Type Conversions

0

Introduction

C has large number of operators all the computation in C language can be done through operator various categories of operators for example automatic operator relational operator logical operator assignment operator unary operator



Expression 

an expression is combination of one or more operands and 0 or more operators to compute value

for eg: z=x+y;  x, and y are operands and (+) plus sign is operators.


Operators 

operators are special character which instructor the compiler or interpreter to perform certain operation on some operands.  operator specifies the operation to be performed . An operand may be variable, a function reference or a constant. some operators requires two or more operands while other act upon only one operands . the operators which act upon a single operands are called unary operators , the operators embedded between the two operands are called binary operators and the operators that act on three operands are called ternary operators . 

In C , the operators can be prefix, postfix and infix. Basically prefix and postfix operators work on single operands .Some operators are indicated before operand, they are called prefix operators . Operators which are indicated after the operands is called postfix operators.  When operators are used in between two or more operands , these are known an infix operators . 

Example:  ++i;            Prefix unary operator

                  i++;           Postfix unary operators

                  a+b;          Binary operators / infix operators


Different type of C operators shown below:

Type of operators                          Symbolic Representation 

1.Arithmetic operators                  +, -, *, /, %

2.Relational operators                   <, >, <=, >=, ==, !=

3.Logical operators                       &&, ||, !

4.Assignment operators                 =

5.Conditional operators                ?=

6.Unary operators                         ++,--,-,sizeof etc.

7.Bitwise operators                        &,^,|,<<,>>,~

8.other operators                            ,


1. Arithmetic operators 

Arithmetic operators are used for mathematical calculations. The arithmetic operator can operate on any built-in data type i.e. integer , float, char.

there are five arithmetic operators.

Arithmetic operator        purpose             example

            *                         Multiplication       4*5=20

            /                          Division                15/3=5

            %                        Modulus                10%3=1

            +                         Addition                2+3=5

            -                          Subtraction          15-5=10

( the operands used with any of the operators can be of integer , float, or char type except the modulus operator. the operator of (%) modulus operator must be of integer)

Arithmetic operators can be used on single operand or two or more operands


Program:

To illustrate the use of arithmetic operators


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
  int a,b,c,d,e,f,g;
printf("Enter the value of a and b ");
scanf("%d%d",&a,&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
printf("sum=%d",c);
printf("\n Difference=%d",d);
printf("\n Multiplication =%d",e);
printf("\n Division=%d",f);
printf("\n Reminder=%d",g);
getch();
}  
Output:
Enter the value of a & b 10    5
Sum = 15
Difference = 5
Multiplication = 50
Division = 2
Reminder = 0


Relational operators 

The relational operators are used to make comparisons between two expressions. and check the two operands how relate each other. 
When two operands are compared . it produce result in true (non-zero) or false (zero).
if comparison succeeds , a value 1 is returned and if comparison fails then value 0 is returned . you can easily understand the concept of relational operator , if you understood the if - else statement.

syntax:

if(condition)
    statement 1;        // this is executed when condition is true
else
    statement 2;        // this is executed when condition is false


The relational operators are listed below:

Relational operator            Meannig
        <                                Less than
        >                                Greater than
        <=                              Less than or Equal to
        >=                             Greater than or Equal to
        ==                            Equal to
        !=                              Not Equal to
(Basically , relational operators are categorized into two sections: One is comparison section and second is equality section. )


Program: 
To illustrate the concept of relational operators.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x=3, y=5;
char c='A';
printf("Result 1 =%d",(x+y)>5);
printf("\n Result 2=%d",c<'B');
printf("\n Result 3=%d",x==5);
getch();
}
Output:
Result 1 = 1
Result 2 = 1
Result 3 = 0
Relational operators are basically used in decision making statements like if statement, while statement, do-while and for statement.


Tags

Post a Comment

0Comments
Post a Comment (0)