Vous êtes sur la page 1sur 25

Polytechnic University of Tirana

Faculty of Information Technology

Leksioni 3

Operators in C - language

Bojken Shehu, Dr
Polytechnic University of Tirana
Faculty of Information Technology
email: bojken.shehu@gmail.com

2017
Expressions

An expression is anything that evaluates to a numeric values

Simple expression : The simplest


Complex expression: The Complex
C expressions consists of a single
Expression consist of simpler
item:
expressions connected by operators.
a simple variable [Ex: rate].
Example:2+8
literal constant [Ex: 20 or -1.25].
Example:1.25/8+5*rate+rate*rate/const
symbolic constant [Ex: PI].

An expression is simply a combination of few operators and operands

Dr. Bojken Shehu C Programming language


Operators

An operator is a special sign or symbol that instructs the compiler to do a


certain task. An operand is something than an operator acts on.

a = 5 + 4;
+ and - signs work as the operators
a = 5 4;

Dr. Bojken Shehu C Programming language


Operators

Types of operators:

Assignment operators.

Arithmetic operators.

Modify operators or Unary operators (Increment and Decrement).

Relational and Logical Operators.

Bitwise operators.

Advanced operators.

Dr. Bojken Shehu C Programming language


Assignments Operators
Example:
a
void main () 4
{
int a, b; b
a = b = 4; 4

printf(%d, a);
} 4 assign to a

Output 4

Dr. Bojken Shehu C Programming language


Arithmetic Operators

Arithmetic operators are: +, -, *, / , %, ++, --.

Unary : ++ Increment - -Decrement.


Binary : +, -, *, /, %.

Operator Precedence:
Operators Precedence
++ increment, -- decrement 1
*, /, % 2
+, - 3

Dr. Bojken Shehu C Programming language


Arithmetic Operators
main () main () main ()
{ { {
Int a, b, c; Int a, b, c; float a, b, c;
a = 10; a = 10; a = 10;
b = 30; b = 30; 0.333333 b = 30;
c = a + b; c = a / b; c = a / b;
printf (%d,c); printf (%d,c); printf (%f,c);
} } }
Output 40 Output 0 Output 0.333333
main () main () main ()
{ { {
Int a, b; Int a, b; Int a, b;
float c; float c; float c; remainder
int/int=int float/int=float
a = 5; a = 5; a = 5;
b = 2; b = 2; b = 2;
c = a / b; c = 1.0 * a / b; c = a % b;
printf (%f,c); printf (%f,c); printf (%f,c);
} 2.0 } 2.5 } 1.0

Dr. Bojken Shehu C Programming language


How to works with modify operators [unary
operators] ?
Modify operators also called unary operators. Unary means that only one
operator is enough to perform operation.

Modify operators or Unary operators

increment decrement

pre-increment post-increment pre-decrement post-decrement

++variable variable++ --variable variable--

Int x = 10; Int x = 10;


++x/x++ --x/x--
x=11 [x=x+1] x=9 [x=x-1]

Dr. Bojken Shehu C Programming language


What is the use of increment and decrement
operators, so what exactly that operators will do ?
Increment operators means Decrement operators means
increases the value of variable by decreases the value of variable by
one. one.

When Pre-increment and Post- When Pre-decrement and Post-


increment increases the value of decrement decreases the value of
variable by one? variable by one?

To understand very clearly only FIVE STEPS we should understand first

Dr. Bojken Shehu C Programming language


Pre-increment, post-increment and pre-
decrement, post-decrement.
Pre-increment
main ()
{ Pre-increment
x Int x = 10, y; Pre-decrement
10 11 11
y=++x;
y printf (%d,%d,x, y); Substation
11 }
Output 11 11
Evaluation
Post-increment
main ()
x {
Assignment
10 11 Int x = 10, y;
10
y y=x++;
Post-increment
10 printf (%d,%d,x, y);
Post-decrement
}
Output 11 10

Dr. Bojken Shehu C Programming language


Pre-increment, post-increment and pre-
decrement, post-decrement.
Example : Pre-increment
y Pre-decrement
20 19
main ()
x { Substation
19 190
10 Int x = 10, y =20, z;
19 * 10
z z=x++ * --y; Evaluation
printf (%d,%d, %d, x, y, z);
190
}
Assignment
Output 11 19 190
Post-increment
Post-decrement

Dr. Bojken Shehu C Programming language


Pre-increment, post-increment and pre-
decrement, post-decrement.
Example : Pre-increment
Pre-decrement

main () 13
x { 12 Substation
6
5 6 12 13 Int x = 5;
6+ 6
x=x++ + ++x; Evaluation
printf (%d, x);
}
Assignment
Output 13
Post-increment
Post-decrement

Dr. Bojken Shehu C Programming language


Pre-increment, post-increment and pre-
decrement, post-decrement.
Example : 12 Pre-increment
8 4 Pre-decrement
main () 7 5
8 4
{ 3 5
b Int a = 2, b=3; Substation
2 +3
3 5 4 5 4 12
b = a++ + b--;
a 3 +5 Evaluation
a = a-- + ++b;
2 3 8 7 8
8 +4
b = ++a + --b; Assignment
printf (%d, %d, a,b);
}
Outout 8 12 Post-increment
Post-decrement

Dr. Bojken Shehu C Programming language


Pre-increment, post-increment and pre-
decrement, post-decrement.
Example : Pre-increment
main() a = a+++a Pre-decrement
{
Int a = 5;
a= a+++a; Substation
a = a + ++a a = a++ + a
printf(%d, a)
Evaluation
a=6+6 a=5+5
a = 12 a = 10
a = 12 a = 11
Assignment

Post-increment
Priority: Modify operators Post-decrement

Dr. Bojken Shehu C Programming language


What is the output?.

Example 1 : Example 2 :
void main()
void main() Pre-increment
{
{ Pre-decrement
Int x = 5;
Int a = 5, b = 3;
x=++2;
printf(%d, ++(a*b+1));
printf(%d, x); Substation
}
}

Output : Evaluation
Compile-time Error

Assignment
The compiler display an error message because
for all increment and decrement operators, the
operand can not be a expression or a constant, it Post-increment
will be always only a Variable Post-decrement

Dr. Bojken Shehu C Programming language


Operator Precedence
Operators Precedence
++ increment, -- decrement 1
*, /, % 2
+, - 3

Brackets has the highest priority

main () main ()
{ {
Int x1, x2, x3, x4; Int x1, x2, x3, x4;
x1 = 5; x1 = 5;
x2 = 6; x2 = 6;
x3 = 7; x3 = 7;
x4 = x1 + x2 * x3; x4 = (x1 + x2) * x3;
printf (%d,x4); printf (%d,x4);
} 47 } 77

Dr. Bojken Shehu C Programming language


Relational operators
Relational Operators: ==, <, >, <=, >=, !=.
Expression How it reads What it evaluates to
5 == 1 Is 5 equal to 1? 0 (false)
5>1 Is 5 greater than 1? 1 (true)
5 != 1 Is 5 not equal to 1? 1 (true)
(5 + 10)==(3 * 5) Is (5+10) equal to (3*5) 1 (true)

Relational Operators Precedence


Operators Precedence
<, <=, >, >=. 1
!=, == 2

Dr. Bojken Shehu C Programming language


Relational operators
Example 1: Example 2: Example 3:
main ()
main () main ()
{
{ {
Int x1, x2, x3, x4;
Int x1, x2, x3, x4; Int x1, x2, x3, x4;
x1 = 5;
x1 = 5; x1 = 5;
x2 = 6;
x2 = 6; 0 (false) x2 = 5; 1 (true) x3 = 7;
x3 = 7; x3 = 7;
x4 = x1 = x2;
x4 = x1 == x2; x4 = x1 == x2;
6 6
printf (%d,x4); printf (%d,x4);
printf (%d,x4);
} }
0 1 } 6

Dr. Bojken Shehu C Programming language


Relational operators
Example 1:
void main ()
Relational Operators
{
x = 3 > 2 > 1;
Int x;
Left to right
x = 3 >2 > 1;
printf (%d,x); x =1 > 1
} 0 x=0 1 (true) 0 (false)

Example 2:
void main () Assignment operator
{
Int a, b, c; Relational operator
a = b = 4;
Is equal to ==
c = a == b;
c = a == b
c = 4 == 4 (true 1)
printf (%d,c);
1 c=1
}

Dr. Bojken Shehu C Programming language


Logical Operators
Logical Operators: &&,||,!.
Expression What it evaluates to
(exp1 && exp2) True (1) only if both exp1 and exp2 are true; false (0)
otherwise
(exp1 || exp2) True (1) if either exp1 or exp2 is true; false (0) only if both
are false.
(!exp1) False (0) if exp1 is true; true (1) if exp1 is false.

Truth table:
exp1 exp2 &&(And) ||(Or) !(Not)
True True True True False
False True False True True
True False False True False
False False False False True

Dr. Bojken Shehu C Programming language


Logical operators (&& operator AND)
Example 1: Example 1:

int main () Both are true(1) int main () Not both


{ { are true
Int x1, x2, x3, x4; Int x1, x2, x3, x4;
x1 = 5; x1 = 5;
x2 = 5; x2 = 5;
x3 = 7; x3 = 7;
x4 = (x1 == x2) && (x2 == 5); x4 = (x1 == x2) && (x2 == 6);
printf (%d,x4); printf (%d,x4);
} }
Output: 1 Output: 0

Dr. Bojken Shehu C Programming language


Logical operators (|| operator OR)
Example 1: Example 1:

int main () False or false int main ()


{ {
Int x1, x2, x3, x4; Int x1, x2, x3, x4;
x1 = 5; x1 = 5;
x2 = 7; x2 = 5;
x3 = 7; x3 = 7;
x4 = (x1 == x2) || (x2 == 6); x4 = (x1 == x2) || (x2 == 6);
printf (%d,x4); printf (%d,x4);
} }
Output: 0 Output: 1

Dr. Bojken Shehu C Programming language


Logical operators (! operator NOT)
Example 1:

int main ()
a = ! a > 14
{
a = !10 > 14
Int a = 10;
a = 0 > 14
a = ! a > 14;
a=0
printf (%d,a);
}
Output: 0

Logical operators highest priority than relational operators

Dr. Bojken Shehu C Programming language


Bitwise operators.

Bitwise operators: & (and), | (or), ^ (xor), ~ (negation), << (right), >>(left)

Floating type values (float and double) cannot use bitwise operators

Advanced Operators: &, *, sizeof(), ?:

Dr. Bojken Shehu C Programming language


Bojken Shehu, Dr
Polytechnic University of Tirana
Faculty of Information Technology

email: bojken.shehu@gmail.com

Dr. Bojken Shehu C Programming language

Vous aimerez peut-être aussi