OPERATORS IN C PROGRAMMING
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators
- Arithmetic Operators.
- Relational Operators.
- Bitwise Operators.
- Assignment Operators.
- Logical operators.
- Special Operators.
Three things to know about each operator
(1) Function =What does it do?
(2) Precedence = In which order are operators combined?
Example:
"a * b + c * d" is the same as "(a * b) + (c * d)" because multiply (*) has a higher precedence than addition (+)
(3) Associativity . In which order are operators of the same precedence combined? .
Example:
"a - b - c" is the same as "(a - b) - c"
because add/sub associate left-to-right
1.Assignment Operator : Changes the value of a variable
Example : x = x + 4;
in the above example it
1. Evaluate right-hand side.
2. Set value of left-hand side variable to result.
In Assignment operator :
- All expressions evaluate to a value .
- Even ones with the assignment operator e.g. y = x = 3
- For assignment, the result is the value assigned
Usually (but not always) the value of the right-hand side
Type conversion might make assigned value
different than computed value
e.g. int x = 15.6/3 = 5.
- Assignment associates right to left
eg : y = x = 3;
y gets the value 3, because (x = 3) evaluates to the value 3
2.Arithmetic Operators : Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs.
Arithmetic Operators/Operation | Example |
+ (Addition) | A+B |
– (Subtraction) | A-B |
* (multiplication) | A*B |
/ (Division) | A/B |
% (Modulus) | A%B |
If mixed types, smaller type is "promoted" to larger
- Example: x + 4.3.
- if x is int, converted to double and result is double .
Integer division -- fraction is dropped .
- Example: x / 3
- if x is int and x=5, result is 1 (not 1.666666...).
Storing mixed type expression values
- int x = 45/7.1;
- C compiler will do automatic down grade if storage is small. Java
compiler will complain
- Thus C has less type safety features compared to Java.
Modulo -- result is remainder
- Example: x % 3
- if x is int and x=5, result is 2.
3.Relational Operators : Relational operators in C are commonly used to check the relationship between the two variables. If the relation is true, then it will return value 1. Otherwise, it returns value 0.Types of relation operators are :
Useful for clearing bits.
AND with zero = 0.
AND with one = no change
Example:11000101
AND 00001111= 00000101.
OR
Useful for setting bits .
OR with zero = no change.
OR with one = 1
Example:11000101
OR 00001111= 11001111.
NOT
Unary operation -- one argument
Flips every bit.
Example : 11000101 NOT = 00111010.
Left Shift (<<)
variable << shift amount
Shifts bits to the left by shift amount
Fills spaces equivalent to shift amount on the right side
with zeroes
x = y << 1 same as x = y + y or 2 * y
(>>) Right shift
Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
5.Logical Operators :
Logical operators in c programming |
6.Special Operators : Changes value of variable by 1 before (or after) its value is used in an expression
Symbol Operation Usage
++ postincrement x++
-- postdecrement x--
++ preincrement ++x
-- predecrement --x
Example : 1.x = 4;
y = x++;
Result: x = 5, y = 4
(because x is incremented after assignment)
2.x = 4;
y = ++x;
Results: x = 5, y = 5
(because x is incremented before assignment)
0 Comments