C has a rich set of operators. They may operate on a
single operand or two operands. They are used to perform basic arithmetic
operations, comparisons, manipulation of bits and so on. C operators are
broadly classified into three main categories.
1.
Unary Operators
2.
Binary Operators
3.
Ternary Operators
1. UNARY OPERATORS :
An
operator that acts upon only one operand is known as a unary operator. The C
unary operators are
1)
Unary minus ( - )
2)
Logical NOT
operator ( ! )
3)
Bitwise
Complementation ( ~ )
2. BINARY OPERATORS :
These
operators act upon two operands. Hence the name binary. The binary operators
are further classified into 4 categories.
1)
Arithmetic Operators
2)
Relational Operators
3)
Logical Operators
4)
Bitwise Operators
3. TERNARY OPERATORS :
These
operators act upon three operands. Hence it is called ternary operator. It is
known as conditional operator.
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
1. ARITHMETIC OPERATORS :
Arithmetic operators are used to perform the basic
arithmetic operations such as addition, subtraction, multiplication and
division. The modulus operator is added to the list of C arithmetic operators.
It is used for finding the remainder after an integer division.
Operation |
Operator |
Precedence |
Associatively |
Addition |
+ |
2 |
L
to R |
Subtraction |
- |
2 |
L
to R |
Multiplication |
* |
1 |
L
to R |
Division |
/ |
1 |
L
to R |
Modulus |
% |
1 |
L to R |
Addition
Operator ( + ) : This operator adds
the two data values (operands) that appear on either side of it.
Example : sum = num1+num2;
Subtraction Operator ( - ) : The operand that appears to the right of the operator
is substracted from the operand that is the left of it.
Example : net = amount – discount;
Multiplication Operator ( * ) : The two operands that appear on either side of the
operator are multiplied.
Example : mul = a * b;
Division Operator ( / ) : The value to the left of / is divided by that on the
right.
Example : div = x / y;
Modulus Operator ( % ) : This operator evaluates the remainder of an integer
division operation.
Example : rem = x % y;
2. RELATIONAL
OPERATORS :
These are used to compare two operands. They define
the relationship existing between two constants or variables. They result in
either a TRUE or FALSE value. The value of TRUE is nonzero (i.e, 1) and that of
FALSE is zero.
Operator |
Meaning |
Precedence |
Associativity |
< |
Lesser
than |
1 |
L to R |
< = |
Less than or equal to |
1 |
L
to R |
> |
Greater than |
1 |
L
to R |
> = |
Greater than or equal to |
1 |
L
to R |
= = |
Equal to |
2 |
L
to R |
! = |
Not
equal to |
2 |
L to R |
Example
:
If
a =100, b=80 Then (a > b) is true
(a
< b) is false
Difference between = and = = operator
=
= operator |
=
operator |
||
1. |
= = is relational operator. |
1. = is assignment operator. |
|
2. |
=
= sign does not change the value of an identifier. |
2 |
=
operator changes the value of the variable to its left. |
3. LOGICAL OPERATORS :
These operators are used to take decisions. These are
three such operators. They are AND, OR and NOT. The result of these operators
is either TRUE or FALSE. The logical operators are used to connect one or more relational expressions.
Operator |
Meaning |
Precedence |
Associativity |
&&
(double ampersand) |
Logical AND |
2 |
L to R |
|| (double pipeline) |
Logical
OR |
3 |
L
to R |
! (exclamation) |
Logical
NOT |
1 |
L
to R |
The logical AND operator is used to perform operation
on two operands. It is equivalent to a multiplication operation. The result of
a logical AND is true, when both the operands are true; otherwise the answer is
false.
Operand1 |
Operand2 |
Operand1 && Operand2 |
False |
False |
False |
False |
True |
False |
True |
False |
False |
True |
True |
True |
The logical OR operator is used to perform operation
on two operands. It is equivalent to a addition operation. The result of a
logical AND is false, only when both the operands are false; otherwise the
answer is true.
Operand1 |
Operand2 |
Operand1
&& Operand2 |
False |
False |
False |
False |
True |
True |
True |
False |
True |
True |
True |
True |
The logical NOT is used to obtain the logical
complement of the operand. The result of a logical NOT is TRUE when the operand
is FALSE and vice-versa.
Operand |
!
Operand |
False |
True |
True |
False |
Example
: If i = 8, x = 5.1, c=’a’ Then
1. ( i > 6
) && ( c = = ‘a’ ) is
true or 1
2. ( i <= 6
) | | ( c = = ‘b’ ) is
false or 0
3. ( i >= 6
) && ( c = = ‘97’) is true
4. ( x < 10
) | | ( i > 10 ) is true
4] ASSIGNMENT OPERATORS :
The assignment operator = is used to assign the value
of a variable, constant or expression to a variable.
Syntax
:
variable = variable / constant /
expression ;
Example
:
a
= 10;
b
= 10;
sum
= a + b
C
also provider abbreviated or short hand assignment operators.
Syntax
:
Variable
Operator = Expression;
Example
:
n
= n + 5; can be written using the short hand assignment
n + = 5;
where + = is the abbreviated or compound operator.
5. INCREMENT AND
DECREMENT OPERATORS :
INCREMENT
OPERATOR :
This
operator is used to increment the value of an integer quantity by one. This is
represented by ‘++’ (double plus) symbol. This symbol can be placed before or
after the integer variable, then
++int_var - indicates pre increment i.e., the value of
int_var must be incremented before it is used. (increment and use)
int_var++ -
indicates post increment i.e., the value of
int_var is used first then increment it (use and increment)
Example : int x, y; x = 10;
y
= ++ x;
DECREMENT OPERATOR :
This
operator is used to reduce the value of an integer quantity by one. This is
represented by ‘--’ (double minus) symbol. This symbol can be placed before or
after the integer variable, then
--int_var - indicates decrement the value of int_var
first and then use it.
int_var-- -
indicates use the value of int_var first and then decrement it.
Example : int x, y; x
= 10;
y = -- x;
Difference between post fix
and pre fix operators :
Pre-increment/decrement |
Post
increment/decrement |
1. The
pre increment or decrement operator first increment or decrement the value of
its operand by 1 and then assigns
the resulting value to the variable on the left hand side. For example : int
a, b; a = 15; b = + +
a; Here, the value of a
is incremented by 1 and then this value 16 is assigned to b. |
2. The post
increment or decrement operator first
assigns the value to the variable on the left hand side and then increment or
decrements the operand by 1. For
example : int m, n; n = 2; m = n - - ; Here, the value of n is first assigned
to m and then decremented by 1, i.e., n becomes 1, but value of m is 2. |
6. CONDITIONAL OPERATOR ( TERNARY OPERATOR) :
The
ternary operator is a conditional operator in C. There is only one such
operator in C. It takes three operands. The symbol ‘?’ is used as a ternary
operator in C.
Syntax
:
Variable = expr1 ? expr2 : expr3 ; The general form of
ternary expression is :
Variable
= (condition) ? true : false ;
1.
The expr1 is
evaluated first.
2.
If expr1 is true,
expr2 is evaluated and its value assigned to
variable.
3.
If expr1 is
false, expr3 is evaluated and its value assigned to the variable.
4.
Either expr2 or
expr3 will be evaluated, but never both.
Example : m = 5; n =
10;
big
= ( m > n ) ? m : n ;
In the above example m > n is evaluated first
giving the result false. Since the expr1 is false the value of n is assigned to
big. Therefore the value of big is 10.
7. BITWISE OPERATORS :
All data items are stored in the computer’s memory as
a sequence of bits (0’s and 1’s), and some applications need the manipulation
of these bits. Manipulation of individual bits is carried out in machine
language or assembly language. To perform, the bitwise operations, C provides
six operators. These operators work with int and char type data. They cannot be
used with floating-point numbers.
Operators |
Symbol Name |
Meaning |
& |
Ampersand |
Bitwise AND |
| |
Pipeline |
Bitwise OR |
^ |
Caret |
Exclusive – OR (XOR) |
~ |
Tilde |
1’s complement |
<< |
Double
less than |
Left
shifting of bits |
>> |
Double greater than |
Right shifting of bits |
Note
: Bitwise operators cannot be applied to float or double. They can be applied
to integers only.
Bitwise AND operator : It is represented by the symbol & (ampersand). The
result of AND operation is 1 if both operand bits are 1, otherwise, the result
is zero.
Example : x=4 & y=5 results in 4
i.e., binary value of x is 0100 binary value of y is
0101
-----------
x & y, After
AND operation 0100 i.e., 4
Bitwise
OR operator : It is represented by the symbol | (pipe). It is also known as
Inclusive OR. The result of bitwise OR operation is 1 if either or both
corresponding bits has a value 1, otherwise it is zero.
Example : x = 4 and y = 9
i.e., binary value of x is 0100 binary
value of y is 1001
-----------
x | y, After
OR operation is 1101 i.e., 13
Bitwise exclusive OR operator : It is represented by the symbol ^ (caret). The result
of bitwise exclusive OR operation is 1, if one of the corresponding bit is 1,
otherwise it is zero.
Example : x = 4 and y = 9
i.e., binary value of x is 0100 binary value of y is
1001
-----------
x ^ y, After
exclusive OR operation is 1101 i.e.,
13
Bitwise
Complement operator : The complement
operator is represented by ~ (tilde) and is also known as one’s complement.
Example : x = 4
i.e.,
x is 0100 Then
~x
is 1011
Bitwise shift operator : The shift operators are used to push the bit patterns
to the right or left by the bit number of bits given by their right operand.
The Right Shift operator is of the form :
Syntax :
Expression
>> n;
Where
n is the number of bit positions to be shifted. However, the right most n bits
are lost.
Further the left most n bits which are vacant will be
filled with 0. Example : num contains the bit pattern
110011110 then
Num>>1
gives 011001111
Num>>3
gives 000110011
Num>>5
gives 000001100
The Left Shift operator is of the form :
Syntax :
Expression
<< n,
Where
n is the number of bit positions to be shifted. The leftmost n bits are lost
and the right most n bits are vacant and filled with zeros.
Example : num contains the bit pattern
11010111 then
Num<<1
gives 10101110
Num<<3
gives 10111000
Num<<5
gives 11100000
8] SPECIAL
OPERATORS OF C
C contains the
following special operators.
Operator |
Description |
, |
Comma operator |
sizeof |
Size in bytes |
& |
Address of operand |
* |
Access value from the address |
. (dot) |
Dot operator |
|
Arrow operator |
COMMA OPERATOR :
The comma operator is basically associated with the for statement. It is also used to link
two or more related expressions together. In such cases, the expressions are
evaluated in a left-to-right fashion. And, we can obtain the value of the
combined expression from the value of the rightmost expression.
Example: sum = (a = 12, b = 22, a+b);
There are three expressions namely, a =
12, b = 22 and a + b. The value of the rightmost expression, 33 (i.e., a + b =
12 + 22 = 33) is set to the variable sum.
sizeof( ) OPERATOR :
The sizeof( ) operator returns the size (i.e., number
of bytes) of an operand. It is a function and therefore has to be written in
lowercase letters. It must precede its operand. The operand may be constant, a
variable or a data type. It is normally used to determine the size of arrays
and structures. The syntax of sizeof( ) operator is as follows.
sizeof(operand);
Example :
x = sizeof(int); y = sizeof(sum);
Consider the following
table :
Variable
declaration |
Expression |
Value |
int
num |
sizeof(num) |
2 |
long
num |
sizeof(num) |
4 |
float num |
sizeof(num) |
4 |
char ch |
sizeof(ch) |
1 |
double num |
sizeof(num) |
8 |
int a, b; |
sizeof(a+2*b) |
2 |
EXPRESSIONS :
It
is a combination of operators and operands that computes to a specific value.
There are three types of expressions.
1.
Arithmetic Expression
2.
Relational Expression
3.
Logical Expression
1. ARITHMETIC EXPRESSIONS
An expression involving arithmetic
operators is called an arithmetic expression. It takes one or more operands and
connects them by an arithmetic operator. These operands are either integers or
floating point numbers.
EVALUATION OF ARITHMETIC EXPRESSION
Arithmetic
expressions are evaluated from left to right. Expressions involving high
priority operators are evaluated first.
Rules for evaluation of arithmetic expressions :
1)
If the given
expression involves parentheses, then the expressions inside the parenthesis
must be evaluated first.
2)
If a unary minus
is present in the expressions, then the term associated with unary minus must
be evaluated before the other expressions.
Precedence and
associativity of arithmetic operators
Precedence
Level |
Operators |
Associativity |
1 |
(
) expressions inside the brackets |
|
2 |
Exponent |
|
3 |
++, - -, unary minus ( - ) |
Right to Left |
4 |
*, /, % |
Left to Right |
5 |
+, - |
Left to Right |
Example
:
1. 3abc = 3 * a * b *
c
2. 2(a+bc) = 2 *
( a + b * c)
3. s+y z
= ( x + y ) / z
4. 2p2 + 3p + 1 = 2 * p
* p + 3 * p + 1
5. ( 2x
+ 1 )( 3y + z ) = ( 2 * x + 1 ) * ( 3 * y + z )
6.
√a + b = sqrt (
a + b )
7.
(a+b) (a–b)
= ( a + b ) / (
a – b )
Evaluate the
following expression :
X |
= |
9 – 12 / ( 3 + 3 ) * ( 2 – 1 ) |
X |
= |
9 – 12 / 6 * 1 |
X |
= |
9 – 2 * 1 |
X |
= |
9 – 2 |
X |
= |
7 |
2. RELATIONAL EXPRESSIONS
A relational expression in a valid combination of
variables, constant and relational operators. Generally relational expressions
compare the values of two variables or expressions, through relational
operators. These operators are used to construct expressions that evaluate to
true or false.
Relational
operators, their precedence and associativity for evaluation of an expression.
Operator |
Meaning |
Precedence |
Associativity |
< |
Lesser than |
1 |
Left to Right |
< = |
Lesser
than or Equal to |
1 |
Left to Right |
> |
Greater than |
1 |
Left to Right |
> = |
Greater than or Equal to |
1 |
Left to Right |
= = |
Equal
to |
2 |
Left to Right |
! = |
Not equal to |
2 |
Left to Right |
Syntax : Expression1
Relational operator Expression2
Where, Expression1 and Expression2 are
arithmetic expressions, which may be simple constants, variable or combination
of them.
Example
:
4.5 < = 10 - TRUE
4.5 > 10 - FALSE
A+B = = C + D - TRUE
if the sum of (A+B) is equal to (C+D)
3. LOGICAL EXPRESSIONS
Expressions involving the logical operators are called
Logical Expressions. The result of a logical expression is either TRUE
(Non-Zero) or FALSE (Zero). The logical expression involves AND ( && ),
OR ( | | ) and NOT ( ! ) operators.
Operator |
Meaning |
Precedence |
Associativity |
&& (double ampersand) |
Logical
AND |
2 |
L
to R |
|| (double pipeline) |
Logical OR |
3 |
L to R |
! (exclamation) |
Logical
NOT |
1 |
L
to R |
Example : ( marks > = 35 )
&& ( marks < = 100) ( ch > = ‘A’) & & ( ch < = ‘Z’)
TYPE CONVERSION
In some applications, we may often want to
change the data type of the variable. When we declare some variable as int, the
desired output may be float or vice versa. In
such situations, we may change
the nature of data stored in a variable. This process is known as data type
conversion. It is also called type casting.
The process of converting one predefined
type into another is called type conversion. There are two types.
1.
Implicit type conversion
2.
Explicit type conversion
IMPLICIT TYPE CONVERSION :
This conversion performed by compiler without programmers
intervention. This conversion is applied generally whenever different data
types are intermixed in an expression.
Assignment |
Conversion |
Result |
int = float |
Converts float into int by
truncating the fraction part |
int x; foat y=25.5543; x=y; value of x is
25. |
float = int |
Converts
int to float by adding zeros to the fractional
part |
float y; y = 10; value of y is
10.000000 |
double = float |
Converts float to double
by addition fractional part with across. |
float x = 5.123456;
double y; y = x; The value of y
ix 5.1234560000 |
float = double |
Double value is rounded to
the precision of float before truncating |
float x; double y =
2.5678987650; x=y; The value x is
2.567899 |
int = char char = int |
Assign the ASCII code of
char to int. Assign the ASCII code of int to char |
int x; x = ‘A’ value
of x is 65 char a; a = 65; value of a is
‘A’ |
EXPLICIT TYPE CONVERSION :
The implicit type conversions are
automatic. However, a user can convert data explicitly by type casting.
The
general form of type casting is as follows :
(data
type) expression; Where,
data type – any basic data type and
must be written within the parentheses.
Example :
n1 = ( int ) 25.5;
n2 = ( int ) 20.5 / ( int ) 5.5; n3 = (
int ) ( p+q )
n4 = ( int ) p + q
MATHEMATICAL
FUNCTIONS
There are certain functions which are
readily available in the language. C itself are called library functions.
C provides a large number of library functions which
perform specific operations. In C all mathematical functions are enclosed in
the header file math.h. Mathematical functions are also included among these
library functions.
Function |
Purpose |
Usage |
|
sin(x) |
Sin of x, x is
radians |
If
d=60, x=d*3.142/180, y=sin(x) |
|
cos(x) |
Cosine of x, x
in radians |
If
d=60, x=d*3.142/180, y=cos(x) |
|
tan(x) |
Tangent of x, x
in radians |
If
d=60, x=d*3.142/180, y=tan(x) |
|
exp(x) |
E raised to the
power of x (e = 2.718282) |
If
x = 2.5, y=exp(x) |
|
log(x) |
Natural logarithm of x (base e) |
y=log(3.0) |
|
log10(x) |
Base 10 logarithm of x |
y=log10(3.0) |
|
pow(x, y) |
X raised to the power y, i.e., XY |
y=pow(3,4) |
|
sqrt(x) |
Square root of x |
y=sqrt(25) |
|
fabs(x) |
Absolute value of x |
y=fabs(-3.14) |
|
ciel(x) |
Rounds x up to
the nearest integer |
y=ciel(2.339) |
|
floor(x) |
Rounds x down to the nearest integer |
y=floor(3.339) |
Transfer
the following mathematical expressions in to C expressions :
1. √a + b + c |
= |
sqrt
( a + b + c ) |
2.
( a + b ) 4 |
= |
pow
( a+b, 4 ) |
3. Log10 (x/y + c) |
= |
log ( x/y + c ) |
No comments:
Post a Comment