Académique Documents
Professionnel Documents
Culture Documents
Leksioni 3
Operators in C - language
Bojken Shehu, Dr
Polytechnic University of Tirana
Faculty of Information Technology
email: bojken.shehu@gmail.com
2017
Expressions
a = 5 + 4;
+ and - signs work as the operators
a = 5 4;
Types of operators:
Assignment operators.
Arithmetic operators.
Bitwise operators.
Advanced operators.
printf(%d, a);
} 4 assign to a
Output 4
Operator Precedence:
Operators Precedence
++ increment, -- decrement 1
*, /, % 2
+, - 3
increment 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
Post-increment
Priority: Modify operators Post-decrement
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
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
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
}
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
int main ()
a = ! a > 14
{
a = !10 > 14
Int a = 10;
a = 0 > 14
a = ! a > 14;
a=0
printf (%d,a);
}
Output: 0
Bitwise operators: & (and), | (or), ^ (xor), ~ (negation), << (right), >>(left)
Floating type values (float and double) cannot use bitwise operators
email: bojken.shehu@gmail.com