21

Bab 4 (Data Type,Operator Dan Simple Function in c)

  • Upload
    kyuchi

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 1/21

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 2/21

Data Types

y Data type is a set of value and a set of operations onthose values.

y  A standard data type in C : char, double, float and

int.

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 3/21

Data Type int

y In mathematics, integer are whole numbers.

y The int data type is used to represent integers in C.

y Some values that you can store in type int variable :

-10500 435 +15 -25 32767

y Syntax : int variable_list ;

int number;

int minutes, seconds;

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 4/21

Data Type float /double

y  A real number has an integral part and a fractional part which are separated by a decimal point.

y In C, the data type float/double is used to represent real .

y  You can store a real number in type float/double variable, perform the common arithmetic operations(add, subtract, multiply and divide) and compare them.

y Syntax : float variable_list ;

double variable_list ;

float total;

double average;

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 5/21

Data Type char

y Represents an individual character value ± a letter, adigit or a special symbol.

y Each type char value is enclosed in apostrophes

(single quotes) as shown here.µA¶ µz¶ µ2¶ µ*¶ µ:¶ µ ´ ¶ µ ¶

y Syntax : char variable_list ;

char gender;

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 6/21

 Arithmetic Expressions

y To solve most programming problems, you will need to write arithmetic expression that manipulate type int anddouble/float.

y

Known as unary operators if use for single operand :eg : a++;

y W hen used between constants or variable or both, it¶s calleda binary operator because it operates with two value :

eg : z = x + y ;

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 7/21

 Arithmetic Operators

  Arithmetic operator Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

% Remainder

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 8/21

Operators / and %

y W hen applied to two positive integers, the division operator(/) computes the integral par of the result of dividing itsfirst operand by its second.

y

Example : 7.0 / 2.0 is 3.5 7 / 2 is 3y The remainder operator (%) returns the integer remainder

of the result of dividing its first operand by its second.

y Example : 7 % 2 is 1 299 % 100 is 99

3 22 7 100 299

6 200

1 7 % 2 99 299 % 100

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 9/21

Unary Operators

y Operates on, or affects, a single operand.

a = -30; a++; b = +40; --b;

Operator Example Description Equialent Statement

++ i++ Post i = i + 1 i += 1

++ ++i Pre i = i + 1 i += 1

-- i-- Post i = i - 1 i -= 1

-- --i Pre i = i - 1 i -= 1

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 10/21

Unary Operators

y Example 1 :

int main ( )

{

int x, c;c = -64;

x = +c; // x = -64

x = -c; // x = 64

x = ++c; // x = 65x = --c; // x = 64

}

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 11/21

Unary Operators

y Example 2 :int main ( )

{

int x, xcc, xtt;x = 24;

xcc = x++;

xtt = ++x;

printf(³xcc = %d, xtt = %d´, xcc, xtt);}

xcc = 24, xtt = 26

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 12/21

Binary Operators

y Each operator manipulates two operands which may beconstants, variables or other arithmetic expression.

y Operation related to numeric value (integer andfloat/double).

Ty pe of operation Example

Constant and constant 5 + 25 ± 25 * 2

5 / 25 % 2

Constant and variable a + 9

 Variable and variable x + y  

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 13/21

Binary Operators

y Example 1 :int kira = 5;

nilai_pertama = 10;

nilai_kedua = 5 * --kira + nilai_pertama;

y Example 2 :int main ( )

{ int x, y, z;

x = 10; y = 17;

z = x + y; // z diumpukkn dgn nilai 27

y = y ± x; // y diumpukkn dgn nilai 7

x = y * z; // x diumpukkn dgn nilai 189

z = x / 20; // z diumpukkn dgn nilai 9

y = z % x; // y diumpukkn dgn nilai 9 }

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 14/21

Order of Precedence

y  Arithmetic operation priority 

high Priority 

( ) left to right-- , ++ left to right

*, /, % left to right

+, - left to right

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 15/21

Relational Operators

y Use for data comparisons (equal to, greater than, etc)

y Used with the IF statements

y The result is TR UE (1) or FALSE (0).

Operator Description Example

== Equal to x == y  

> Greater than x > y  

>= Greater or equal to x >= y  

< Less than x < y  

<= Less or equal to x <= y  

!= Not equal to x != y  

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 16/21

Logical Operators

Operator Meaning Example

&& AND ((x < y ) && (x != 0))

|| OR ((x < y ) || (x != 0))! NOT ! ( x >= y)

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 17/21

Precedence of Operators

Operators Precedence

!, + - (unary operators) First

* , / , % Second

+ , - Third

< , <= , >= , > Fourth

= = , != Fifth

&& Sixth

|| Seventh

= (assignment operator) last

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 18/21

 W riting Mathematical Formulas in C

y 2 problem in writing a mathematical formula in C : Multiplication often can be implied in formula by writing the two

items to be multiplied next to each other.

Ù Eg : a = bc in C always use the * operator to indicate

multiplication a = b * c; Difficulty arises in formulas with division

Ù Eg : m = in C the numerator and denominator are placed

on the same line. m = y / x; 

Ù Eg 2: m = parentheses are often needed to separate thenumerator from the denominator and to

indicate clearly the order of evaluation of the

operators in the expression.

m = ( y ± b) / (x ± a);

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 19/21

Library Functions

y C promotes reuse by providing many predefined functionsthat can be used to perform mathematical computations.

y C¶s standard math library defines a function named sqrt

that performs the square root computation.

y the function call in the assignment statement

y = sqrt (x);

y If x is 16.0, the assignment statement above evaluatedas follows : x is 16.0, so function sqrt computes the ¥16.0, or 4.0

The function result, 4.0, is assigned to y.

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 20/21

Library Functions

y Table of the Mathematical library functions lists thenames and descriptions of some of the most commonly used functions along with the name of the standardheader fie to #include in order to have access to eachfunction.

y If one of the functions is called with a numeric argumentthat is not of the argument type listed, the argument value is converted to the require type before it is used.

8/3/2019 Bab 4 (Data Type,Operator Dan Simple Function in c)

http://slidepdf.com/reader/full/bab-4-data-typeoperator-dan-simple-function-in-c 21/21

Some Mathematical Library Functions

Function Standardheader file

Purpose : example arg result

abs (x) <stdlib.h> Return the absolute value : abs(-5) is 5 int int

ceil (x) <math.h> Return the smallest whole number thatis not less than x: fceil (45.23) is 46.0

double double

exp (x) <math.h> Return ex  where e = 2.71828« :exp (1.0) is 2.71828

double double

floor (x) <math.h> Return the largest whole number thatis not greater than x: floor (45.23) is

45.0

double double

log(x) <math.h> Return the natural logarithm of x for x> 0.0 : log(2.71828) is 1.0

double double

pow (x,y) <math.h> Return x y .pow(0.16, 0.5) is 0.4

double,double

double

sqrt (x) <math.h> Return the non-negative square root of x (¥x) for x 0.0 : sqrt(2.25) is 1.5

double double