There are four main classes of operators:
arithmetic,
relational,
logical,
bitwise
In addition, there are some special operators,
such as the assignment operator, for particular tasks.
Assignment Operator (In Short):
Syntax: variable_name = expression;
Arithmetic Operators:
Operator
|
Action
|
+
|
Addition, also unary plus
|
-
|
Subtraction, also unary minus
|
*
|
Multiplication
|
/
|
Division
|
%
|
Modulus
|
++, --
|
Increment & Decrement
|
//example
using arithmatic operators
#include <stdio.h>
int main()
{
int a = 20, b = 10;
int add = a + b;
int sub = a - b;
int mul = a * b;
int div = a / b;
int rem = a % b;
int u_plus = +a;
int u_minus = -a;
printf("
+ result: %d\n", add);
printf("
- result: %d\n", sub);
printf("
* result: %d\n", mul);
printf("
/ result: %d\n", div);
printf("
%% result: %d\n", rem);
printf("
unary + result: %d\n", u_plus);
printf("
unary - result: %d\n", u_minus);
getchar();
return 0;
}
|
Increment and Decrement Operators:
There are two useful operators that simplify two
common operations. These are the increment and decrement operators, ++ and --.
x = x+1; is the same as ++x;
x = x–1; is the same as --x;
Both the increment and decrement operators may
either precede (prefix) or follow (postfix) the operand.
For example,
x = x+1; can be written ++x; or x++;
Difference between prefix & postfix
increment or decrement operators:
When an increment or decrement operator precedes
its operand, the increment or decrement operation is performed before obtaining
the value of the operand for use in the expression.
If the operator follows its operand, the value
of the operand is obtained before incrementing or decrementing it.
For example:
int x, y;
//then,
x = 10; y = ++x;
//sets x to 11 and y to 11. But,
x = 10; y = x++;
//sets x to 11 and y to 10.
|
Precedence of the arithmetic operators:
Highest
|
++ --
|
|
unary plus unary minus
|
|
* /
%
|
Lowest
|
+ -
|
We can use parentheses() to alter the order of
evaluation. Parentheses force an operation, or set of operations, to have a
higher level of precedence.
Relational and Logical Operators:
Relational Operators:
Operator
|
Action
|
>
|
Greater than
|
>=
|
Greater than or equal
|
<
|
Less than
|
<=
|
Less than or equal
|
==
|
Equal
|
!=
|
Not equal
|
Logical Operators:
Operator
|
Action
|
&&
|
AND
|
||
|
OR
|
!
|
NOT
|
All relational and logical expressions produce a
result of either true or false (1 or 0).
Examples:
//
example using relational operators
#include <stdio.h>
void print(int val)
{
printf("
%d\n", val);
}
int main()
{
print(2 > 1);
print(2 < 1);
print(2 >= 1);
print(2 <= 2);
print(2 == 0);
print(2 != 1);
getchar();
return 0;
}
|
//example
using logical operators
#include <stdio.h>
void printLogics(int p, int q)
{
printf("%d\t%d\t%d\t%d\t%d\n\n", p, q, p && q, p || q, !p);
}
int main()
{
printf("p\tq\tp&&q\tp||q\t!p\n\n");
printLogics(0, 0);
printLogics(0, 1);
printLogics(1, 1);
printLogics(1, 0);
getchar();
return 0;
}
|
The relative precedence of the relational and logical
operators:
Highest
|
!
|
|
>
>= < <=
|
|
== !=
|
|
&&
|
Lowest
|
||
|
Bitwise Operators:
Operator
|
Action
|
&
|
AND
|
|
|
OR
|
^
|
Exclusive OR (XOR)
|
~
|
One`s complement (NOT)
|
>>
|
Shift right
|
<<
|
Shift left
|
Bitwise operators are used for testing, setting,
or shifting the actual bits in a byte or word, which correspond to the standard
char and int data types and variants.
[Note: Bitwise operator cannot be use on float,
double, long double, void, or other more complex types.]
Examples:
//example
using bitwise AND,OR,XOR and One`s complement
#include <stdio.h>
void printBitwiseOperations(int p, int q)
{
printf("%d\t%d\t%d\t%d\t%d\t%d\n\n", p, q, p&q, p | q, p^q, ~p);
}
int main()
{
printf("p\tq\tp&q\tp|q\tp^q\t~p\n\n");
printBitwiseOperations(0, 0);
printBitwiseOperations(0, 1);
printBitwiseOperations(1, 1);
printBitwiseOperations(1, 0);
getchar();
return 0;
}
|
//
example using bitwise shifting
/* A
bit shift example. */
#include <stdio.h>
int main(void)
{
unsigned int a, b, c, d, e;
a = 1;
/* left shifts */
/* left shift a by 1,
which is same as a multiply by 2 */
b = a << 1;
printf("Left
shift %d: %d\n", a, b);
c = b << 1;
printf("Left
shift %d: %d\n", b, c);
/* right shifts */
/* right shift i by 1,
which is same as a division by 2 */
d = b >> 1;
printf("Right
shift %d: %d\n", b, d);
e = c >> 1;
printf("Right
shift %d: %d\n", c, e);
getchar();
return 0;
}
|
Assignment Operator (In Details):
Syntax:
variable_name = expression;
|
A single equal sign is used to indicate
assignment.
Left part of the syntax is called lvalue and
right part is called rvalue.
Left part must be an object such as a variable
and right part must be a value (any constant or value of an expression).
Type Conversion in Assignments:
When variables of one type are mixed with
variables of another type, a type conversion will occur automatically. The
value of the right side of the assignment is converted to the type of the left
side.
Multiple Assignments:
Many variables can be assigned with the same
value by using multiple assignments in a single statement.
For example,
int x, y, z;
x = y = z = 7;
|
Compound Assignments:
Syntax:
var = var operator expression
can be rewritten as
var operator = expression
|
+=, -=, *=, /=, %= etc are Compound Assignments.
For example, x = x+5 can be written as x += 5;
All binary operators (that require two operands)
can be used as Compound assignment operator.
? Operator (ternary operator):
Syntax:
means Exp1 is calculated and if Exp1 is 1
or(true) then Exp2 is calculated otherwise Exp3 is calculated.
For example a variable can be assigned according
to a condition like
variable = condition ? variable1 : variable2;
if condition is 1 then variable= variable1
otherwise variable= variable2
& and * (address and pointer Operators):
Address operator (&): As a unary operator,
returns the memory address of its operand.
For example,
m = &var;
Here m equals the memory address of var.
Pointer operator (*): As a unary operator,
returns the value of the object located at the address that follows it.
For example:
v = *m;
the value of var is assigned to v.
//
simple example using address and pointer Operators.
#include <stdio.h>
int main(void)
{
int target,
source;
int *m;
source = 2500;
m = &source;
target = *m;
printf("%d", target);
return 0;
}
|
Compile-Time Operator sizeof:
sizeof is a unary compile-time operator that returns the size( in
bytes) of the variable or parenthesized type that it precedes.
For example,
//
example sizeof operator
#include <stdio.h>
int main()
{
double f = 3.1415;
printf("%d
", sizeof f);
printf("%d", sizeof(int));
return 0;
}
|
Comma Operator (,):
The left side of the comma operator is always
evaluated as void. The expression on the right side becomes the value of the
total comma-separated expression.
For example,
x = (y=3, y+1);
Here, 3 is assigned to y and then value of x is
calculated as y+1.
Parentheses are necessary with the comma
operator because comma operator has a lower precedence than the assignment
operator.
The Dot (.) and Arrow (–>) Operators:
These operators are used to access
elements/members of structures, unions. The dot operator is used when working
with a structure or union directly while arrow operator is used with a pointer
to a structure or union.
For example:
//
example using dot (.) and arrow (->) operator
struct Circle
{
float radius;
float centerX;
float centerY;
};
struct Circle c = { 2.0F, 4.5F,
6.7F };
struct Circle *p = &c; /* address of c into p */
#include <stdio.h>
int main()
{
printf("Radius
using dot operator= %f", c.radius);
printf("\nRadius
using arrow operator = %f", p->radius);
getchar();
return 0;
}
|
The [ ] and ( ) Operators:
Parentheses () are operators that increase the
precedence of the operations inside them. For example,
int a,b,c,d,e;
a =2;
b=3;
c=4;
d=5;
e = ((a+b)*(c+d))*c;
Here expression (a+b) and (c+d) are calculated
first then ((a+b)*(c+d)) and then ((a+b)*(c+d))*c;
Square brackets [ ] perform array indexing.
Given an array, the expression within square brackets provides an index into
that array.
For example:
//
example using [] operator
#include <stdio.h>
int main(void)
{
char pi[10] = {`3`,`.`,`1`,`4`,`1`,`5`,`2`,`9`,`6`,`\0`};
printf("%s", pi);
printf("\n");
printf("%c", pi[1]);
printf("\n");
printf("%c", pi[9]);
getchar();
return 0;
}
|
operators list with Precedence :
Highest
|
( ) [ ]
–> .
|
|
! ~
++ --
- (type)
* &
sizeof
|
|
* /
%
|
|
+ -
|
|
<< >>
|
|
< <=
> >=
|
|
== !=
|
|
&
|
|
^
|
|
|
|
|
&&
|
|
||
|
|
?:
|
|
= +=
–= *=
/= etc.
|
Lowest
|
,
|