14S1201 - PROGRAMMING 1
- OPERATORS -
Dosen: Arnaldo Marulitua Sinaga (aldo@[Link])
11/02/20 AMS/Pengantar Pemrograman 1
Overview
§ Assignment Operator
– Multiple Assignment
– Compound Assignment
§ Arithmetic Operator
– Increment and Decrement Operator
§ Relational and Logical Operator
§ Bitwise Operator
§ Special Operator
– ? Operator
– & and * Operator
– Comma Operator
– Dot (.) and Arrow (->) Operator
– [] and () Operator
11/02/20 AMS/Pengantar Pemrograman 2
Operators
§ C is very rich in built-in operators.
§ There are four main classes of operators:
– arithmetic,
– relational,
– logical,
– and bitwise.
§ In addition, there are some special operators,
such as the assignment operator, for particular
tasks.
11/02/20 AMS/Pengantar Pemrograman 3
Assignment Operator
§ = (read: single equal sign)
§ General form:
variable_name = expression;
§ Assignment may involve type conversion:
The value of the right side (expression side) of
the assignment is converted to the type of the
left side (target variable)
11/02/20 AMS/Pengantar Pemrograman 4
Assignment Operator:
Type Conversion Example (1)
int x;
char ch;
float f;
void func(void) {
ch = x; /* line 1 */
x = f; /* line 2 */
f = ch; /* line 3 */
f = x; /* line 4 */
}
11/02/20 AMS/Pengantar Pemrograman 5
Assignment Operator:
Type Conversion Example (2)
§ Line 1:
– The left high-order bits of the integer variable x are lopped
off, leaving ch with the lower 8 bits.
– If x were between 255 and 0, ch and x would have
identical values. Otherwise, the value of ch would reflect
only the lower-order bits of x.
§ Line 2:
– x will receive the non-fractional part of f.
§ Line 3:
– f will convert the 8-bit integer value stored in ch to the
same value in the floating-point format.
§ Line 4:
– Similar with line 3, except that f will convert an integer
value into floating-point format
11/02/20 AMS/Pengantar Pemrograman 6
Keluarannya:
11/02/20 AMS/Pengantar Pemrograman 7
Multiple Assignment
§ You can assign many variables the same value
by using multiple assignments in a single
statement.
§ Example below is assigned 0 to variable x, y,
and z on the nearly same time:
x = y = z = 0
11/02/20 AMS/Pengantar Pemrograman 8
Compound Assignment
§ There is a variation on the assignment statement, called
compound assignment, that simplifies the coding of a
certain type of assignment operations.
§ For example,
x = x + 10
can be written as
x += 10
§ Generally, compound assignment operator exists for all
operators that require two operands.
11/02/20 AMS/Pengantar Pemrograman 9
Arithmetic Operator
Operator Meaning Example
– Subtraction, also unary minus 5–3=2
+ Addition 10+7 = 17
* Multiplication 2*4=8
/ Division 6/3=2
% Modulus 18 % 5 = 3
–– Decrement
++ Increment
11/02/20 AMS/Pengantar Pemrograman 10
Keluarannya:
11/02/20 AMS/Pengantar Pemrograman 11
Increment and Decrement Operator (1)
§ These are the increment and decrement operators, ++
and – –.
§ The operator ++ adds 1 to its operand, and – – subtracts
1.
§ Example:
Postfix Prefix Meaning
x++; ++x; x=x+1
x--; --x; x=x-1
It seems exactly the same, doesn’t it? No, it has a difference.
11/02/20 AMS/Pengantar Pemrograman 12
Increment and Decrement Operator (2)
§ A difference between the prefix and postfix
forms appears when it’s used in a larger
expression:
– When an increment or decrement operator precedes
its operand, the increment or decrement operation is
performed before obtaining the value of the operand
for use in the expression.
– If the operator follows its operand, the value of the
operand is obtained before incrementing or
decrementing it.
11/02/20 AMS/Pengantar Pemrograman 13
Increment and Decrement Operator (3)
§ Example:
– Prefix
x=10;
x=10; equivalence with x=x+1;
y=++x; y=x;
sets y to 11 and x to 11 as well.
– Postfix
x=10;
x=10; equivalence with y=x;
y=x++; x=x+1;
sets y to 10 and x to 11.
11/02/20 AMS/Pengantar Pemrograman 14
Relational & Logical Operator
Relational Operator
Operator Meaning
Logical Operator
> Greater than
Operator Meaning
>= Greater than or equal
&& AND
< Less than
|| OR
<= Less than or equal
! NOT
== Equal
!= Not equal
11/02/20 AMS/Pengantar Pemrograman 15
11/02/20 AMS/Pengantar Pemrograman 16
Bitwise Operator
Operator Meaning Example
& AND 27 & 21 = 17
| OR 10 | 7 = 15
^ Exclusive OR (XOR) 10 ^ 29 = 23
~ One’s complement (NOT) ~ (0101)2 = 1010
>> Shift Right 7 >> 2 = 1
<< Shift Left 9 << 3 = 576
11/02/20 AMS/Pengantar Pemrograman 17
11/02/20 AMS/Pengantar Pemrograman 18
? Operator
§ Ternary operator ? Takes general form:
Expression 1 ? Expression 2: Expression 3
§ Example:
y = x>9 ? 100:200;
if x=10 or greater, then value 100 is assigned to y.
if x=9 or lesser, then value 200 is assigned to y.
§ Equivalence code can be written using if-else statement:
if (x>9) then
y=100;
else
y=200;
§ The ? operator will be discussed more fully in relationship to the
other conditional statements.
11/02/20 AMS/Pengantar Pemrograman 19
& and * Pointer Operator
§ A pointer is the memory address of an object. A pointer
variable is a variable that is specifically declared to hold
a pointer to an object of its specified type.
§ Pointers are one of C's most powerful features, and they
are used for a wide variety of purposes.
§ The first pointer operator & is a unary operator that
returns the memory address of its operand
§ The second pointer operator * is the complement of &.
The * is a unary operator that returns the value of the
object located at the address that follows it.
§ The pointer operator, especially *, will be discussed
more in later chapter.
11/02/20 AMS/Pengantar Pemrograman 20
Comma Operator
§ Comma operator puts several expressions together as one
statement.
§ The expression on the right side becomes the value of the total
comma-separated expression.
§ Example:
x = (y=3, y+1);
first assigns value 3 to y and then assigns value 4 (from 3+1) to x.
Note:
The parentheses are necessary because the comma operator has a
lower precedence than the assignment operator.
§ The comma operator has somewhat the same meaning as the word
"and" in English, as used in the phrase "do this and this and this.“
11/02/20 AMS/Pengantar Pemrograman 21
Dot (.) and Arrow (->) Operator
§ The . (dot) and the –> (arrow) operators access
individual elements of structures and unions.
§ The dot operator is used when working with a
structure or union directly.
§ The arrow operator is used with a pointer to a
structure or union.
§ Both operator will be discussed more in later
chapter
11/02/20 AMS/Pengantar Pemrograman 22
[ ] and ( ) Operator
§ Square brackets perform array indexing
(arrays are discussed fully later).
§ Parentheses are operators that increase
the precedence of the operations inside
them.
11/02/20 AMS/Pengantar Pemrograman 23
Operator Precedence
highest
lowest
11/02/20 AMS/Pengantar Pemrograman 24
Type Conversion Example
char ch;
int i;
float result;
result = (ch / i) + ch;
int
int
int
float
11/02/20 AMS/Pengantar Pemrograman 25
Casts
§ You can force an expression to be of a specific
type by using a cast.
§ General form:
(type) expression
§ Example:
(float) x/2
§ Casts are technically operators. As an operator,
a cast is unary and has the same precedence as
any other unary operator.
11/02/20 AMS/Pengantar Pemrograman 26
Type casting for arithmetic
11/02/20 AMS/Pengantar Pemrograman 27
Any question?
11/02/20 AMS/Pengantar Pemrograman 28