Professional Documents
Culture Documents
SPe22-05.Operators
SPe22-05.Operators
Operators
Content
◼ Arithmetic operations
◼ cast operations
◼ Logical operators
◼ Relational operators
STRUCTURED PROGRAMMING 2
Operators
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
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); }
GRAMMING 12
Operators
printf("%d\n", j=93);
a=(b=c)+(d=e+2);
STRUCTURED PROGRAMMING 13
Operators
+= 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 )
STRUCTURED PROGRAMMING 17
Operators
expression explanation
++var The value of var is incremented by 1 first, then, it returns
the value
STRUCTURED PROGRAMMING 20
Operators
Content
◼ Arithmetic operations
◼ cast operations
◼ Relational operators
◼ Logical operators
STRUCTURED PROGRAMMING 23
Operators
Relational operators
Relational operators
STRUCTURED PROGRAMMING 24
Operators
((b=2) == a) returns:
1 (true)
STRUCTURE D PROGRAMMING 26
Operators
Content
◼ Arithmetic operations
◼ cast operations
◼ Relational operators
◼ Logical operators
STRUCTURED PROGRAMMING 32
Operators
STRUCTURED PROGRAMMING 34
Operators
STRUCTURED PROGRAMMING 35
Operators
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
◼ 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
STRUCTURED PROGRAMMING 47
Operators
#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