OPERATORS
GAYATHRI P
WHAT IS AN OPERATOR?
• An operator is a symbol that tells the
computer to perform certain mathematical
or logical manipulations.
• These operators are used in programs to
manipulate data and variables.
www.programming9.com
2
TYPES OF OPERATORS
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
www.programming9.com
3
ARITHMETIC OPERATORS:
• Arithmetic operators are used to perform numerical calculations among
the values.
www.programming9.com
4
OPERATOR MEANING
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
#include<stdio.h>
int main()
{
int a, b, add, sub, mul, div, rem;
printf("Enter a, b values : ");
scanf("%d%d",&a,&b); // Reading two values
add=a+b; // Addition Operator
sub=a-b; // Subtraction Operator
mul=a*b; // Multiplication Operator
div=a/b; // Division Operator
rem=a%b; // Remainder (Modulo) Operator
printf("Result of addition is=%dn", add);
printf("Result of subtraction=%dn", sub);
printf("Result of multiplication is=%dn", mul);
printf("Result of division is=%dn", div);
printf("Result of remainder=%dn",rem);
return 0; }
www.programming9.com
5
RELATIONAL OPERATOR:
• Relational Operators are used to compare two quantities and take certain
decision depending on their relation.
If the specified relation is true it returns one.
If the specified relation is false it returns zero.
www.programming9.com
6
OPERATOR MEANING
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
#include<stdio.h>
void main()
{
int a, b;
printf(“Enter values for a and b : ");
scanf("%d %d", &a, &b);
printf("n The < value of a is %d", a<b);
printf("n The <= value of a is %d", a<=b);
printf("n The > value of a is %d", a>b);
printf("n The >= value of a is %d", a>=b);
printf("n The == value of a is %d", a==b);
printf("n The != value of a is %d", a!=b);
}
www.programming9.com
7
Example Program
LOGICAL OPERATORS:
• These operators are used for testing more than one condition and making
decisions.
'c' has three logical operators they are:
www.programming9.com
8
OPERATOR MEANING
&& Logical AND
|| Logical OR
! Logical NOT
LOGICAL OPERATORS
EXAMPLE PROGRAM
#include<stdio.h>
void main()
{
int a, b;
printf(“Enter values for a and b : ");
scanf(“%d %d", &a, &b);
printf("n %d",(a<b)&&(a!=b));
printf("n %d",(a<b)||(b<a));
printf("n %d",!(a==b));
}
www.programming9.com
9
ASSIGNMENT OPERATORS
• These operators are used for assigning the result of an expression to a
variable.
• b=a;
OPERATORS:
==, +=, -=, *=, /=, %=,
>>=, <<=, &=, |=, ^=
www.programming9.com
10
ASSIGNMENT OPERATORS
EXAMPLE
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter the values for a and b : ");
scanf("%d %d",&a,&b);
printf("n the values of= is:%d",c=a+b);
printf("n the values of +=is:%d",c+=b);
printf("n the value of-= is:%d",c-=a);
printf("n the value of *=is:%d",c*=a);
printf("n the value of /=is:%d",c/=b);
printf("n the value of %=is:%d",c%=b);
}
www.programming9.com
11
INCREMENT & DECREMENT
OPERATORS
Two most useful operators which are present in 'c' are increment and
decrement operators.
Operators: ++ and --
The operator ++ adds one to the operand
The operator -- subtracts one from the operand.
Both are unary operators and can be used as pre or post
increment/decrement.
www.programming9.com
12
INCREMENT & DECREMENT OPERATORS
EXAMPLE
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the values for a and b :");
scanf("%d %d", &a, &b);
printf("n The value of c is %d", c=++a);
printf("n The value of c is %d", c=a++);
printf("n The value of c is %d", c=--b);
printf("n The value of c is %d", c=b--);
}
www.programming9.com
13
CONDITIONAL OPERATORS
These conditional operator are used to construct conditional expressions of the form.
Syntax:
exp1?exp2:exp3.
where exp1,exp2,exp3 are expressions.
Operator: ?: (ternary operator)
www.programming9.com
14
CONDITIONAL OPERATORS
EXAMPLE
#include<stdio.h>
void main()
{
int a, b, x;
printf("Enter the values of a add b : ");
scanf("%d %d", &a, &b);
x=(a>b)?a:b;
printf("BiggestValue is :%d",x);
}
www.programming9.com
15
BITWISE OPERATORS
• These operators works on bit level
• Applied to Integers only
www.programming9.com
16
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
<< Shift Left
>> Shift Right
SPECIAL OPERATORS
'C' supports some special operators such as comma operator, sizeof operator and
pointer operators.
Comma operator:
the comma operator is used to combine related expressions.
A comma linked list of expressions are evaluated left to right and the value of right
most expression is the value of combined expression..
Example: value=(x=10, y=5, x+y);
www.programming9.com
17
SPECIAL OPERATORS
CONTD…
Sizeof Operator:
Sizeof is an operator used to return the number of bytes the
operand occupies.
Syntax:
m=sizeof(sum);
k=sizeof(2351);
www.programming9.com
18
WWW.PROGRAMMING9.COM
Like us @
www.facebook.com/programming9
www.programming9.com
19

Operators in Computer programming presentation

  • 1.
  • 2.
    WHAT IS ANOPERATOR? • An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. • These operators are used in programs to manipulate data and variables. www.programming9.com 2
  • 3.
    TYPES OF OPERATORS 1.Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment and decrement operators 6. Conditional operators 7. Bitwise operators 8. Special operators www.programming9.com 3
  • 4.
    ARITHMETIC OPERATORS: • Arithmeticoperators are used to perform numerical calculations among the values. www.programming9.com 4 OPERATOR MEANING + Addition - Subtraction * Multiplication / Division % Modulo Division
  • 5.
    #include<stdio.h> int main() { int a,b, add, sub, mul, div, rem; printf("Enter a, b values : "); scanf("%d%d",&a,&b); // Reading two values add=a+b; // Addition Operator sub=a-b; // Subtraction Operator mul=a*b; // Multiplication Operator div=a/b; // Division Operator rem=a%b; // Remainder (Modulo) Operator printf("Result of addition is=%dn", add); printf("Result of subtraction=%dn", sub); printf("Result of multiplication is=%dn", mul); printf("Result of division is=%dn", div); printf("Result of remainder=%dn",rem); return 0; } www.programming9.com 5
  • 6.
    RELATIONAL OPERATOR: • RelationalOperators are used to compare two quantities and take certain decision depending on their relation. If the specified relation is true it returns one. If the specified relation is false it returns zero. www.programming9.com 6 OPERATOR MEANING < Is less than <= Is less than or equal to > Is greater than >= Is greater than or equal to == Is equal to != Is not equal to
  • 7.
    #include<stdio.h> void main() { int a,b; printf(“Enter values for a and b : "); scanf("%d %d", &a, &b); printf("n The < value of a is %d", a<b); printf("n The <= value of a is %d", a<=b); printf("n The > value of a is %d", a>b); printf("n The >= value of a is %d", a>=b); printf("n The == value of a is %d", a==b); printf("n The != value of a is %d", a!=b); } www.programming9.com 7 Example Program
  • 8.
    LOGICAL OPERATORS: • Theseoperators are used for testing more than one condition and making decisions. 'c' has three logical operators they are: www.programming9.com 8 OPERATOR MEANING && Logical AND || Logical OR ! Logical NOT
  • 9.
    LOGICAL OPERATORS EXAMPLE PROGRAM #include<stdio.h> voidmain() { int a, b; printf(“Enter values for a and b : "); scanf(“%d %d", &a, &b); printf("n %d",(a<b)&&(a!=b)); printf("n %d",(a<b)||(b<a)); printf("n %d",!(a==b)); } www.programming9.com 9
  • 10.
    ASSIGNMENT OPERATORS • Theseoperators are used for assigning the result of an expression to a variable. • b=a; OPERATORS: ==, +=, -=, *=, /=, %=, >>=, <<=, &=, |=, ^= www.programming9.com 10
  • 11.
    ASSIGNMENT OPERATORS EXAMPLE #include<stdio.h> void main() { inta, b, c; printf("Enter the values for a and b : "); scanf("%d %d",&a,&b); printf("n the values of= is:%d",c=a+b); printf("n the values of +=is:%d",c+=b); printf("n the value of-= is:%d",c-=a); printf("n the value of *=is:%d",c*=a); printf("n the value of /=is:%d",c/=b); printf("n the value of %=is:%d",c%=b); } www.programming9.com 11
  • 12.
    INCREMENT & DECREMENT OPERATORS Twomost useful operators which are present in 'c' are increment and decrement operators. Operators: ++ and -- The operator ++ adds one to the operand The operator -- subtracts one from the operand. Both are unary operators and can be used as pre or post increment/decrement. www.programming9.com 12
  • 13.
    INCREMENT & DECREMENTOPERATORS EXAMPLE #include<stdio.h> void main() { int a,b,c; printf("Enter the values for a and b :"); scanf("%d %d", &a, &b); printf("n The value of c is %d", c=++a); printf("n The value of c is %d", c=a++); printf("n The value of c is %d", c=--b); printf("n The value of c is %d", c=b--); } www.programming9.com 13
  • 14.
    CONDITIONAL OPERATORS These conditionaloperator are used to construct conditional expressions of the form. Syntax: exp1?exp2:exp3. where exp1,exp2,exp3 are expressions. Operator: ?: (ternary operator) www.programming9.com 14
  • 15.
    CONDITIONAL OPERATORS EXAMPLE #include<stdio.h> void main() { inta, b, x; printf("Enter the values of a add b : "); scanf("%d %d", &a, &b); x=(a>b)?a:b; printf("BiggestValue is :%d",x); } www.programming9.com 15
  • 16.
    BITWISE OPERATORS • Theseoperators works on bit level • Applied to Integers only www.programming9.com 16 OPERATOR MEANING & Bitwise AND | Bitwise OR ^ Bitwise Exclusive OR << Shift Left >> Shift Right
  • 17.
    SPECIAL OPERATORS 'C' supportssome special operators such as comma operator, sizeof operator and pointer operators. Comma operator: the comma operator is used to combine related expressions. A comma linked list of expressions are evaluated left to right and the value of right most expression is the value of combined expression.. Example: value=(x=10, y=5, x+y); www.programming9.com 17
  • 18.
    SPECIAL OPERATORS CONTD… Sizeof Operator: Sizeofis an operator used to return the number of bytes the operand occupies. Syntax: m=sizeof(sum); k=sizeof(2351); www.programming9.com 18
  • 19.