Fundamentals of Software Development
Lecture 4
Brahim El Boudani
Lecture 3 Recap
• Constants and literals
• Constant key words
• Constants types
Today’s Lecture
• C Basic Syntax
• Operators and operator precedence
Operators
• An operator is a symbol that instructs the compiler which logical or
mathematical function to use.
• C language is rich in built-in operators and provides the following types of
operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Assignment &
Arithmetic Operators
Assignment Operator
• All expressions must evaluate to a value, including those with the assignment
operator.
• For assignment operator, the end result is the value assigned. usually (but not
always) the value of the right-hand side.
• Type conversion might make assigned value different than computed value (
ex. float vs int)
• Assignment associates a value on right to an identifier on left : y = x = 5;
• Here, y gets the value 5 because x was given this value first.
Assignment Operator
Arithmetic Operators
Operator Description Example
• These type operators are used to + Adds two operands. A + B = 30
conduct arithmetic
− Subtracts second operand from the A − B = -10
operations/calculations on one or first.
more operands.
• The table on the right shows all the * Multiplies both operands. A * B = 200
arithmetic operators used in the C / Divides numerator by de-numerator. B / A =2
language.
• Assume variable A holds 10 and % Modulus Operator and remainder of B % A =0
variable B holds 20. after an integer division.
• Warning: integer division truncates.
++ Increment operator increases the ++A = 11
( ex. 9/10 =0 and 17/5=3) integer value by one.
-- Decrement operator decreases the --A = 9
integer value by one.
Arithmetic Operators ex:
Relational and Logical Operators
Relational Operators
• The binary relational and equality operators compare their first operand to their
second operand to test the validity of the specified relationship.
• The result type is integer (int) (0 if false and 1 if true)
• The table below shows all the relational operators used in the C language.
Assume variable A holds 10 and variable B holds 20 then:
Logical Operators
• The logical operator do not perform any arithmetic operation.
• Each operand is evaluated in terms of its equivalence to zero.
• Following table shows all the logical operators supported by C language.
Assume variable A holds 1 and variable B holds 0, then:
Logical Operators example
Bitwise Operators
Bitwise Operators
• Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, |, and ^ is as follows:
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Bitwise Operators
• PLEASE! Do not confuse bitwise operators with logical operators
• C provides 6 operators for bit manipulation. The following table lists the bitwise
operators supported by C.
• Assume variable 'A' holds 60 and variable 'B' holds 13
Bitwise Operators
•Assume A = 60 and B = 13 in binary format as follows
A = 0011 1100 and B = 0000 1101
• A&B = 0000 1100 : A and B must equal 1
• A|B = 0011 1101: A or B must equal 1
• A^B = 0011 0001 : Exclusive A or B
• ~A = 1100 0011 : Not (A and B)
Bitwise Operators: Example in C
Bitwise Operators shift: Example in C
Special & Misc. Operators
Special Operators
(shorthand): +=, *=, etc
Special Operators (Shorthand) (contd.)
<<= Left shift AND assignment operator. C <<= 2 is same as C
= C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C
= C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C =
C&2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C =
C^2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C =
C|2
Misc. Operators
Operator Description Example
Sizeof() Returns the size of a variable. sizeof(x), where x is integer, will
return 4.
& Returns the address of a variable &a; returns the actual address of
the variable
* Pointer to a variable *a;
? Conditional Expression If Condition is true ? then value X
: otherwise value Y
Operators:
Precedence &
Associativity
Operators Precedence: Order and
Associativity
• Operator precedence determines the grouping of terms in an expression and
decides how an expression is evaluated. Certain operators have higher
precedence than others. (the multiplication operator has a higher
precedence than the addition or subtraction operator)
• Precedence Order: If two operators share the same operand the operator
with the higher precedence goes first. For instance, 1 + 2 * 3 is treated as 1 +
(2 * 3) because multiplication has a higher precedence than addition
• Associativity: If an expression has two operators of the same precedence
rank, the expression is evaluated according to its associativity criterion. For
instance, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right
associativity.
Operators Precedence: Order matters!
• Some operators have higher precedence than other operators, meaning that
they are performed first
• Given this, what is the value of the expression 3 + 5 / 2 ?
• Assignment statements have the lowest precedence of all, so all other
operations are performed before the result is assigned.
• Parentheses always have the highest precedence, and can be used to
override the natural precedence of other operators.
• In the expression 3 * ( 4 + 2 ), the addition is performed first and then the
multiplication.
Operators Associativity: Left or Right?
• For operators of equal precedence, most operators are evaluated from left
to right.
• Ex: What is the value of the expression 5 / 3 * 2.0 ?
• There are a few exceptions where the evaluation is from right to left.
• For instance, in the following assignment operation, zero is first assigned to C
and then the result of that operation ( zero ) is assigned to B by the middle
operator, and then finally that result ( 0 ) is assigned to A:
A = B = C = 0;
Operators Precedence
& Associativity Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & Right to left
sizeof
• Here, operators with the highest Multiplicative */% Left to right
precedence appear at the top of the
Additive +- Left to right
table, those with the lowest appear at the
bottom. Shift << >> Left to right
• Within an expression, higher precedence
operators will be evaluated first. Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= Right to left
<<= &= ^= |=
Comma , Left to right
Trivial example: check leap year
Trivial example: check leap
Lecture summary
• C offers various sets of operators for arithmetic, logic,
relationship, bits and many more.
• Some operators have higher precedence than other
operators, meaning that they are performed first.
• Arithmetic operators: *, /, %, -,+, ++,--
• Relational Operators: ==,!=,>=,<=
• Logical Operators: &&,||, !
• Bitwise Operators: &,|,^,~
• Assignment Operators: =, *=, +=,<<=,etc..
• Misc. Operators: sizeof(), ?, & (always with variable), *
Next Lecture:
Decision making and Loops