0% found this document useful (0 votes)
68 views47 pages

Operators and Expression - Student

The document discusses different types of operators and expressions in C programming. It describes arithmetic, relational, logical, and unary operators as well as arithmetic, logical, integer, floating point, and mixed mode expressions. Operands are manipulated using operators to represent specific actions in expressions.

Uploaded by

linesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views47 pages

Operators and Expression - Student

The document discusses different types of operators and expressions in C programming. It describes arithmetic, relational, logical, and unary operators as well as arithmetic, logical, integer, floating point, and mixed mode expressions. Operands are manipulated using operators to represent specific actions in expressions.

Uploaded by

linesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Operators and Expression

Ts. Dr Norfazillah Talib


JKP, FKMP
Operators
• Arithmetic operators
• Unary operators
• Relational operators
• Logical operators
• Assignment operators
• Conditional operators
• Comma operators
Expression
• Arithmetic expression
• Logical expression
• Character/string expression

Operands are the objects that are manipulated and


operators are the symbols that represent specific actions.

For example, in the expression;


5+x
X and 5 are operands and + is an operator
Arithmetic Operators
Arithmetic operators C operators
Addition +
Substraction -
Multiplication *
Division /
Modulo division %
Arithmetic Expression

• 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

I=(int) ‘D’ D converted to integer. The ASCII


value of D is stored to I (68)
Exercises I/O
Input: 1.23.456.789.10.11.12

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

int a=2, e=3, d=5, f=(int)'a';


scanf("%003d%*02d%2d%*1d%4d%1d", &a, &e, &d, &a, &a);
printf("%04d%+4d%4d%-4d", a%e, e+f, a*e, e-d);
Hierarchy of arithmetic operators
Operator Arrangement/Priority
(if appears more than
once in a statement)
() Left to right
++, -- Right to left
*, /, % Left to right
+, - Left to right
Example:
5+2*6–4/2

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

Combination of more than one statement

Can consists of variable vs variable

variable vs constant
produce

constant vs constant

0 (if false) 1(if true)

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

int a=6, b =1, c = -2;

1) a+ b == c 2) a != b

6 + 1== -2 6 != 1
7 == -2

Answer: 0(False) Answer : 1 (True)

29
Relation Expression

Example 2 :

int a=6, b =1, c = -2;

3) b < a 4) b + c <= a

1 < -2 1 + -2 < 6
-1 < 6

Answer: 0 (False) Answer : 1 (True)

30
Relation Expression

An example program which uses relational expression

#include <stdio.h>
void main()
{ int age;

printf(“\nPlease enter your age >>”);


scanf(“%d”,&age);

if (age > 21) Relational expression

printf(“\nYou are qualified to vote”);

31
Example
Logical Expression

Logical expression Logical Operator


use

Combination of one or more expressions

Can consists of
Relational expr. vs logical expr.

produces Relational expr. vs variable

Relational expr. vs constant

0 (if false) 1(if true)

33
Logical Expression

Logical Operator

Operator Description

&& AND

|| OR

! NOT

Logical operator && dan || is used between 2 or more


relational expression

34
Logical Expression

Logical operator truth table for AND

AND (&&) Logical Result


Operator
Value 0 1 False AND False False
0 0 0
False AND True False
1 0 1 True AND False False
True AND True True

35
Logical Expression

Logical operator truth table for OR

OR (||) Logical Result


Operator
Value 0 1 False OR False
0 0 1 False

1 1 1 False OR True True


True OR False Trrue
True OR True True

36
Logical Expression

Logical operator truth table for NOT

NOT(!)

Value Result Logical Result


!0 1 Operator
Not false True
!1 0
Not true False

37
Logical Expression

Example 1:

Evaluate the following logical expression:

a) (2 < 5 ) && ( 5 < 10) b) (7 % 2 < 2) || ( 2 * 3 == 6)

1 && 1 (1 < 2) || (6 == 6)

1 1 || 1

38
Logical Expression

Example 2:

Evaluate the following logical expression:

Given a = 3, b = 4;

c) !((5 * b <= 23 - a )) d) ! ((b +3 != 8) &&( 3 * a < 2))

!((5 * 4 <= 23 – 3)) !(( 7 != 8 ) && ( 9 < 2 ))

!(20 <= 20) !( 1 && 0 )

!(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

An example program which using Logical


Expression:

#include <stdio.h>
void main()
{ int mark;

printf(“\nEnter your mark >>”);


scanf(“%d”,&mark);

if (mark >= 85 && mark <= 100)


printf(“\nGred A”);

else if( mark >=80 && mark <=84)


printf(“\nGred A-”);
}

41
Example
Arithmetic Expression

Function

To combine two different operator together.


To simplify arithmetic operator
Original function of operator does not affected
Allowed combination are as follow:

operator:

+= , -= , *= , /= , %=

43
Example :

Assignment Expression Meaning


Operator
+= total + = 300 total = total+ 300
-= total - = count+ 300 total = total - (count +
300)
*= total *=300 total = total * 300
/= total /= count– 10 total = total / ( count
–10)
%= total % = 7 total = total % 7

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

You might also like