Vous êtes sur la page 1sur 10

Operators & Expressions

2.1. INTRODUCTION
An operator is a symbol or a special character that tells the compiler to
perform certain mathematical or logical manipulations. Operators are used in
programs to manipulate data and variables. They usually form a part of the
mathematical or logical expressions.
The data items that operators act upon are called operands.
An expression is a combination of variables, constants and operators written
according to the syntax of the language.
2.2. TYPES OF OPERATORS
In C, operators can be classified into various categories based on their utility
and action. They are :
. Arithmetic operators
!. "elational operators
#. $ogical operators
%. Assignment operator
&. Increment and decrement operators
'. Conditional operators
(. )itwise operators
*. Comma operator
+. si,eof operator
2.2.1. Arithmeti Operators
Integers, floating point numbers and double precision numbers can be added,
subtracted, divided or multiplied. The operators used for these arithmetic
operations are called arithmetic operators. C supports all basic arithmetic
operators. The list of arithmetic operators and their meanings are given below :
Operator Meaning
- Addition or unary plus
. /ubtraction or unary minus
0 1ultiplication
2 3ivision
2.2 Operators & Expressions
4 1odulo division or remainder after division
Note !
. C does not have an operator for exponentiation. 5owever there is a
library function pow to carry out exponentiation.
!. The unary minus operator has the effect of multiplying its operand by
6.
#. 1odulo operator 748 re9uires both operands to be integer and second
operand to be non.,ero.
Inte"er Arithmeti ! :hen both the operands in a single arithmetic
expression such as a - b are declared integers, the expression is called an integer
expression. An arithmetic operation performed on these integer is called integer
arithmetic and it always yields an integer value. ;or example, if a and b are
integers, and the values of a < = and b < %, we have the following results :
Integer Arithmetic Expression Result
a - b %
a . b '
a 0 b %=
a 2 b ! 73ecimal part truncated8
a 4 b ! 7"emainder after division8
Note !
. Integer division truncates the decimal part.
!. In modulo division, the sign of the result is always the sign of the first
operand i.e., the dividend.
Rea# Arithmeti ! An arithmetic operation involving only real operands is
called real arithmetic. A real operand may assume values either in decimal or
exponential notation. /ince floating point values are rounded to the number of
significant digits permissible, the final value is an approximation of the correct
result. ;or example, if a and b are floats, and the values of a < &.!& and b < .&,
we have the following results :
Real Arithmetic Expression Result
a - b '.(&====
a 6 b #.(&====
a 0 b (.*(&===
a 2 b #.&=====
Note ! The modulo operator 748 cannot be used with real operands.
Operators & Expressions 2.3
$ixe%&mo%e Arithmeti ! :hen one of the operands is real and the other is
integer, the expression is called a mixed.mode arithmetic expression. If either
operand is of the real type, then only the real operation is performed and the
result is always a real number. ;or example, if a is integer and b is float, and the
values of a < & and b < !.&, we have the following results :
Mixed-mode Arithmetic Expression Result
a - b (.&=====
a . b !.&=====
a 0 b !.&=====
a 2 b !.======
2.2.2. Re#ationa# Operators
The relational operators are used to compare the value between two variables
or between a variable and a constant. The list of relational operators and their
meanings are given below :
Operator Meaning
> $ess than
? @reater than
>< $ess than or e9ual to
?< @reater than or e9ual to
<< A9ual to
B< Cot e9ual to
An expression containing a relational operator is called as relational
expression. The relational operators produce an integer result to express the
condition of the comparison. If the condition is false, then the integer value is =.
If the condition is true, then the integer value is . The syntax is :
exp1 rel_op exp2
:here exp and exp! are arithmetic expressions, which may be simple
constants, variables or combination of both and relDop represents a relational
operator. If a < = and b < %, we have the following results :
Relational Expression Result
a > b ;alse
a ? b True
a >< b ;alse
a ?< b True
a << b ;alse
a B< b True
2.4 Operators & Expressions
Note ! :hen arithmetic expressions are used on either side of a relational
operator, the arithmetic expressions will be evaluated first and then the results
are compared. That is, arithmetic operators have a higher priority over relational
operators.
2.2.'. (o"ia# Operators
$ogical operators are used to combine two or more relations. The logical
operators are called )oolean operators because the tests between values are
reduced to either true or false, with ,ero being false and one being true. The list
of logical operators and their meanings are given below :
Operator Meaning
EE $ogical AC3
FF $ogical O"
B $ogical COT
An expression, which combines two or more relational expressions, is called
as logical expression or a compound relational expression. A logical expression
yields a value of one or ,ero, according to the truth table as shown below :
Op1 Op2 Op1 && Op2 Op1 || Op2
; ; ; ;
; T ; T
T ; ; T
T T T T
The result of the logical AC3 operation will be true, only if both the operands
are true. The result of the logical O" operation will be true if either of the operand
is true or both the operands are true. The logical COT negates the value of a
logical expression, i.e., it causes the expression that originally becomes false and
vice versa. If a < = and b < %, we have the following results :
Logical Expression Result
a ? & EE b ? ! True
a > & EE b > ! ;alse
a ? & EE b > ! ;alse
a ? & FF b ? ! True
a > & FF b > ! ;alse
a ? & FF b > ! True
2.2.). Assi"nment Operator
Operators & Expressions 2.5
The assignment operator is used to assign the result of an expression to a
variable. The most commonly used assignment operator is G<H.
The syntax is :
ar ! exp "
:here var is a variable and exp is represents a constant or a variable or a
complex expression.
Ialid examples are :
n*m + 2, - a"e + 1. - pi + '.1) -
area + '.1) / r / r -
In addition, C has a set of shorthand assignment operator. The syntax is :
ar op ! exp "
:here var is a variable, exp is an expression or constant or variable and op is
a C binary arithmetic operator. The operator op < is Jnown as shorthand
assignment operator. The above syntax is e9uivalent to :
ar ! ar op #exp$ "
Table below shows some of the commonly used shorthand assignment
operator :
%imple assignment
operator
%horthand operator
a < a - a - <
a < a 6 a . <
a < a 0 ! a 0 < !
a < a 2 b a 2 < b
a < a 4 b a 4 < b
c < c 0 7a - b8 c 0 < 7a - b8
b < b 2 7a - b8 b 2 < 7a - b8
2.2.,. Inrement an% Derement Operators
C allows two useful unary operators generally not found in other computer
languages. These are increment 7--8 and decrement 7..8 operators. The operator
-- adds to its operand and 6 subtracts to its operand. The increment and
decrement operators and their meanings are given below :
2.6 Operators & Expressions
Operator Meaning
-- Increment operator 7Adds 8
.. 3ecrement operator 7/ubtracts 8
The syntax is :
&&aria'le_name " --aria'le_name "
or
aria'le_name&& " aria'le_name-- "
The operator placed either before or after the variable name. If the operator
placed before the variable liJe --i or 6.i, it is Jnown as pre.increment and the
pre.decrement respectively. If the operator appears after the variable liJe i-- or
i.., it is Jnown as post increment and post decrement respectively.
2.2.0. Con%itiona# Operators
The conditional operators consist of two symbols, a 9uestion marJ 7K8 and a
colon 7:8. The syntax is :
exp1 ( exp2 ) exp* "
:here exp, exp! and exp# are expressions. 5ere exp is evaluated first, if
exp is true 7that is, if its value is non.,ero8, then the value returned will be exp!
otherwise the value returned will be exp#. It is also called as ternary operator,
since they taJe three arguments.
;or example :
1i" + a 2 1 3 a ! 1 -
If the value of a and b are &= and ==, then the value of == will be assigned
to the variable big, since the condition a ? b becomes false.
2.2.4. 5it6ise Operators
One of CHs powerful features is a set of bit manipulation operators. This
permits the programmer to access and manipulate individual bits within a piece of
data. The bitwise operators can operate upon int and char data types but not on
float and double data types. The list of bitwise operators and their meanings are
given below :
Operator Meaning
E )itwise AC3
F )itwise O"
L )itwise MO"
Operators & Expressions 2.7
>> /hift left
?? /hift right
N OneHs complement
All operators except N operator are binary operators which re9uires two
operands. :hile using the bit operators, each operand is treated as a binary
number consisting of a series of individual s and =s. The respective bits in each
operand are then compared on a bit by bit basis and result is determined based
on the selected operation. Table below shows the result of logical bitwise
operations :
Op1 Op2 Op1 & Op2 Op1 | Op2 Op1 + Op2
= = = = =
= =
= =
=
2.2... Comma Operator
The comma operator can be used to linJ the related expressions together. A
comma.linJed list of expressions are evaluated left to right and the value of right
most expression is the value of the combined expression.
;or example :
s*m + 7a + 189 1 + 289 a : 1; -
5ere, the statement first assigns the value = to a, then assigns != to b, and
finally assigns #= 7i.e. = - !=8 to sum. /ince comma operator has the lowest
precedence of all operators, the parentheses are necessary.
2.2.<. si=eo> Operator
The si,eof is a compile time operator and when used with an operand, it
returns the number of bytes the operand occupies. The operand may be a
variable, a constant or a data type 9ualifier. The syntax is :
si,eo-#expression$ " or
si,eo-#data_t.pe$ "
;or example :
a + si=eo>7s*m; -
1 + si=eo>7#on" int; -
+ si=eo>72',(; -

2.8 Operators & Expressions
The si,eof operator is normally used to determine the lengths of arrays and
structures, when their si,es are not Jnown in advance by the programmer. It is
also used to allocate memory space dynamically to variables during execution of a
program.
2.'. E?A(UATION OF E@PRESSIONS
Axpressions are evaluated using an assignment statement. The syntax is O
aria'le ! expression "
:here variable is any C variable name. An expression evaluation usually
starts from left and passes through the expression to the extreme right. As it
passes, the highest priority operators are encountered first and then the lower
priority operators are encountered. The operators of the same precedence are
evaluated either from left to right or from right to left, depending on the level. )ut
introducing parenthesis in an expression can change the order of evaluation.
:hen parenthesis are used, the expressions within the parenthesis assumes
highest priority. If one or more parenthesis appear in an expression, the left most
parentheses is evaluated first and it passes to the right one by one. Parentheses
are usually used to increase the readability of the program. The result of the
expression is then replaces the previous value of the variable on the left.hand
side. All the variables used in the expression must be assigned values before
evaluation is attempted.
The table below shows some examples of C expressions e9uivalent to
algebraic expression :
Alge'raic Expression E/uialent 0 Expression
#.%x r
!
#.% 0 r 0 r
a
bc
a 0 2 7b 0 c8
&a
!
- #a - & 0 a 0 a - # 0 a -
s7s.a87s.b87s.c8 s 0 7s.a8 0 7s.b8 0 7s.c8
Ialid examples are :
area + # / 1 / h -
root + 1 / 1 A ) / a / -
aB" + s*m C n -
2.). PRECEDENCE OF OPERATORS
Operators & Expressions 2.9
Table below shows the list of operators, their precedence levels and their
rules of association.
Operator 1escription Associatiit. Ran2
78
QR
.? and .
;unction call
Array element reference
/tructure operators
$eft to "ight
-
.
--
..
B
N
0
E
si,eof
7type8
Snary plus
Snary minus
Increment
3ecrement
$ogical negation
OneHs complement
Pointer reference 7indirection8
Address
/i,e of an obTect
Type cast 7conversion8
"ight to left !
0
2
4
1ultiplication
3ivision
1odulus
$eft to "ight #
-
.
Addition
/ubtraction
$eft to "ight %
>>
??
$eft shift
"ight shift
$eft to "ight &
>
><
?
?<
$ess than
$ess than or e9ual to
@reater than
@reater than or e9ual to
$eft to "ight '
<<
B<
A9uality
Ine9uality
$eft to "ight (
E )itwise AC3 $eft to "ight *
L )itwise MO" $eft to "ight +
F )itwise O" $eft to "ight =
EE $ogical AC3 $eft to "ight
FF $ogical O" $eft to "ight !
K: Condition expression "ight to left #
<
0<
2<
4<
-<
.<
E<
L<
Assignment operators "ight to left
%
2.10 Operators & Expressions
F<
>><
??<
, Comma operator $eft to "ight &

Vous aimerez peut-être aussi