Top Banner
Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure and data type of C programs Skill: Edit, compile and execute C programs
27

Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

Aug 24, 2019

Download

Documents

lyngoc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

Computer Science Department

Bahasa C : Struktur

Program dan Tipe Data

Knowledge:

Understand the complete structure and data type of C programs

Skill:

Edit, compile and execute C programs

Page 2: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

/* Task: This program calculates BMI */

#include <stdio.h>

#define WEIGHT 60.0

#define HEIGHT 1.53

void main(void) {

float bmi;

bmi = WEIGHT / (HEIGHT * HEIGHT);

if (bmi > 0.0025)

printf(“\nYour BMI is %.2f. Need to lose weight! \n”, bmi);

}

TK1913-C Programming 2

Introduction

Resembles Algebraic

Expression

Augmented by certain

English keywords

eg. if , else, while

C is a High-level Language Can also be used at lower-level This flexibility allows it to be

used for SYSTEM

PROGRAMMING (eg.

Operating systems like

UNIX and WINDOWS)

APPLICATION

PROGRAMMING

(Billing System ? )

C has small instruction set,

though the actual

implementations include

extensive library functions

“myfunction.h”

C encourages users

to write additional

library

functions of their own

Finally, C programs are highly

portable.

They can be executed on different

platforms without having to be

recompiled (or with little modification)

Page 3: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 3

C Program Structure

Preprocessor

Instruction

void main (void)

{

}

Statement

Global Declaration

Local Declaration

Page 4: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 4

Example of C Program

#include <stdio.h>

void main(void) {

printf(“Welcome to Fasilkom Unsri!”);

}

program

Main function

statement Preprocessor instruction

Page 5: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 5

Preprocess Instruction

2 types of preprocess instruction that are

normally used:

#include

#define

#include is used to include certain files into the

program. Those files need to be included

because they contain the necessary information

for compilation (e.g. stdio.h file contains

information about printf function)

Page 6: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 6

Preprocess Instruction #define is used to declare macro constants

Example:

#include <stdio.h>

#define PI 3.141593

void main(void) {

float luas;

luas = PI * 7 * 7;

printf(“Luas %.2f:”, luas);

}

Macro constant

Before preprocess Example:

#include <stdio.h>

#define PI 3.141593

void main(void) {

float luas;

luas = 3.141593 * 7 * 7;

printf(“Luas %.2f:”, luas);

}

After preprocess

Page 7: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 7

Main Function

Every C program must have a main

function, called main()

The execution of C program starts from

main() function

Page 8: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 8

Statement

„Sentence-like‟ action steps that are written in the

body of the function

In C, all statements must be ended with ; symbol

Example: A statement to display a string of characters on the screen by using printf() function

printf(“Welcome to Fasilkom Unsri”);

Output:

Welcome to Fasilkom Unsri

Page 9: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 9

Comment You could include comments in your program to ease

understanding

Comments will be ignored during compilation

A block of comment is labeled with /* (start) and */ (end)

compiler will ignore any text written after /* symbol till */

symbol is found

Nested comments (comment within comment) are not

allowed, for example:

/* my comment /* subcomment*/ my comment continues */

Page 10: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 10

Example of C Program

/* This program is to print Welcome to Fasilkom Unsri */

#include <stdio.h>

void main() {

printf(“Welcome to\n”);

printf(“Fasilkom“);

printf(“Unsri\n”);

}

Page 11: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 11

Example of C Program

Welcome to

Fasilkom Unsri

What will the output be?

Page 12: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 12

Identifiers

Identifiers are:

Variable

Constant

Function

Others

Page 13: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 13

Identifiers

Syntax Rules:

Consist of only letters, digits and underscores

Cannot begin with a digit

Cannot use C reserved words

Try not to use/redefine C standard identifiers

What are C

reserved words?

What are C standard

identifiers?

Page 14: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 14

C Reserved Word

A word that has special meaning in C

C Standard Identifier

A word having special meaning but may be

redefined (but is not recommended!!)

Examples of reserved word:

int, void, double, return Examples of standard identifiers:

printf, scanf

Identifiers

Page 15: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 15

Variable

A name associated with a memory

cell whose value can change

Needs to be declared:

variable_type variable_name;

Example: int x;

int entry_time, charge;

Page 16: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 16

Variable Types of variable:

Character: char

An individual character value – a letter, a digit or a

symbol (e.g. „A‟, „4‟, „*‟)

Integer: int

Whole numbers (e.g. +16, 568, -456)

Float: float

A real number which has a decimal point (e.g. 8.00,

3.1416)

High-level Float: double

Page 17: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 17

Variable

Variable Declaration:

Example 1:

char letter;

letter is a character-type variable

Example 2:

float matric;

matric is a ??? variable

Page 18: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 18

Variable

Example:

Calculate and display the price of a number of

apples if the quantity in kg and price per kg are

given.

•Input: quantity and price_per_kg

•Output: price

•Process: price = quantity * price_per_kg

Page 19: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 19

Variable

Example:

int quantity;

float price_per_kg;

float price;

Why did we

declare it as int?

Why did we

declare them as

float?

Page 20: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 20

Example:

int number1, number2;

number1 = 25;

number2 = 23;

number1 = number2;

Variable - Value Assignment

number1 ?

number2 ?

25 23

23

Page 21: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 21

Variable - Value Assignment

Algorithm

variable expression

Syntax

variable = expression;

Rules

Expression‟s type must be the same as variable‟s type

Valid Example: Invalid Example:

int x; int y;

x = 12; y = 5.75;

Page 22: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 22

Variable - Value Assignment

Example:

int quantity;

float price_per_kg, price;

quantity = 5;

price_per_kg = 4.50;

price = quantity * price_per_kg;

How does this

program work?

Page 23: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 23

Example:

int quantity;

float price_per_kg, price;

quantity = 2;

price_per_kg = 4.50;

price = quantity * price_per_kg;

Variable - Value Assignment

quantity ?

price_per_kg ?

price ?

4.50

9.00

2

Page 24: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 24

Variable - Value Assignment

Example:

int number1, number2;

number1 = 25;

number2 = 23;

number1 = number2;

How does this

program

segment work?

Page 25: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 25

Constant

A value that will not change

Consists of:

Float (e.g. 2.3 0.255)

Integer (e.g. 3 67 -2)

Character(e.g. „z‟ „3‟ „$‟ „\n‟)

String (e.g. “UKM” “1” “5a”)

Page 26: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 26

Exercise

To practice what you‟ve

learned, try exercises on

Lab

Page 27: Bahasa C : Struktur Program dan Tipe Data - si.ilkom.unsri ... · Computer Science Department Bahasa C : Struktur Program dan Tipe Data Knowledge: Understand the complete structure

TK1913-C Programming 27

End of Lecture 4

What’s next? …INPUT AND OUTPUT on the way …

If you want to excel: • revise chapter 1 & 2

• practice programming skills for next week… Otherwise, you may watch tv, sleep etc. as a preparation for next week classes.