C Programming Tutorials: N.V.Raja Sekhar Reddy
C Programming Tutorials: N.V.Raja Sekhar Reddy
com
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
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
ARITHMETIC OPERATORS:
Arithmetic operators are used to perform numerical calculations among the values.
OPERATOR + * / % MEANING Addition Subtraction Multiplication Division Modulo Division
www.programming9.com
#include<stdio.h>
int main()
{ int a, b, add, sub, mul, div, rem; printf("Enter a, b values : ");
scanf("%d%d",&a,&b);
add=a+b; sub=a-b; mul=a*b;
div=a/b;
rem=a%b;
// Division Operator
// Remainder (Modulo) Operator
printf("Result of addition is=%d\n", add); printf("Result of subtraction=%d\n", sub); printf("Result of multiplication is=%d\n", mul); printf("Result of division is=%d\n", div); printf("Result of remainder=%d\n",rem); return 0;
5
}
www.programming9.com
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.
OPERATOR < <=
> >= == !=
6
Example Program
#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); }
7 www.programming9.com
LOGICAL OPERATORS:
These operators are used for testing more than one condition and making decisions. 'c' has three logical operators they are:
OPERATOR && || MEANING Logical AND Logical OR
Logical NOT
www.programming9.com
ASSIGNMENT OPERATORS
These operators are used for assigning the result of an expression to a variable. b=a;
OPERATORS:
==, +=, -=, *=, /=, %=, >>=, <<=, &=, |=, ^=
10
www.programming9.com
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.
12 www.programming9.com
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--); }
13 www.programming9.com
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:
14
?: (ternary operator)
www.programming9.com
15
www.programming9.com
BITWISE OPERATORS
|
^ << >>
16
Bitwise OR
Bitwise Exclusive OR Shift Left Shift Right
www.programming9.com
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);
17 www.programming9.com
18
www.programming9.com
www.programming9.com
Like us @ www.facebook.com/programming9
19
www.programming9.com