SlideShare a Scribd company logo
1 of 46
Hands-on, crash course with code examples
1/46www.tenouk.com, ©
 Operators are symbols which take one or more operands or
expressions and perform arithmetic or logical computations.
 Operands are variables or expressions which are used in
conjunction with operators to evaluate the expression.
 Combination of operands and operators form an expression.
 Expressions are sequences of operators, operands, and
punctuators that specify a computation.
 Evaluation of expressions is based on the operators that the
expressions contain and the context in which they are used.
 Expression can result in a value and can produce side effects.
 A side effect is a change in the state of the execution
environment.
2/46www.tenouk.com, ©
 An expression is any valid set of literals, variables,
operators, operands and expressions that evaluates
to a single value.
 This value can be a number, a string or a logical
value.
 For instance a = b + c; denotes an expression in
which there are 3 operands a, b, c and two
operator + and =.
 A statement, the smallest independent
computational unit, specifies an action to be
performed.
 In most cases, statements are executed in sequence.
 The number of operands of an operator is called its
arity.
 Based on arity, operators are classified as nullary
(no operands), unary (1 operand), binary (2 3/46www.tenouk.com, ©
Operators Description Example Usage
Postfix operators
()
Function call operator. A function call
is an expression containing the
function name followed by the
function call operator, (). If the
function has been defined to receive
parameters, the values that are to be
sent into the function are listed inside
the parentheses of the function call
operator. The argument list can
contain any number of expressions
separated by commas. It can also be
empty.
sumUp(inum1, inum2)
displayName()
student(cname, iage,
caddress)
Calculate(length, wide +
7)
4/46www.tenouk.com, ©
[]
Array subscripting operator. A
postfix expression followed by an
expression in [ ] (brackets)
specifies an element of an array.
The expression within the brackets
is referred to as a subscript. The
first element of an array has the
subscript zero.
#include <stdio.h>
int main(void)
{
int a[3] = { 11, 12, 33 };
printf("a[0] = %dn", a[0]);
return 0;
}
.
Dot operator used to access class,
structure, or union members type.
objectVar.memberOfStructUnion
->
Arrow operator used to access
class, structure or union members
using a pointer type.
aptrTo->memberOfStructUnion
1. Example 1: function call operator
2. Example 2: array subscripting operator
3. Example 3: pointer dot and arrow operator
5/46www.tenouk.com, ©
Unary Operators
+
Unary plus maintains the value of the operand.
Any plus sign in front of a constant is not part
of the constant.
+aNumber
-
Unary minus operator negates the value of the
operand. For example, if num variable has the
value 200, -num has the value -200. Any
minus sign in front of a constant is not part of
the constant.
-342
6/46www.tenouk.com, ©
You can put the ++/-- before or after the operand. If it appears before the
operand, the operand is incremented/decremented. The incremented
value is then used in the expression. If you put the ++/-- after the
operand, the value of the operand is used in the expression before the
operand is incremented/decremented.
++
Post-increment. After the result is obtained, the
value of the operand is incremented by 1.
aNumber++
--
Post-decrement. After the result is obtained,
the value of the operand is decremented by 1.
aNumber--
++
Pre-increment. The operand is incremented by
1 and its new value is the result of the
expression.
++yourNumber
--
Pre-decrement. The operand is decremented
by 1 and its new value is the result of the
expression.
--yourNumber
7/46www.tenouk.com, ©
&
The address-of operator (&) gives the address of its
operand. The operand of the address-of operator can
be either a function designator or an l-value that
designates an object that is not a bit field and is not
declared with the register storage-class specifier.
The result of the address operation is a pointer to the
operand. The type addressed by the pointer is the type
of the operand.
&addressOfData
*
The indirection operator (*) accesses a value
indirectly, through a pointer. The operand must be a
pointer value. The result of the operation is the value
addressed by the operand; that is, the value at the
address to which its operand points. The type of the
result is the type that the operand addresses.
*aPointerTo
8/46www.tenouk.com, ©
sizeof
sizeof operator for
expressions
sizeof 723
sizeof() sizeof operator for types sizeof(char)
(type) cast-
expression
Explicit conversion of the type
of an object in a specific
situation. This is also call
automatic promotion.
(float)i
Program example: plus, minus, address-of, pre and post increment/decrement,
sizeof() and type promotion
9/46www.tenouk.com, ©
Multiplicative Operators
* The multiplication operator causes its two operands to be multiplied. p = q * r;
/
The division operator causes the first operand to be divided by the
second. If two integer operands are divided and the result is not an
integer, it is truncated according to the following rules:
1.The result of division by 0 is undefined according to the ANSI C
standard. The Microsoft C compiler generates an error at compile time or
run time.
2.If both operands are positive or unsigned, the result is truncated
toward 0.
3.If either operand is negative, whether the result of the operation is the
largest integer less than or equal to the algebraic quotient or is the
smallest integer greater than or equal to the algebraic quotient is
implementation defined.
a = b / c;
%
The result of the remainder operator is the remainder when the first
operand is divided by the second. When the division is inexact, the result
is determined by the following rules:
1.If the right operand is zero, the result is undefined.
2.If both operands are positive or unsigned, the result is positive.
3.If either operand is negative and the result is inexact, the result is
implementation defined.
x = y % z;
10/46www.tenouk.com, ©
Addition and subtraction Operators
+ addition d = e + f
- subtraction r = s – t;
 The operands can be integral or floating values. Some additive
operations can also be performed on pointer values, as outlined
under the discussion of each operator.
 The additive operators perform the usual arithmetic conversions
on integral and floating operands. The type of the result is the
type of the operands after conversion.
 Since the conversions performed by the additive operators do
not provide for overflow or underflow conditions, information may
be lost if the result of an additive operation cannot be
represented in the type of the operands after conversion.
Program example: assignment, add, subtract, multiply, divide and modulus
11/46www.tenouk.com, ©
 For relational expression, 0 is FALSE, 1 is TRUE.
 Any numeric value is interpreted as either TRUE
or FALSE when it is used in a C / C++
expression or statement that is expecting a
logical (true or false) value. The rules are:
◦ A value of 0 represents FALSE.
◦ Any non-zero (including negative numbers) value
represents TRUE.
12/46
Program example: true, false and negate
www.tenouk.com, ©
Relational Inequality Operators
The relational operators compare two operands and determine the validity of a
relationship.
<
Specifies whether the value of the left operand is less than the
value of the right operand. The type of the result is int and
has the value 1 if the specified relationship is true, and 0 if
false.
i < 7
>
Specifies whether the value of the left operand is greater than
the value of the right operand. The type of the result is int
and has the value 1 if the specified relationship is true, and 0 if
false.
j > 5
<=
Specifies whether the value of the left operand is less than or
equal to the value of the right operand. The type of the result is
int and has the values 1 if the specified relationship is true,
and 0 if false.
k <= 4
>=
Specifies whether the value of the left operand is greater than
or equal to the value of the right operand. The type of the
result is int and has the values 1 if the specified relationship
is true, and 0 if false.
p >= 3
13/46www.tenouk.com, ©
Relational Equality Operators
The equality operators, like the relational operators, compare two operands for the validity of a
relationship.
==
Indicates whether the value of the left operand is equal to the
value of the right operand. The type of the result is int and has
the value 1 if the specified relationship is true, and 0 if false. The
equality operator (==) should not be confused with the
assignment (=) operator. For example:
if (x == 4) evaluates to true (or 1) if x is equal to four.
while
if (x = 4) is taken to be true because (x = 4) evaluates to a
nonzero value (4). The expression also assigns the value 4 to x.
nChoice == 'Y'
!=
Indicates whether the value of the left operand is not equal to the
value of the right operand. The type of the result is int and has
the value 1 if the specified relationship is true, and 0 if false.
nChoice != 'Y'
14/46www.tenouk.com, ©
Expressions Evaluates as
(3 == 3) && (4 != 3) True (1) because both operands are true
(4 > 2) || (7 < 11)
True (1) because (either) one
operand/expression is true
(3 == 2) && (7 == 7) False (0) because one operand is false
! (4 == 3) True (1) because the expression is false
NOT(FALSE) = TRUE
NOT(TRUE) = FALSE
15/46
Program example: less-than, greater-than, less-than and equal-to, greater-than and
www.tenouk.com, ©
Logical NOT Operator (unary)
!
Logical not operator. Produces value 0 if its
operand or expression is true (nonzero) and the
value 1 if its operand or expression is false (0). The
result has an int type. The operand must be an
integral, floating, or pointer value.
!(4 == 2)
!x
Operand (or expression) Output
!0 1 ( T )
!1 0 ( F )
16/46www.tenouk.com, ©
Logical AND Operator
&&
Indicates whether both operands are true.
If both operands have nonzero values, the result has the value 1.
Otherwise, the result has the value 0. The type of the result is
int. Both operands must have an arithmetic or pointer type. The
usual arithmetic conversions on each operand are performed.
The logical AND (&&) should not be confused with the bitwise AND
(&) operator. For example:
1 && 4 evaluates to 1 (True && True = True)
while
1 & 4 (0001 & 0100 = 0000) evaluates to 0
x && y
Operand1 Operand2 Output
0 0 0 ( F )
0 1 0 ( F )
1 0 0 ( F )
1 1 1 ( T )
17/46www.tenouk.com, ©
Logical OR Operator
||
Indicates whether either operand is true. If either of the
operands has a nonzero value, the result has the value
1. Otherwise, the result has the value 0. The type of the
result is int. Both operands must have a arithmetic or
pointer type. The usual arithmetic conversions on each
operand are performed.
The logical OR (||) should not be confused with the
bitwise OR (|) operator. For example:
1 || 4 evaluates to 1 (or True || True = True)
while 1 | 4 (0001 | 0100 = 0101) evaluates to 5
x || y
Operand1 Operand2 Output
0 0 0 ( F )
0 1 1 ( T )
1 0 1 ( T )
1 1 1 ( T )
Program example: Logical-AND, logical-OR
18/46www.tenouk.com, ©
Conditional Operator (ternary)
?:
The first operand/expression is evaluated, and
its value determines whether the second or
third operand/expression is evaluated:
1.If the value is true, the second
operand/expression is evaluated.
2.If the value is false, the third
operand/expression is evaluated.
The result is the value of the second or third
operand/expression. The syntax is:
First operand ? second operand :
third operand
size != 0 ? size : 0
Program example: tenary conditional operator
19/46www.tenouk.com, ©
 The compound assignment operators consist of a
binary operator and the simple assignment
operator.
 They perform the operation of the binary operator
on both operands and store the result of that
operation into the left operand.
 The following table lists the simple and compound
assignment operators and expression examples:
20/46www.tenouk.com, ©
Simple Assignment Operator
=
 The simple assignment operator has
the following form:
lvalue = expr
 The operator stores the value of the
right operand expr in the object
designated by the left operand lvalue.
 The left operand must be a modifiable
lvalue.
 The type of an assignment operation is
the type of the left operand.
i = 5 + x;
21/46www.tenouk.com, ©
Compound
Assignment
Operator
Example Equivalent expression
identifier operator= entity represents identifier =
identifier operator entity
+= nindex += 3 index = nindex + 3
-= *(paPter++) -= 1 * paPter = *( paPter ++) - 1
*= fbonus *= fpercent fbonus = fbonus * fpercent
/= ftimePeriod /= fhours
ftimePeriod = ftimePeriod /
fhours
%= fallowance %= 80 fallowance = fallowance % 80
<<= iresult <<= inum iresult = iresult << inum
>>= byleftForm >>= 1 byleftForm = byleftForm >> 1
&= bybitMask &= 2 bybitMask = bybitMask & 2
^= itestSet ^= imainTest itestSet = itestSet ^ imainTest
|= bflag |= bonBit bflag = bflag | bonBit
Program example: compound operators
22/46www.tenouk.com, ©
Comma Operator
,
A comma expression contains two operands of any type separated by a comma and has
left-to-right associativity. The left operand is fully evaluated, possibly producing side effects,
and its value, if there is one, is discarded. The right operand is then evaluated. The type
and value of the result of a comma expression are those of its right operand, after the usual
unary conversions. In some contexts where the comma character is used, parentheses are
required to avoid ambiguity. The primary use of the comma operator is to produce side
effects in the following situations:
1.Calling a function.
2.Entering or repeating an iteration loop.
3.Testing a condition.
4.Other situations where a side effect is required but the result of the expression is not
immediately needed.
The use of the comma token as an operator is distinct from its use in function calls and
definitions, variable declarations, enum declarations, and similar constructs, where it acts
as a separator.
Because the comma operator discards its first operand, it is generally only useful where the
first operand has desirable side effects, such as in the initializer or the counting expression
of a for loop.
23/46www.tenouk.com, ©
 The following table gives some examples of the uses of the comma operator.
Statement Effects
for (i=0; i<4; ++i, func());
A for statement in which i is incremented and
func() is called at each iteration.
if (func(), ++i, i>1 )
{ /* ... */ }
An if statement in which function func() is called,
variable i is incremented, and variable i is tested
against a value. The first two expressions within this
comma expression are evaluated before the
expression i>1. Regardless of the results of the first
two expressions, the third is evaluated and its result
determines whether the if statement is processed.
func1((++iaArg, func2(iaArg)));
A function call to func1() in which iaArg is
incremented, the resulting value is passed to a
function func2(), and the return value of func2() is
passed to func1(). The function func1() is passed
only a single argument, because the comma
expression is enclosed in parentheses within the
function argument list.
int inum=3, inum2=7, inum3 = 2; Comma acts as separator, not as an operator.
Program example: comma operator
24/46www.tenouk.com, ©
Bitwise (complement) NOT Operators
~
The ~ (bitwise negation) operator yields the bitwise (one)
complement of the operand. In the binary representation of the
result, every bit has the opposite value of the same bit in the binary
representation of the operand. The operand must have an integral
type. The result has the same type as the operand but is not an
lvalue (left value). The symbol used called tilde.
Suppose byNum variable represents the decimal value 8. The 16-bit binary representation of
byNum is:
00000000 00001000
The expression ~byNum yields the following result (represented here as a 16-bit binary
number):
11111111 11110111
Note that the ~ character can be represented by the trigraph ??-.
The 16-bit binary representation of ~0 (which is ~00000000 00000000) is:
11111111 11111111
25/46www.tenouk.com, ©
Bitwise Shift Operators
<<
Left shift operator, shift their first operand left (<<) by the
number of positions specified by the second operand.
nbits << nshiftSize
>>
Right shift operator, shift their first operand right (>>) by the
number of positions specified by the second operand.
nbits >> nshiftSize
 Both operands must be integral values. These operators perform the usual arithmetic conversions; the
type of the result is the type of the left operand after conversion.
 For leftward shifts, the vacated right bits are set to 0. For rightward shifts, the vacated left bits are
filled based on the type of the first operand after conversion. If the type is unsigned, they are set to 0.
Otherwise, they are filled with copies of the sign bit. For left-shift operators without overflow, the
statement:
expression1 << expression2
 is equivalent to multiplication by 2expression2
. For right-shift operators:
expression1 >> expression2
 is equivalent to division by 2expression2
if expression1 is unsigned or has a nonnegative value.
 The result of a shift operation is undefined if the second operand is negative, or if the right operand is
greater than or equal to the width in bits of the promoted left operand.
 Since the conversions performed by the shift operators do not provide for overflow or underflow
conditions, information may be lost if the result of a shift operation cannot be represented in the type
of the first operand after conversion.
26/46www.tenouk.com, ©
Program example: bitwise shift left, bitwise shift right and bitwise-NOT operators
27/46www.tenouk.com, ©
Bitwise AND Operator
&
 The & (bitwise AND) operator compares each bit of its first
operand to the corresponding bit of the second operand. If both
bits are 1's, the corresponding bit of the result is set to 1.
Otherwise, it sets the corresponding result bit to 0.
 Both operands must have an integral or enumeration type. The
usual arithmetic conversions on each operand are performed.
The result has the same type as the converted operands.
 Because the bitwise AND operator has both associative and
commutative properties, the compiler can rearrange the operands
in an expression that contains more than one bitwise AND
operator.
 The bitwise AND (&) should not be confused with the logical AND.
(&&) operator. For example:
1 & 4 evaluates to 0 (0001 & 0100 = 0000) while
1 && 4 evaluates to true [True && True = True]
28/46www.tenouk.com, ©
29/46www.tenouk.com, ©
Bitwise XOR Operator
^
 The bitwise exclusive OR operator (in EBCDIC, the ^ symbol is
represented by the ¬ symbol) compares each bit of its first
operand to the corresponding bit of the second operand. If both
bits are 1's or both bits are 0's, the corresponding bit of the result
is set to 0. Otherwise, it sets the corresponding result bit to 1.
 Both operands must have an integral or enumeration type. The
usual arithmetic conversions on each operand are performed. The
result has the same type as the converted operands and is not an
lvalue (left value).
 Because the bitwise exclusive OR operator has both associative
and commutative properties, the compiler can rearrange the
operands in an expression that contains more than one bitwise
exclusive OR operator. Note that the ^ character can be
represented by the trigraph ??'. The symbol used called caret.
30/46www.tenouk.com, ©
31/46www.tenouk.com, ©
Bitwise (Inclusive) OR Operator
|
 The | (bitwise inclusive OR) operator compares the values (in binary
format) of each operand and yields a value whose bit pattern shows
which bits in either of the operands has the value 1. If both of the bits are
0, the result of that bit is 0; otherwise, the result is 1.
 Both operands must have an integral or enumeration type. The usual
arithmetic conversions on each operand are performed. The result has
the same type as the converted operands and is not an lvalue.
 Because the bitwise inclusive OR operator has both associative and
commutative properties, the compiler can rearrange the operands in an
expression that contains more than one bitwise inclusive OR operator.
Note that the | character can be represented by the trigraph ??!.
 The bitwise OR (|) should not be confused with the logical OR (||)
operator. For example:
1 | 4 evaluates to 5 (0001 | 0100 = 0101) while 1 || 4 (True
|| True = True) evaluates to true
32/46www.tenouk.com, ©
Program example: bitwise-AND, bitwise-OR and eXclusive-OR
(XOR) operators
33/46www.tenouk.com, ©
 Consider the following arithmetic operation:
- left to right
6 / 2 * 1 + 2 = 5
- right to left
6/2 * 1 + 2 = 1
- using parentheses
= 6 / (2 * 1) + 2
= (6 / 2) + 2
= 3 + 2
= 5
34/46
Inconsistent
answers!
www.tenouk.com, ©
 Operator precedence: a rule used to
clarify unambiguously which operations
(operator and operands) should be
performed first in the given (mathematical)
expression.
 Use precedence levels that conform to the
order commonly used in mathematics.
 However, parentheses take the highest
precedence and operation performed from
the innermost to the outermost
parentheses. 35/46www.tenouk.com, ©
 Precedence and associativity of C
operators affect the grouping and
evaluation of operands in expressions.
 Is meaningful only if other operators
with higher or lower precedence are
present.
 Expressions with higher-precedence
operators are evaluated first.
36/46www.tenouk.com, ©
Precedence and Associativity of C Operators
Symbol Type of Operation Associativity
[ ] ( ) . –> postfix ++ and postfix –– Expression Left to right
prefix ++ and prefix –– sizeof
& * + – ~ !
Unary Right to left
typecasts Unary Right to left
* / % Multiplicative Left to right
+ – Additive Left to right
<< >> Bitwise shift Left to right
< > <= >= Relational Left to right
== != Equality Left to right
& Bitwise-AND Left to right
^ Bitwise-exclusive-OR Left to right
| Bitwise-inclusive-OR Left to right
&& Logical-AND Left to right
|| Logical-OR Left to right
? : Conditional-expression Right to left
= *= /= %=
+= –= <<= >>= &=
^= |=
Simple and compound
assignment
Right to left
, Sequential evaluation Left to right 37/46www.tenouk.com, ©
 The precedence and associativity (the order
in which the operands are evaluated) of C
operators.
 In the order of precedence from highest to
lowest.
 If several operators appear together, they
have equal precedence and are evaluated
according to their associativity.
 All simple and compound-assignment
operators have equal precedence.
38/46www.tenouk.com, ©
 Operators with equal precedence such
as + and -, evaluation proceeds
according to the associativity of the
operator, either from right to left or from
left to right.
 The direction of evaluation does not
affect the results of expressions that
include more than one multiplication
(*), addition (+), or binary-bitwise (& |
^) operator at the same level.
39/46www.tenouk.com, ©
 e.g:
 Order of operations is not defined by the
language.
 The compiler is free to evaluate such
expressions in any order, if the compiler can
guarantee a consistent result.
40/46
3 + 5 + (3 + 2) = 13 – right to left
(3 + 5) + 3 + 2 = 13 – left to right
3 + (5 + 3) + 2 = 13 – from middle
www.tenouk.com, ©
 Only the sequential-evaluation (,), logical-AND
(&&), logical-OR (||), conditional-expression
(? :), and function-call operators constitute
sequence points and therefore guarantee a
particular order of evaluation for their operands.
 The sequential-evaluation operator (,) is
guaranteed to evaluate its operands from left to
right.
 The comma operator in a function call is not the
same as the sequential-evaluation operator and
does not provide any such guarantee.
41/46www.tenouk.com, ©
 Logical operators also guarantee evaluation
of their operands from left to right.
 But, they evaluate the smallest number of
operands needed to determine the result of
the expression.
 This is called "short-circuit" evaluation.
 Thus, some operands of the expression
may not be evaluated.
42/46www.tenouk.com, ©
 For example:
 The second operand, y++, is
evaluated only if x is true (nonzero).
 Thus, y is not incremented if x is
false (0).
43/46
x && y++
www.tenouk.com, ©
 Label the execution order for the following expressions
44/46www.tenouk.com, ©
a.(rate*rate) + delta
b.2*(salary + bonus)
c. 1/(time + (3*mass))
d.(a - 7) / (t + (9 * v))
45/46
 Convert the following operations to C
expression
www.tenouk.com, ©
46/46www.tenouk.com, ©

More Related Content

What's hot

Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentationkunal kishore
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming Kamal Acharya
 
Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailgourav kottawar
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressionsvinay arora
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cSowmya Jyothi
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++bajiajugal
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7REHAN IJAZ
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++Pranav Ghildiyal
 

What's hot (20)

Basics of c++
Basics of c++ Basics of c++
Basics of c++
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
05 operators
05   operators05   operators
05 operators
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Operators
OperatorsOperators
Operators
 
Java 2
Java 2Java 2
Java 2
 
Operators & Casts
Operators & CastsOperators & Casts
Operators & Casts
 
Week2 dq4
Week2 dq4Week2 dq4
Week2 dq4
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Operators in c++
Operators in c++Operators in c++
Operators in c++
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
 

Viewers also liked

Методология Mobilebanking 2015
Методология Mobilebanking 2015Методология Mobilebanking 2015
Методология Mobilebanking 2015Kirill Kochkin
 
DÉCRETO 96/2006 DE ORENACIÓN DE PROFESIONES TURÍSTICAS EN CASTILLA LA MANCHA
DÉCRETO 96/2006 DE ORENACIÓN DE PROFESIONES TURÍSTICAS EN CASTILLA LA MANCHADÉCRETO 96/2006 DE ORENACIÓN DE PROFESIONES TURÍSTICAS EN CASTILLA LA MANCHA
DÉCRETO 96/2006 DE ORENACIÓN DE PROFESIONES TURÍSTICAS EN CASTILLA LA MANCHASusanita Ratón
 
GGgнагородження 2015
GGgнагородження 2015GGgнагородження 2015
GGgнагородження 2015school8zv
 
Contoh perampokan
Contoh perampokanContoh perampokan
Contoh perampokanrohis
 
Stem Cell Therapy for Anti – Ageing ,DR. SHARDA JAIN, Dr. Prabhu Mishra , Dr...
Stem Cell Therapy for Anti – Ageing ,DR. SHARDA JAIN, Dr. Prabhu Mishra , Dr...Stem Cell Therapy for Anti – Ageing ,DR. SHARDA JAIN, Dr. Prabhu Mishra , Dr...
Stem Cell Therapy for Anti – Ageing ,DR. SHARDA JAIN, Dr. Prabhu Mishra , Dr...StemcellGP21
 
праздник
праздникпраздник
праздникserg32
 
Presentasjon gjestehuset
Presentasjon gjestehusetPresentasjon gjestehuset
Presentasjon gjestehusetFay Johansen
 
Boletín 15-04-15
Boletín 15-04-15Boletín 15-04-15
Boletín 15-04-15Openbank
 
найкрасивіші дороги світу
найкрасивіші дороги світунайкрасивіші дороги світу
найкрасивіші дороги світуNataSulik
 

Viewers also liked (12)

Методология Mobilebanking 2015
Методология Mobilebanking 2015Методология Mobilebanking 2015
Методология Mobilebanking 2015
 
DÉCRETO 96/2006 DE ORENACIÓN DE PROFESIONES TURÍSTICAS EN CASTILLA LA MANCHA
DÉCRETO 96/2006 DE ORENACIÓN DE PROFESIONES TURÍSTICAS EN CASTILLA LA MANCHADÉCRETO 96/2006 DE ORENACIÓN DE PROFESIONES TURÍSTICAS EN CASTILLA LA MANCHA
DÉCRETO 96/2006 DE ORENACIÓN DE PROFESIONES TURÍSTICAS EN CASTILLA LA MANCHA
 
Design of robust plant layout for dynamic plant layout problem2
Design of robust plant layout for dynamic plant layout problem2Design of robust plant layout for dynamic plant layout problem2
Design of robust plant layout for dynamic plant layout problem2
 
GGgнагородження 2015
GGgнагородження 2015GGgнагородження 2015
GGgнагородження 2015
 
Contoh perampokan
Contoh perampokanContoh perampokan
Contoh perampokan
 
Design of robust layout for dynamic plant layout layout problem 1
Design of robust layout for dynamic plant layout layout problem 1Design of robust layout for dynamic plant layout layout problem 1
Design of robust layout for dynamic plant layout layout problem 1
 
Kelompok 7
Kelompok 7Kelompok 7
Kelompok 7
 
Stem Cell Therapy for Anti – Ageing ,DR. SHARDA JAIN, Dr. Prabhu Mishra , Dr...
Stem Cell Therapy for Anti – Ageing ,DR. SHARDA JAIN, Dr. Prabhu Mishra , Dr...Stem Cell Therapy for Anti – Ageing ,DR. SHARDA JAIN, Dr. Prabhu Mishra , Dr...
Stem Cell Therapy for Anti – Ageing ,DR. SHARDA JAIN, Dr. Prabhu Mishra , Dr...
 
праздник
праздникпраздник
праздник
 
Presentasjon gjestehuset
Presentasjon gjestehusetPresentasjon gjestehuset
Presentasjon gjestehuset
 
Boletín 15-04-15
Boletín 15-04-15Boletín 15-04-15
Boletín 15-04-15
 
найкрасивіші дороги світу
найкрасивіші дороги світунайкрасивіші дороги світу
найкрасивіші дороги світу
 

Similar to Cprogrammingoperator

Similar to Cprogrammingoperator (20)

C operator and expression
C operator and expressionC operator and expression
C operator and expression
 
cprogrammingoperator.ppt
cprogrammingoperator.pptcprogrammingoperator.ppt
cprogrammingoperator.ppt
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
 
Report Group 4 Constants and Variables(TLE)
Report Group 4 Constants and Variables(TLE)Report Group 4 Constants and Variables(TLE)
Report Group 4 Constants and Variables(TLE)
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
C PRESENTATION.pptx
C PRESENTATION.pptxC PRESENTATION.pptx
C PRESENTATION.pptx
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
c programming2.pptx
c programming2.pptxc programming2.pptx
c programming2.pptx
 
Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01
 
C# operators
C# operatorsC# operators
C# operators
 
C operators
C operatorsC operators
C operators
 
MODULE_2_Operators.pptx
MODULE_2_Operators.pptxMODULE_2_Operators.pptx
MODULE_2_Operators.pptx
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
Token and operators
Token and operatorsToken and operators
Token and operators
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
 
Operators
OperatorsOperators
Operators
 

More from teach4uin

Master pages
Master pagesMaster pages
Master pagesteach4uin
 
.Net framework
.Net framework.Net framework
.Net frameworkteach4uin
 
Scripting languages
Scripting languagesScripting languages
Scripting languagesteach4uin
 
State management
State managementState management
State managementteach4uin
 
security configuration
security configurationsecurity configuration
security configurationteach4uin
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tagsteach4uin
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tagsteach4uin
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentationteach4uin
 
.Net overview
.Net overview.Net overview
.Net overviewteach4uin
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lessonteach4uin
 
storage clas
storage classtorage clas
storage clasteach4uin
 

More from teach4uin (20)

Controls
ControlsControls
Controls
 
validation
validationvalidation
validation
 
validation
validationvalidation
validation
 
Master pages
Master pagesMaster pages
Master pages
 
.Net framework
.Net framework.Net framework
.Net framework
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Css1
Css1Css1
Css1
 
Code model
Code modelCode model
Code model
 
Asp db
Asp dbAsp db
Asp db
 
State management
State managementState management
State management
 
security configuration
security configurationsecurity configuration
security configuration
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
 
.Net overview
.Net overview.Net overview
.Net overview
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
enums
enumsenums
enums
 
memory
memorymemory
memory
 
array
arrayarray
array
 
storage clas
storage classtorage clas
storage clas
 

Recently uploaded

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Cprogrammingoperator

  • 1. Hands-on, crash course with code examples 1/46www.tenouk.com, ©
  • 2.  Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations.  Operands are variables or expressions which are used in conjunction with operators to evaluate the expression.  Combination of operands and operators form an expression.  Expressions are sequences of operators, operands, and punctuators that specify a computation.  Evaluation of expressions is based on the operators that the expressions contain and the context in which they are used.  Expression can result in a value and can produce side effects.  A side effect is a change in the state of the execution environment. 2/46www.tenouk.com, ©
  • 3.  An expression is any valid set of literals, variables, operators, operands and expressions that evaluates to a single value.  This value can be a number, a string or a logical value.  For instance a = b + c; denotes an expression in which there are 3 operands a, b, c and two operator + and =.  A statement, the smallest independent computational unit, specifies an action to be performed.  In most cases, statements are executed in sequence.  The number of operands of an operator is called its arity.  Based on arity, operators are classified as nullary (no operands), unary (1 operand), binary (2 3/46www.tenouk.com, ©
  • 4. Operators Description Example Usage Postfix operators () Function call operator. A function call is an expression containing the function name followed by the function call operator, (). If the function has been defined to receive parameters, the values that are to be sent into the function are listed inside the parentheses of the function call operator. The argument list can contain any number of expressions separated by commas. It can also be empty. sumUp(inum1, inum2) displayName() student(cname, iage, caddress) Calculate(length, wide + 7) 4/46www.tenouk.com, ©
  • 5. [] Array subscripting operator. A postfix expression followed by an expression in [ ] (brackets) specifies an element of an array. The expression within the brackets is referred to as a subscript. The first element of an array has the subscript zero. #include <stdio.h> int main(void) { int a[3] = { 11, 12, 33 }; printf("a[0] = %dn", a[0]); return 0; } . Dot operator used to access class, structure, or union members type. objectVar.memberOfStructUnion -> Arrow operator used to access class, structure or union members using a pointer type. aptrTo->memberOfStructUnion 1. Example 1: function call operator 2. Example 2: array subscripting operator 3. Example 3: pointer dot and arrow operator 5/46www.tenouk.com, ©
  • 6. Unary Operators + Unary plus maintains the value of the operand. Any plus sign in front of a constant is not part of the constant. +aNumber - Unary minus operator negates the value of the operand. For example, if num variable has the value 200, -num has the value -200. Any minus sign in front of a constant is not part of the constant. -342 6/46www.tenouk.com, ©
  • 7. You can put the ++/-- before or after the operand. If it appears before the operand, the operand is incremented/decremented. The incremented value is then used in the expression. If you put the ++/-- after the operand, the value of the operand is used in the expression before the operand is incremented/decremented. ++ Post-increment. After the result is obtained, the value of the operand is incremented by 1. aNumber++ -- Post-decrement. After the result is obtained, the value of the operand is decremented by 1. aNumber-- ++ Pre-increment. The operand is incremented by 1 and its new value is the result of the expression. ++yourNumber -- Pre-decrement. The operand is decremented by 1 and its new value is the result of the expression. --yourNumber 7/46www.tenouk.com, ©
  • 8. & The address-of operator (&) gives the address of its operand. The operand of the address-of operator can be either a function designator or an l-value that designates an object that is not a bit field and is not declared with the register storage-class specifier. The result of the address operation is a pointer to the operand. The type addressed by the pointer is the type of the operand. &addressOfData * The indirection operator (*) accesses a value indirectly, through a pointer. The operand must be a pointer value. The result of the operation is the value addressed by the operand; that is, the value at the address to which its operand points. The type of the result is the type that the operand addresses. *aPointerTo 8/46www.tenouk.com, ©
  • 9. sizeof sizeof operator for expressions sizeof 723 sizeof() sizeof operator for types sizeof(char) (type) cast- expression Explicit conversion of the type of an object in a specific situation. This is also call automatic promotion. (float)i Program example: plus, minus, address-of, pre and post increment/decrement, sizeof() and type promotion 9/46www.tenouk.com, ©
  • 10. Multiplicative Operators * The multiplication operator causes its two operands to be multiplied. p = q * r; / The division operator causes the first operand to be divided by the second. If two integer operands are divided and the result is not an integer, it is truncated according to the following rules: 1.The result of division by 0 is undefined according to the ANSI C standard. The Microsoft C compiler generates an error at compile time or run time. 2.If both operands are positive or unsigned, the result is truncated toward 0. 3.If either operand is negative, whether the result of the operation is the largest integer less than or equal to the algebraic quotient or is the smallest integer greater than or equal to the algebraic quotient is implementation defined. a = b / c; % The result of the remainder operator is the remainder when the first operand is divided by the second. When the division is inexact, the result is determined by the following rules: 1.If the right operand is zero, the result is undefined. 2.If both operands are positive or unsigned, the result is positive. 3.If either operand is negative and the result is inexact, the result is implementation defined. x = y % z; 10/46www.tenouk.com, ©
  • 11. Addition and subtraction Operators + addition d = e + f - subtraction r = s – t;  The operands can be integral or floating values. Some additive operations can also be performed on pointer values, as outlined under the discussion of each operator.  The additive operators perform the usual arithmetic conversions on integral and floating operands. The type of the result is the type of the operands after conversion.  Since the conversions performed by the additive operators do not provide for overflow or underflow conditions, information may be lost if the result of an additive operation cannot be represented in the type of the operands after conversion. Program example: assignment, add, subtract, multiply, divide and modulus 11/46www.tenouk.com, ©
  • 12.  For relational expression, 0 is FALSE, 1 is TRUE.  Any numeric value is interpreted as either TRUE or FALSE when it is used in a C / C++ expression or statement that is expecting a logical (true or false) value. The rules are: ◦ A value of 0 represents FALSE. ◦ Any non-zero (including negative numbers) value represents TRUE. 12/46 Program example: true, false and negate www.tenouk.com, ©
  • 13. Relational Inequality Operators The relational operators compare two operands and determine the validity of a relationship. < Specifies whether the value of the left operand is less than the value of the right operand. The type of the result is int and has the value 1 if the specified relationship is true, and 0 if false. i < 7 > Specifies whether the value of the left operand is greater than the value of the right operand. The type of the result is int and has the value 1 if the specified relationship is true, and 0 if false. j > 5 <= Specifies whether the value of the left operand is less than or equal to the value of the right operand. The type of the result is int and has the values 1 if the specified relationship is true, and 0 if false. k <= 4 >= Specifies whether the value of the left operand is greater than or equal to the value of the right operand. The type of the result is int and has the values 1 if the specified relationship is true, and 0 if false. p >= 3 13/46www.tenouk.com, ©
  • 14. Relational Equality Operators The equality operators, like the relational operators, compare two operands for the validity of a relationship. == Indicates whether the value of the left operand is equal to the value of the right operand. The type of the result is int and has the value 1 if the specified relationship is true, and 0 if false. The equality operator (==) should not be confused with the assignment (=) operator. For example: if (x == 4) evaluates to true (or 1) if x is equal to four. while if (x = 4) is taken to be true because (x = 4) evaluates to a nonzero value (4). The expression also assigns the value 4 to x. nChoice == 'Y' != Indicates whether the value of the left operand is not equal to the value of the right operand. The type of the result is int and has the value 1 if the specified relationship is true, and 0 if false. nChoice != 'Y' 14/46www.tenouk.com, ©
  • 15. Expressions Evaluates as (3 == 3) && (4 != 3) True (1) because both operands are true (4 > 2) || (7 < 11) True (1) because (either) one operand/expression is true (3 == 2) && (7 == 7) False (0) because one operand is false ! (4 == 3) True (1) because the expression is false NOT(FALSE) = TRUE NOT(TRUE) = FALSE 15/46 Program example: less-than, greater-than, less-than and equal-to, greater-than and www.tenouk.com, ©
  • 16. Logical NOT Operator (unary) ! Logical not operator. Produces value 0 if its operand or expression is true (nonzero) and the value 1 if its operand or expression is false (0). The result has an int type. The operand must be an integral, floating, or pointer value. !(4 == 2) !x Operand (or expression) Output !0 1 ( T ) !1 0 ( F ) 16/46www.tenouk.com, ©
  • 17. Logical AND Operator && Indicates whether both operands are true. If both operands have nonzero values, the result has the value 1. Otherwise, the result has the value 0. The type of the result is int. Both operands must have an arithmetic or pointer type. The usual arithmetic conversions on each operand are performed. The logical AND (&&) should not be confused with the bitwise AND (&) operator. For example: 1 && 4 evaluates to 1 (True && True = True) while 1 & 4 (0001 & 0100 = 0000) evaluates to 0 x && y Operand1 Operand2 Output 0 0 0 ( F ) 0 1 0 ( F ) 1 0 0 ( F ) 1 1 1 ( T ) 17/46www.tenouk.com, ©
  • 18. Logical OR Operator || Indicates whether either operand is true. If either of the operands has a nonzero value, the result has the value 1. Otherwise, the result has the value 0. The type of the result is int. Both operands must have a arithmetic or pointer type. The usual arithmetic conversions on each operand are performed. The logical OR (||) should not be confused with the bitwise OR (|) operator. For example: 1 || 4 evaluates to 1 (or True || True = True) while 1 | 4 (0001 | 0100 = 0101) evaluates to 5 x || y Operand1 Operand2 Output 0 0 0 ( F ) 0 1 1 ( T ) 1 0 1 ( T ) 1 1 1 ( T ) Program example: Logical-AND, logical-OR 18/46www.tenouk.com, ©
  • 19. Conditional Operator (ternary) ?: The first operand/expression is evaluated, and its value determines whether the second or third operand/expression is evaluated: 1.If the value is true, the second operand/expression is evaluated. 2.If the value is false, the third operand/expression is evaluated. The result is the value of the second or third operand/expression. The syntax is: First operand ? second operand : third operand size != 0 ? size : 0 Program example: tenary conditional operator 19/46www.tenouk.com, ©
  • 20.  The compound assignment operators consist of a binary operator and the simple assignment operator.  They perform the operation of the binary operator on both operands and store the result of that operation into the left operand.  The following table lists the simple and compound assignment operators and expression examples: 20/46www.tenouk.com, ©
  • 21. Simple Assignment Operator =  The simple assignment operator has the following form: lvalue = expr  The operator stores the value of the right operand expr in the object designated by the left operand lvalue.  The left operand must be a modifiable lvalue.  The type of an assignment operation is the type of the left operand. i = 5 + x; 21/46www.tenouk.com, ©
  • 22. Compound Assignment Operator Example Equivalent expression identifier operator= entity represents identifier = identifier operator entity += nindex += 3 index = nindex + 3 -= *(paPter++) -= 1 * paPter = *( paPter ++) - 1 *= fbonus *= fpercent fbonus = fbonus * fpercent /= ftimePeriod /= fhours ftimePeriod = ftimePeriod / fhours %= fallowance %= 80 fallowance = fallowance % 80 <<= iresult <<= inum iresult = iresult << inum >>= byleftForm >>= 1 byleftForm = byleftForm >> 1 &= bybitMask &= 2 bybitMask = bybitMask & 2 ^= itestSet ^= imainTest itestSet = itestSet ^ imainTest |= bflag |= bonBit bflag = bflag | bonBit Program example: compound operators 22/46www.tenouk.com, ©
  • 23. Comma Operator , A comma expression contains two operands of any type separated by a comma and has left-to-right associativity. The left operand is fully evaluated, possibly producing side effects, and its value, if there is one, is discarded. The right operand is then evaluated. The type and value of the result of a comma expression are those of its right operand, after the usual unary conversions. In some contexts where the comma character is used, parentheses are required to avoid ambiguity. The primary use of the comma operator is to produce side effects in the following situations: 1.Calling a function. 2.Entering or repeating an iteration loop. 3.Testing a condition. 4.Other situations where a side effect is required but the result of the expression is not immediately needed. The use of the comma token as an operator is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a separator. Because the comma operator discards its first operand, it is generally only useful where the first operand has desirable side effects, such as in the initializer or the counting expression of a for loop. 23/46www.tenouk.com, ©
  • 24.  The following table gives some examples of the uses of the comma operator. Statement Effects for (i=0; i<4; ++i, func()); A for statement in which i is incremented and func() is called at each iteration. if (func(), ++i, i>1 ) { /* ... */ } An if statement in which function func() is called, variable i is incremented, and variable i is tested against a value. The first two expressions within this comma expression are evaluated before the expression i>1. Regardless of the results of the first two expressions, the third is evaluated and its result determines whether the if statement is processed. func1((++iaArg, func2(iaArg))); A function call to func1() in which iaArg is incremented, the resulting value is passed to a function func2(), and the return value of func2() is passed to func1(). The function func1() is passed only a single argument, because the comma expression is enclosed in parentheses within the function argument list. int inum=3, inum2=7, inum3 = 2; Comma acts as separator, not as an operator. Program example: comma operator 24/46www.tenouk.com, ©
  • 25. Bitwise (complement) NOT Operators ~ The ~ (bitwise negation) operator yields the bitwise (one) complement of the operand. In the binary representation of the result, every bit has the opposite value of the same bit in the binary representation of the operand. The operand must have an integral type. The result has the same type as the operand but is not an lvalue (left value). The symbol used called tilde. Suppose byNum variable represents the decimal value 8. The 16-bit binary representation of byNum is: 00000000 00001000 The expression ~byNum yields the following result (represented here as a 16-bit binary number): 11111111 11110111 Note that the ~ character can be represented by the trigraph ??-. The 16-bit binary representation of ~0 (which is ~00000000 00000000) is: 11111111 11111111 25/46www.tenouk.com, ©
  • 26. Bitwise Shift Operators << Left shift operator, shift their first operand left (<<) by the number of positions specified by the second operand. nbits << nshiftSize >> Right shift operator, shift their first operand right (>>) by the number of positions specified by the second operand. nbits >> nshiftSize  Both operands must be integral values. These operators perform the usual arithmetic conversions; the type of the result is the type of the left operand after conversion.  For leftward shifts, the vacated right bits are set to 0. For rightward shifts, the vacated left bits are filled based on the type of the first operand after conversion. If the type is unsigned, they are set to 0. Otherwise, they are filled with copies of the sign bit. For left-shift operators without overflow, the statement: expression1 << expression2  is equivalent to multiplication by 2expression2 . For right-shift operators: expression1 >> expression2  is equivalent to division by 2expression2 if expression1 is unsigned or has a nonnegative value.  The result of a shift operation is undefined if the second operand is negative, or if the right operand is greater than or equal to the width in bits of the promoted left operand.  Since the conversions performed by the shift operators do not provide for overflow or underflow conditions, information may be lost if the result of a shift operation cannot be represented in the type of the first operand after conversion. 26/46www.tenouk.com, ©
  • 27. Program example: bitwise shift left, bitwise shift right and bitwise-NOT operators 27/46www.tenouk.com, ©
  • 28. Bitwise AND Operator &  The & (bitwise AND) operator compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's, the corresponding bit of the result is set to 1. Otherwise, it sets the corresponding result bit to 0.  Both operands must have an integral or enumeration type. The usual arithmetic conversions on each operand are performed. The result has the same type as the converted operands.  Because the bitwise AND operator has both associative and commutative properties, the compiler can rearrange the operands in an expression that contains more than one bitwise AND operator.  The bitwise AND (&) should not be confused with the logical AND. (&&) operator. For example: 1 & 4 evaluates to 0 (0001 & 0100 = 0000) while 1 && 4 evaluates to true [True && True = True] 28/46www.tenouk.com, ©
  • 30. Bitwise XOR Operator ^  The bitwise exclusive OR operator (in EBCDIC, the ^ symbol is represented by the ¬ symbol) compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's or both bits are 0's, the corresponding bit of the result is set to 0. Otherwise, it sets the corresponding result bit to 1.  Both operands must have an integral or enumeration type. The usual arithmetic conversions on each operand are performed. The result has the same type as the converted operands and is not an lvalue (left value).  Because the bitwise exclusive OR operator has both associative and commutative properties, the compiler can rearrange the operands in an expression that contains more than one bitwise exclusive OR operator. Note that the ^ character can be represented by the trigraph ??'. The symbol used called caret. 30/46www.tenouk.com, ©
  • 32. Bitwise (Inclusive) OR Operator |  The | (bitwise inclusive OR) operator compares the values (in binary format) of each operand and yields a value whose bit pattern shows which bits in either of the operands has the value 1. If both of the bits are 0, the result of that bit is 0; otherwise, the result is 1.  Both operands must have an integral or enumeration type. The usual arithmetic conversions on each operand are performed. The result has the same type as the converted operands and is not an lvalue.  Because the bitwise inclusive OR operator has both associative and commutative properties, the compiler can rearrange the operands in an expression that contains more than one bitwise inclusive OR operator. Note that the | character can be represented by the trigraph ??!.  The bitwise OR (|) should not be confused with the logical OR (||) operator. For example: 1 | 4 evaluates to 5 (0001 | 0100 = 0101) while 1 || 4 (True || True = True) evaluates to true 32/46www.tenouk.com, ©
  • 33. Program example: bitwise-AND, bitwise-OR and eXclusive-OR (XOR) operators 33/46www.tenouk.com, ©
  • 34.  Consider the following arithmetic operation: - left to right 6 / 2 * 1 + 2 = 5 - right to left 6/2 * 1 + 2 = 1 - using parentheses = 6 / (2 * 1) + 2 = (6 / 2) + 2 = 3 + 2 = 5 34/46 Inconsistent answers! www.tenouk.com, ©
  • 35.  Operator precedence: a rule used to clarify unambiguously which operations (operator and operands) should be performed first in the given (mathematical) expression.  Use precedence levels that conform to the order commonly used in mathematics.  However, parentheses take the highest precedence and operation performed from the innermost to the outermost parentheses. 35/46www.tenouk.com, ©
  • 36.  Precedence and associativity of C operators affect the grouping and evaluation of operands in expressions.  Is meaningful only if other operators with higher or lower precedence are present.  Expressions with higher-precedence operators are evaluated first. 36/46www.tenouk.com, ©
  • 37. Precedence and Associativity of C Operators Symbol Type of Operation Associativity [ ] ( ) . –> postfix ++ and postfix –– Expression Left to right prefix ++ and prefix –– sizeof & * + – ~ ! Unary Right to left typecasts Unary Right to left * / % Multiplicative Left to right + – Additive Left to right << >> Bitwise shift Left to right < > <= >= Relational Left to right == != Equality Left to right & Bitwise-AND Left to right ^ Bitwise-exclusive-OR Left to right | Bitwise-inclusive-OR Left to right && Logical-AND Left to right || Logical-OR Left to right ? : Conditional-expression Right to left = *= /= %= += –= <<= >>= &= ^= |= Simple and compound assignment Right to left , Sequential evaluation Left to right 37/46www.tenouk.com, ©
  • 38.  The precedence and associativity (the order in which the operands are evaluated) of C operators.  In the order of precedence from highest to lowest.  If several operators appear together, they have equal precedence and are evaluated according to their associativity.  All simple and compound-assignment operators have equal precedence. 38/46www.tenouk.com, ©
  • 39.  Operators with equal precedence such as + and -, evaluation proceeds according to the associativity of the operator, either from right to left or from left to right.  The direction of evaluation does not affect the results of expressions that include more than one multiplication (*), addition (+), or binary-bitwise (& | ^) operator at the same level. 39/46www.tenouk.com, ©
  • 40.  e.g:  Order of operations is not defined by the language.  The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result. 40/46 3 + 5 + (3 + 2) = 13 – right to left (3 + 5) + 3 + 2 = 13 – left to right 3 + (5 + 3) + 2 = 13 – from middle www.tenouk.com, ©
  • 41.  Only the sequential-evaluation (,), logical-AND (&&), logical-OR (||), conditional-expression (? :), and function-call operators constitute sequence points and therefore guarantee a particular order of evaluation for their operands.  The sequential-evaluation operator (,) is guaranteed to evaluate its operands from left to right.  The comma operator in a function call is not the same as the sequential-evaluation operator and does not provide any such guarantee. 41/46www.tenouk.com, ©
  • 42.  Logical operators also guarantee evaluation of their operands from left to right.  But, they evaluate the smallest number of operands needed to determine the result of the expression.  This is called "short-circuit" evaluation.  Thus, some operands of the expression may not be evaluated. 42/46www.tenouk.com, ©
  • 43.  For example:  The second operand, y++, is evaluated only if x is true (nonzero).  Thus, y is not incremented if x is false (0). 43/46 x && y++ www.tenouk.com, ©
  • 44.  Label the execution order for the following expressions 44/46www.tenouk.com, ©
  • 45. a.(rate*rate) + delta b.2*(salary + bonus) c. 1/(time + (3*mass)) d.(a - 7) / (t + (9 * v)) 45/46  Convert the following operations to C expression www.tenouk.com, ©