Operators in C language

Operators in c language

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operation one or more operands where an operand can be an variable, constant or an expression.An operation in an action (s) performed on one or more operands that evaluates mathematical expressions or change the data.
In C, an operators can be either be unary, binary or ternary. If an operator operates on single operands then it is termed as unary operators. if an operator operates on two operands, it is called binary operator. The operators that work with three operands is called ternary operator.

There are seven type of operators are :
  1. Arithmetic operators  (+,-,*,%)
  2. Relational operators  (<,>,<=,>=,==,!=)
  3. Logical operators    (&&,||,!)
  4. Assignment operators  (=,+=,-=,*=,/= etc)
  5. Unary operators (-,++,--,(type),sizeof etc.)
  6. Conditional operators   (?:)
  7. Bitwise operators     (&,|,^,~,<<,>>)

Arithmetic operators :

The arithmetic operators are used to perform many of the familiar mathematical operations (such as addition, multiplication, subtraction etc.) that involves the calculation of numeric values represented by variables, constant etc. They are binary operators that work with integers, floating-point numbers and even characters (i.e. they can be used with any basic type).

A program of arithmetic operators :
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,d,e,f,g;
clrscr();
printf("enter the value of a=");
scanf ("%d",&a);
printf("enter value of b=");
scanf ("%d",&b);
c=a+b;
d=a*b;
e=a-b;
f=a/b;
g=a%b;
printf("%d + %d", a,b,c);
printf("%d * %d", a,b,d);
printf("%d - %d", a,b,e);
printf("%d / %d", a,b,f);
printf("%d % %d", a,b,g);
getch();
}

OUTPUT:
enter the value of a =7
enter the value of b=3
7+3=10
7*3=21
7-3=4
7/3=2
7%3=1


Relational operators :

The relational operators are used to test the relationship between two operands. It does so by comparing one operand against another and return either true or false, depending upon the status of the match. A relational expression that is interpreted by true evaluates to an integer value 1 and if false it evaluates to an integer value of 0.
Examples of relational operators are :
>  less than
< greater than 
>=  less than equal to
<=  greater than equal to
==  is equal to
!=  is not equal to


program of relational operators :
#include<stdio.h>
#include<conio.h>
main()
{
int x=10, y=15;
clrscr();
printf("%d<%d= %d", x,y,x<y);
printf("%d<=%d= %d", x,y,x<=y);
printf("%d>%d= %d", x,y,x>y);
printf("%d>=%d= %d", x,y,x>=y);
printf("%d!=%d= %d", x,y,x!=y);
printf("%d==%d= %d", x,y,x==y);
getch();
}
OUTPUT:
10<15=1
10<=15=1
10>15=0
10>=15=0
10!=15=1
10==15=0

Logical operators

Logical operators are used to combine one or more relational expressions that results in formation of complex logical expression. Like relational operators, the logical operators evaluate the result of logical expression in terms of boolean values that can be true (1) or false (0) according to the result of the expression . They are generally used in an expression.
In C there are three logical operator, logical AND(&&),logical OR(||) and logical NOT (!). The logical AND and logical OR are binary operators wheras logical NOT is an unary operator.

Program of logical operator:
#include<stdio.h>
#include<conio.h>
main()
{
int num;
clrscr();
printf("enter a number =");
scanf("%d" &num);
printf("if answer =0(n0) and =1(yes)");
printf("\is %d",num);
printf("\n \tdivisible by 3 and 5? %d", (num%3==0 && num%5==0));
printf("\n \tdivisible by 3 and 5? %d", (num%3==0 || num%5==0));

getch();
}


OUTPUT :
enter a number =33
if answer =0(no) and =1(yes) is 33 
divisible by 3 and 5? 0
divisible by 3 or 5? 1


Assignment operator:

The assignment operator(=) is the most commonly used binary operator in C.It evaluates the operand on  the right hand side and then assign the resulting value to a variable on the lefthand side . The right operand can be a variable , constant , function call or expression . The general form of representing assignment operator is 
variable= expression/constant/function call
Example:
a=3; /*conatant*/
x=y+10;  /* expression /*


A program to show importance of +=,in assignment operator:
#include<stdio.h>
#include<conio.h>
main()
{
int a=17;
clrscr();
a+=10;
printf("%d".a);
getch();
}


OUTPUT:
27


Unary operators

The operators that act upon a single operands to produce a new value are known as unary operatoes. these include
-,++,--,!,~,*,&, sizeof, (type)


Conditional operators :

Conditional operators (?:) is the only ternary operator available in C which operates on three operands . It evaluates to one of the two expression depending on whether logical expression evaluates to true or false. It is typically used to replace if-else logic in some solutions. the general expression is
                  condition_expr?expr1:expr2
A program to find greatest of two numbers a and b using conditional operator

#include<stdio.h>
#include<conio.h>
main()
{
int a, b, max;
clrscr();
printf ("\nenter value of a:=  ");
scanf("%d",&a);
printf ("\nenter value of b:=  ");
scanf("%d",&b);
max=(a>b)?a:b;
printf("\nthe number %d is greater between %d and %d", max,a,b);
getch();
}


OUTPUT:
enter value of a: 10
enter value of b: 20
the number 20 is greater between 10 and 20

Bitwise operators:

C provides an extensive bit manipulation operators for programmers who want to write program at machine level. These operator act directly on the bits of there operands rather than numeral value. They are used for testing bits or shifting them either right to left  or left to right.
There are various type bitwise operators are :
  1. &      bitwise AND
  2. |        bitwise  OR
  3. ^       bitwise exclusive OR
  4. ~       bitwise one's compliment
  5. <<     shift left
  6. >>      shoft right

                                      THANKS 







No comments

Powered by Blogger.