0% found this document useful (0 votes)
13 views28 pages

W02S1-Operator Pada Bahasa C 2

The document provides an overview of various operators in C programming, including assignment, arithmetic, relational, logical, bitwise, and special operators. It explains concepts such as multiple and compound assignments, increment and decrement operations, and the use of pointers and type casting. Additionally, it covers operator precedence and examples of each operator type to illustrate their usage.

Uploaded by

Hans Manalu
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)
13 views28 pages

W02S1-Operator Pada Bahasa C 2

The document provides an overview of various operators in C programming, including assignment, arithmetic, relational, logical, bitwise, and special operators. It explains concepts such as multiple and compound assignments, increment and decrement operations, and the use of pointers and type casting. Additionally, it covers operator precedence and examples of each operator type to illustrate their usage.

Uploaded by

Hans Manalu
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/ 28

Basic Programming

- Operators -
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

Basic Programing, 2021/2022 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.
Basic Programming, 2021/2022 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)

Basic Programming, 2021/2022 4


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

Basic Programming, 2021/2022 5


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.

Basic Programming, 2021/2022 6


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

Basic Programming, 2021/2022 7


Keluarannya:

Basic Programming, 2021/2022 8


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.

Basic Programming, 2021/2022 9


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.

Basic Programming, 2021/2022 10


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.

Basic Programming, 2021/2022 11


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

Basic Programming, 2021/2022 12


Basic Programming, 2021/2022 13
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

Basic Programming, 2021/2022 14


? 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.

Basic Programming, 2021/2022 15


& 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.

Basic Programming, 2021/2022 16


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.“

Basic Programming, 2021/2022 17


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

Basic Programming, 2021/2022 18


[ ] and ( ) Operator
▪ Square brackets perform array indexing
(arrays are discussed fully later).
▪ Parentheses are operators that increase
the precedence of the operations inside
them.

Basic Programming, 2021/2022 19


Operator Precedence
highest

lowest

Basic Programming, 2021/2022 20


Type Conversion Example
char ch;
int i;
float result;

result = (ch / i) + ch;


int
int
int

float

Basic Programming, 2021/2022 21


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.

Basic Programming, 2021/2022 22


Type casting for arithmetic

Basic Programming, 2021/2022 23


Something wrong with this
code..

Basic Programming, 2021/2022 24


Basic Programming, 2021/2022 25
Basic Programming, 2021/2022 26
Basic Programming, 2021/2022 27
Any question?

Basic Programming, 2021/2022 28

You might also like