Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Operators

Operators

Content
◼ Arithmetic operations
◼ cast operations
◼ Logical operators
◼ Relational operators

STRUCTURED PROGRAMMING 2
Operators

Arithmetic operations - examples


int main() {
int whole; /* whole number */
float real; /* real value */
real = 1.0 / 2.0; /* assign real (float) 0.5 */
whole = 1 / 3; /* assign int 0 */
real = (1 / 2) + (1 / 2); /* assign float 0.0 */
real = 3.0 / 2.0; /* assign float 1.5 */
whole = real; /* assign int 1 */
whole = whole +1;
return (0);
}

STRUCTURED PROGRAMMING 7
Operators

Solve now!
Write a program that converts Celsius into Fahrenheit
degrees. Celsius degrees should be entered from the
keyboard, and the conversion formula is:
f = 1.8C + 32
#include <stdio.h>
int main ( ) {
float fahrenheit, celsius;
scanf("%f", &celsius);
fahrenhеit = 1.8*celsius + 32.0;
printf ("fahrenheit = %f\n", fahrenheit);
return 0;
} /* end-main() */

STRUCTURED PROGRAMMING 9
Operators

Content
◼ Arithmetic operations
◼ cast operator – changing type
◼ Logical operators
◼ Relational operators

STRUCTURED PROGRAMMING 10
Operators

cast operator - changing type / type casting


◼ format
(data type) value
◼ Example:
int i;
double d = 6.28;
i = (int) d;
◼ Example:
(int) 3.56 3.56 into 3
(long) 6.28 6.28 into 6L
(double) 2 2 into 2.0
(char) 70 70 into char with code 70 ('F')
(unsigned short) 3.14 3.14 into 3 (unsigned short)
STRUCTURED PROGRAMMING 11
FACULTY OF Operators
COMPUTER SCIENCE
AND ENGINEERING
o Illustrating direct cast o Illustrating direct cast

int main() {
#include <stdio.h> int i=5, j=4;
double f;
int main() { f = (double) i/j; // f=1.25
int a = 50; f = i / (double) j;// f=1.25
float b = 20.2; f = (double) i / (double) j;
// f=1.25
char c; f = (double) (i/j);// f=1.0
c = (char)a + (char)b; return 0;
printf("c = %c \n",c); }

return 0; double d; float f;


} long l; int i;
i = l = f = d = 10/3;
d = f = l = i = 10/3;
o The program will output i = l = f = d = 10/3.;
c = F d = f = l = i = (double) 10/3;
d = i = l = f = 10/3.0;

GRAMMING 12
Operators

Assignment operator (=)


◼ The assignment operator returns a value that can
be assigned to another variable.
◼ The assignment operator is flexible:
int i, j, k, l, m, n;
i = j = k = l = m = n = 22;
- which variable will be the first to be assigned a value?? n
 n=22 is the first operation that is executed, and that makes
the value 22 available to the next assignment operation of
assigning 22 to m, etc.

printf("%d\n", j=93);
a=(b=c)+(d=e+2);

STRUCTURED PROGRAMMING 13
Operators

Compound assignment operators


Every expression of the type Variable = Variable op Value;
Can be written in the following form Variable =op Value;

operator expression equivalent expression

+= x+=2; x=x+2;
-= x-=2; x=x-2;
*= x*=2; x=x*2;
x*=a+b; x=x*(a+b);
/= x/=2; x=x/2;
x/=j+2; x=x/(j+2);
%= x%=2; x=x%2;
STRUCTURED PROGRAMMING 14
Operators

Example 1
◼ Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
compound expression expanded assignment
assignment explanation
operator
+= c += 7 c=c+7 10 to с

-= d -= 4 d=d-4 1 to d

*= e *= 5 e=e*5 20 to е

/= f /= 3 f=f/3 2 to f

%= g %= 9 g=g%9 3 to g

STRUCTURED PROGRAMMING 15
Operators

#include<stdio.h>
int main( ) {
int a,b,c,d;
printf("Enter values for a,b,c,d\n");
scanf("%d%d%d%d",&a, &b, &c, &d);
a += b*c+d;
printf("\n a = %d",a);
}
For the following values
a = 5, b= 5, c = 7, d = 8, the
output of the program will
be:
Enter values for a,b,c,d
5
What is the 5
value of а? 7
8
a = 48

STRUCTURED PROGRAMMING 16
Operators

Operators ++ and --
◼ i=i+1 can also be written as
++i or i++
◼ i=i-1 can also be written as o Example:
--i or i-- Given i=1 and
◼ Example:
the following code segment
If i=1 the following code segment
printf (" i = %d\n", i); printf (" i = %d\n", i);
printf (" i = %d\n", ++i); printf (" i = %d\n", i++);
printf (" i = %d\n", i); printf (" i = %d\n", i )

◼ will output: o the output will be:


i = 1 i = 1
i = 2 i = 1
i = 2 i = 2

STRUCTURED PROGRAMMING 17
Operators

Increment ++ and Decrement --


Operator as Prefix and Postfix

expression explanation
++var The value of var is incremented by 1 first, then, it returns
the value

var++ The original value of var is returned first, then, var is


incremented by 1.

--var The value of var is decremented by 1 first, then, it


returns the value

var-- The original value of var is returned first, then, var is


decremented by 1.

STRUCTURED PROGRAMMING 20
Operators

Prefix and postfix versions of ++


(example)
#include <stdio.h>
int main() {
int i, j=5;
i = ++j; /* same as j++; i=j;*/
printf("i=%d, j=%d\n", i, j);
j=5;
i=j++; /* same as i=j; j++;*/
printf("i=%d, j=%d\n", i, j);
return 0; The output will be:
} i=6 j=6
i=5 j=6
STRUCTURED PROGRAMMING 21
Operators

Content
◼ Arithmetic operations
◼ cast operations
◼ Relational operators
◼ Logical operators

STRUCTURED PROGRAMMING 23
Operators

Relational operators

Operators Sintax Example Meaning

Relational operators

> > x>y x is greater than y


< < x<y x is lesser than y
>= x >= y x is greater or equal to y
<= x <= y x is lesser or equal to y
Equality operators
=/= == x == y x is equal to y
!= x != y x is unequal to y

STRUCTURED PROGRAMMING 24
Operators

Logic type rules


◼ There IS NO Boolean type in C
◼ Every nonzero value is considered TRUE
◼ The value of 0 is considered FALSE (0.0, -0,
+0)
◼ The result of any logical or relational
operation is always either 0 or 1
 1 if the condition is satisfied, or
 0 if the condition is not satisfied
STRUCTURED PROGRAMMING 25
Operators

Relational operations (2)


Exercise:
◼ Examples Let a=2, b=3, c=6
 (7 == 5) returns 0 (false)
 (5 > 4) returns 1 (true) (a == 5) returns:
0 (false)
 (3 != 2) returns 1 (true)
 (6 >= 6) returns 1 (true) (a*b >= c) returns:
 (5 < 5) returns 0 (false) 1 (true)

(b+4 > a*c) returns:


0 (false)

((b=2) == a) returns:
1 (true)
STRUCTURE D PROGRAMMING 26
Operators

Content
◼ Arithmetic operations
◼ cast operations
◼ Relational operators
◼ Logical operators

STRUCTURED PROGRAMMING 32
Operators

Logical operators (1)


◼ Logical AND - &&
 The value of the expression will be true (1) if and only if both
operands are true
◼ Logical OR - ||
 The value of the expression will be true (1) if at least one operand is
true
◼ Logical NO - !
 The value of the expression will be true (1) if the operand is false
◼ Exclusive OR - XOR - ^
 The value of the expression will be true (1) if and only if one of the
operands is false while the other one is true
Expression Result
true && false false What are the values of:
true || false true • 3&&5||0 1
!false true
• !5||!0.5&&-3.5 0
• (9&&3>5||'x')>!6 1

STRUCTURED PROGRAMMING 34
Operators

Logical operators (2)


◼ The precedence of the ! operator is
 Higher than multiplication and division,
 Same as increment and decrement operators
 Lower than brackets
◼ The operator && has
 Higher precedence than ||
 Both have lower precedence than relational operators
◼ Therefore, the expression
a > b && b > c || b > d
will be evaluated as
((a > b) && (b > c)) || (b > d)
◼ Example with problems
 (c==' ') || (c='\t') || (c=='\n') will always be true. Why?
 1 < i < 10 will always be true. Why?

STRUCTURED PROGRAMMING 35
Operators

Evaluation of logical expressions


◼ All logical expressions are evaluated left to
right
◼ Evaluation is performed as long as “we are
not certain” of the result
◼ Example:
 for i=11 when evaluating (i<10) && (i>5) only the first part
(i<10) will be evaluated. Since the result is 0, the
evaluation of the entire expression will stop
◼ Example:
 What will be the value of the expression !i == 5?

STRUCTURED PROGRAMMING 39
Operators

Side effects
#include <stdio.h>
#define Print printf("x=%d y=%d z=%d w=%d\n",x,y,z,w)
int main() {
int x,y,z,w;
y=(x=4,z=2,w=1);
w=(--x<2)&&(z=y); What will be printed?
Print;
z=(y-=w)||(w++!=1); x=3 y=1 z=2 w=0
Print;
x=3 y=1 z=1 w=0
y=(--x>2)&&(--z==++w);
Print; x=2 y=0 z=1 w=0
w=(y||(z=(y+w||--x)&&(w==0))); x=1 y=0 z=1 w=1
Print;
return(0);
}

STRUCTURED PROGRAMMING 44
Operators

Operator precedence and associativity


◼ Precedence
 All unary operators have higher precedence than binary
operators
 The use of brackets changes precedence
 There are 15 levels in C

◼ Associativity
 When two adjacent operators with the same
precedence, the operations than should be executed is
chosen based on the rules for operator associativity
 There are “left to right” and “right to left” associativities

STRUCTURED PROGRAMMING 45
Operators

Level Operator Type Order


(precedence) (associativity)
High
() [] -> . Binary Left to Right
+ ++ ! * sizeof
- -- ~ & Unary Right to Left
->* .* Binary Left to Right
* / % Binary Left to Right
+ - Binary Left to Right
<< >> Binary Left to Right
< <= > >= Binary Left to Right
== != Binary Left to Right
& Binary Left to Right
^ Binary Left to Right
| Binary Left to Right
&& Binary Left to Right
|| Binary Left to Right
? : Ternary Left to Right
+= *= ^= &= <<=
= -= /= %= |= >>= Binary Right to Left
Low , Binary Left to Right
STRUCTURED PROGRAMMING 46
Operators

The ternary (conditional) operator ( ? : )


◼ Defines a conditional expression
◼ Has 3 operands
(①?②:③)
① - condition
② - value to be returned if the condition is true
③ - value to be returned if the condition is false
condition ? value_if_true : value_if_false
Example:
printf("%s\n", score>=50 ? "Pass" : "Fail" );

STRUCTURED PROGRAMMING 47
Operators

What will the following program print on the screen?

#include <stdio.h>
int main() {
int i=0, j, k=7, m=5, n;
j = m+= 2; /* j = m = m+2; */
printf("j= %d\n", j);
j = k++ > 7; /* j = k>7, k++ */
printf("j= %d\n", j);
j = i == 0 && k;/* i==0, j = (1 && k) */
printf("j= %d\n", j);
j=7
return 0; j=0
} j=1
STRUCTURED PROGRAMMING 51
Operators

STRUCTURED PROGRAMMING 52

You might also like