Operators and Expression - Student
Operators and Expression - Student
• Integer Expression
• Floating point expression
• Mixed mode expression
Integer Expression
• Mathematical expression obtained by
combining integer variables and/or integer
constant with help of arithmetic operators
• Rules
– A signed or unsigned integer variable or an a, -b, 4, 3
integer constant is an integer expression
– An integer expression connected by
arithmetic operator to an unsigned integer k*a
constant is an integer expression
– An integer expression enclosed in (k+b)
parentheses is an integer expression
– 2 integer expression connected by an
arithmetic operator is an integer expression 3+5
% operator can only be used with integers
Floating point expression
• Mathematical expression obtained by
combining floating point variables and/or
floating point constant with help of arithmetic
operators
• Rules- same with integer
Mixed mode expression and implicit conversion
• Expression formed by integer, floating point or character
constant
• A is integer and B floating point.. A/B??
– Before division A must be converted to floating point then A is divided
by B. Finally result will be stored as floating point number.
• During conversion lower type operand are converted to
higher type operand and the result is the higher type
Data type Order
Long double Higher
Double
Float
Unsigned long int
Long int
Unsigned int
int
Char, short Lower
Type casting
Format: (data_type) variable name
Expression Action
A= (int) 8.75 The value of A is finally 8
B= (float)x +y x is converted to float then added to
y
C=(double)(x+y) x +y converted to double
D=(char) 65 65 converted to character (A) – ASCII
value
E= (float)1/5 1 is converted to float 1.0 divided by
5.0 the value of E becomes 0.2
float a, e, d, f;
scanf(“%3f %2f %*2f %3f %*2f %3f”, &e, &a, &d, &f);
print f(“%*.*f% -4.3f %1.4f”, (int)a, (int)d, a+e, e/f, d*e);
Input: 123456789123456
5 + 12 - 4 / 2
5 + 12 - 2
17 - 2
15
Exercises
Express the answers in integer
(1) 2*2+3/3+2/2*2+3/3+2
(2) 2*3+2/3+2+2*3+2/3+2*3+2+3*3+2/3+2
Exercises
//5. Determine the output for the following statements
#include<stdio.h>
#include<math.h>
#define A 2+1
#define B(a) 2+a*A
int main()
{
int x,y,a;
x=A*A-A*A;
y=B(5+1)*B(A);
printf("x=%d y=%d", x,y);
getchar();
return 0;
}
//6. Determine the output for the following statements
#include <stdio.h>
#include<math.h>
#define A 3+2
#define B(a) a*A/A
#define D(b,e) B(b+2)*A+e
int main()
{
int y, x=2;
y=D(B(x),B(3))+1;
printf(“y=%d", y);
getchar();
return 0;
}
Library Functions
Mathematic C Equivalent Mode of output Header file
function
Power (ab) pow (a,b) double math.h
Exponential exp(x) double math.h
logarithm log(x) double math.h
squareroot sqrt(x) double math.h
Sin sin( x) double math.h
Cos cos(x) double math.h
Tangent tan(x) double math.h
Absolute |x| abs(x) int stdlib.h
Absolute |x| fabs(x) double math.h
Round up to next floor(x) double math.h
integer value
Exercises:
3𝑥 + 2
1. 𝑓 𝑥 =
4𝑎 − 1
2. 𝑓 𝑥 = 𝑥 2 + 𝑒 3𝑥 − sin 5𝑥
3. 𝑥 3 − 2𝑥 2 + 𝑥 − 6.3
𝑓 𝑥 =3
𝑥 + 0.05𝑥 − 3.14
4. 𝑥3 + 𝑥 − 2 sin 𝑥 3
𝑓 𝑥 = − 5
𝑥 + 𝑙𝑜𝑔2 𝑥 𝑥 +2
Unary Operators
Operator Meaning
- negative
+ positive
-- decrement
++ increment
Example:
Unary Operators
++x is prefix: prefix adds the value before processing the data/command
x++ is postfix: process the data/command first before it adds the value
Unary Operators
Size of operator
• Give the size of operand in bytes
• sizeof is a compile-time operator used to calculate
the size of data type or variable.
• sizeof operator will return the size in integer format.
• sizeof is a keyword. Example:
Size of int : 4
Size of long int : 4
Size of long long int : 8
Size of float : 4
Size of double : 8
Size of char : 1
Exercises:
Typical Integer Sizes and Values for Signed Integers
Computer Science: A
Structured Programming 26
Approach Using C
Relation Expression
Relational use
Relational operator
expression
variable vs constant
produce
constant vs constant
27
Relation Expression
Operator Description
== Equal to
> Greater than
< Less than
>= Greater than or
equal
<= Less than or
equal
!= Not equal
28
Relation Expression
P/s:
Example 1: a, b and c are variables,
Replace with the given values
1) a+ b == c 2) a != b
6 + 1== -2 6 != 1
7 == -2
29
Relation Expression
Example 2 :
3) b < a 4) b + c <= a
1 < -2 1 + -2 < 6
-1 < 6
30
Relation Expression
#include <stdio.h>
void main()
{ int age;
31
Example
Logical Expression
Can consists of
Relational expr. vs logical expr.
33
Logical Expression
Logical Operator
Operator Description
&& AND
|| OR
! NOT
34
Logical Expression
35
Logical Expression
36
Logical Expression
NOT(!)
37
Logical Expression
Example 1:
1 && 1 (1 < 2) || (6 == 6)
1 1 || 1
38
Logical Expression
Example 2:
Given a = 3, b = 4;
!(1) ! ( 0)
0 1
39
Exercises:
#include<stdio.h> #include<stdio.h>
int main() #include<math.h>
{ int main()
int a=4,b=5, c=6, d=7; {
printf("%d",(a<b)&&(c<d)); int a=3,b=5, c=6, d=7;
printf("\n%d", (a<b)||(d<c)); printf("%d",(a<b)&&(c<a));
printf("\n%d",!(a<b)&&(c<d)); printf("\n%d",(d%b<c)||(c*a>=d));
return 0; printf("\n%d",!((c-b)==(d-c)));
} getchar();
return 0;
}
Logical Expression
#include <stdio.h>
void main()
{ int mark;
41
Example
Arithmetic Expression
Function
operator:
+= , -= , *= , /= , %=
43
Example :
44
Arithmetic Expression
x=4;
x=5;y=2
Conditional operator
• The general synthax is:
Expression1 ? Expression2: expression 3;
– Value of expression1 is evaluate first. If value true, expression2
is performed otherwise expression3 is performed.
Comma operator
• A set of expression within parentheses and separated by
commas can be assigned to variable
• The expression evaluated left to right and the value of
last expression is the final value of variable