intro to c programming ec201 fundamental programming

8
EC201 F d t l P i EC201 F d t l P i EC201: Fundamental Programming EC201: Fundamental Programming Introduction Introduction To C To C Language Language By: Pn Wan Fazlini Idayu bt Wan Fakari By: Pn Wan Fazlini Idayu bt Wan Fakari 1 How computer working?? How computer working?? How computer working?? How computer working?? Input Process Output Input Process Output Data Processing Data Processing Cycle Cycle (Kit Kit P Dt) Dt) (Kitaran Kitaran P emproses P emproses Data) Data) 2 Data Processing Cycle Data Processing Cycle Data processing cycle is the process of converting of a data into a useful information. The data is processed again and again until the accurate result is achieved the accurate result is achieved. 3 basic activities of data processing: (a) Input (b) Process (b) Process (c) Output Input Process Output 3 (a) Input (a) Input (a) Input (a) Input A set of information called data will be A set of information called data will be entered into the computer from keyboard, floppy disk, hard disk etc. and stored in a portion of the computer memory. This input which is an input to the computer will then be processed to the computer will then be processed to produced the desired result. Input Process Output 4

Upload: wan-fazlini-idayu

Post on 10-Oct-2014

317 views

Category:

Documents


2 download

DESCRIPTION

EC201 Fundamental ProgrammingChaptern2: Intro to C Programming

TRANSCRIPT

Page 1: Intro to C Programming EC201 Fundamental Programming

EC201 F d t l P iEC201 F d t l P iEC201: Fundamental ProgrammingEC201: Fundamental Programming

Introduction Introduction To C To C LanguageLanguage

By: Pn Wan Fazlini Idayu bt Wan FakariBy: Pn Wan Fazlini Idayu bt Wan Fakari

1

How computer working??How computer working??How computer working??How computer working??

Input Process OutputInput Process Output

Data ProcessingData Processing CycleCycle((KitKit PP D t )D t )((KitaranKitaran PemprosesPemproses Data)Data)

2

Data Processing CycleData Processing Cycleg yg yData processing cycle is the process of converting of a data into a useful information. The data is processed again and again until the accurate result is achieved the accurate result is achieved. 3 basic activities of data processing:

(a) Input (b) Process (b) Process (c) Output

Input Process Output

3

p p

(a) Input(a) Input(a) Input(a) InputA set of information called data will be A set of information called data will be entered into the computer from keyboard, floppy disk, hard disk etc. and stored in a portion of the computer p pmemory. This input which is an input to the computer will then be processed to the computer will then be processed to produced the desired result.

Input Process Outputp p

4

Page 2: Intro to C Programming EC201 Fundamental Programming

(b) Process(b) Process(b) Process(b) ProcessData control and storing numerical Data control and storing, numerical comparison and arithmetic handling being done to the input to get the output.

Input Process Outputp p

5

(c) (c) OutputOutput(c) (c) OutputOutputThe processed data which produced The processed data which produced certain result is known as the output. The output data will be presented in a sheet of paper through the printer or display on p p g p p ya monitor

Input Process Outputp p

6

CartaCarta AlirAlirSimbol Makna

Arah aliranMula / Tamat

Input/output

Proses

PilihPilihan

P h bPenghubungOffpage connector

P d f d PPredefined Process

7

Annotation

What you have learned so far What you have learned so far What you have learned so far ….What you have learned so far ….

Analysis:Analysis:- Understand the problem

Design:Design:- How to develop solutions (Algorithm)- How flowchart works

How to construct flowcharts- How to construct flowcharts

8

Page 3: Intro to C Programming EC201 Fundamental Programming

The next step is codingThe next step is codingThe next step is codingThe next step is coding

Coding is a process of converting Coding is a process of converting flowchart to program code (source code)

But before you can start doing this you But, before you can start doing this, you should learn some basics including the l it lflanguage itself.

9

The conversion is almost straight forwardThe conversion is almost straight forwardE l lti l i t bExample: multiplying two number

void main ()Start

Read A

{printf("Enter the first number=> ");scanf("%d",&A);

Read ARead B printf("Enter the second number=> ");

scanf("%d",&B);

Calculate result C=A*B

C = A * B;

printf("The Result is %d" C);

Display the

printf( The Result is %d , C);

}p y

result C

i

10

Stop The program still cannot be executed.It is not completed yet.

You will get these errorsYou will get these errors

Error 1: Call to undefined function ‘printf ’We’re trying to call a function that is notWere trying to call a function that is notrecognized by the compiler.`

Error 3: Undefined symbol ‘A’We’re trying to use a variable but it has nevere e t y g to use a va ab e but t as evebeen defined. Compiler doesn’t recognize it

11

Errors we found just now are called Errors we found just now are called syntax errors.Syntax means rules of a programming languagep g g g gYou will get syntax errors if you do not follo the r lesnot follow the rules

12

Page 4: Intro to C Programming EC201 Fundamental Programming

Fix the errors and complete the programFix the errors and complete the program

This statement will help thecompiler to recognizefunction ‘printf ’ andfunction ‘scanf’

Variable must be declaredbefore we can use itbefore we can use it

13

Structure of a C programStructure of a C program

14

The greeting programThe greeting program

15

CommentCommentCommentComment

Comment is a statement that is not Comment is a statement that is not executed.Compiler will ignore all comments in your programy p gIt is used to describe part of your

k program; makes your program more readable.This helps people and also yourself to understand your codes.understand your codes.

16

Page 5: Intro to C Programming EC201 Fundamental Programming

Examples of comments

17

IdentifiersIdentifiersA b l f d f h bA symbolic name of data, functions or other objects.

ORAn Identifier is a name for a variable type type member template An Identifier is a name for a variable, type, type member, template, class, function, namespace etc and is usually limited to letters, digits and underscores.

E.g:main => symbolic name of main functionmain => symbolic name of main functionprintf => symbolic name of a function that prints output onto the

screen

Rules for identifiers:1. First character must be alphabetic character or underscore’2. Must consist only alphabetic characters, digits, or underscores3 First 31 characters of an identifier are significant3. First 31 characters of an identifier are significant4. Cannot duplicate a reserved word

18

IdentifiersIdentifiersW d t id tifiWord cannot use as identifiers:

auto double int structbreak else long switchcase enum register typedefchar extern return unionconst float short unsignedgcontinue for signed voiddefault goto sizeof volatiledo if static while do if static while

Rules for forming identifier nameh fi h b l h b ( l ) ◦ the first character must be analphabet (uppercase or lowercase) or an

underscore◦ all succeeding characters must be letters or digits.◦ no special characters or punctuation symbols are allowed except the ◦ no special characters or punctuation symbols are allowed except the

underscore"_".◦ no two successive underscores are allowed.◦ keywords shouldn't be used as identifierskeywords shouldn t be used as identifiers.

19

Example:Valid Names:astudent8student_nameperson name_p _

TRUEFALSEFALSE

Invalid Names:Invalid Names:$a3name3nameperson nameintintwhat?

20

Page 6: Intro to C Programming EC201 Fundamental Programming

In C language, identifiers are case sensitive.

Example:a is diffe e t f o Aa is different from A.

21

Nyatakan sama ada pencam takrifan penggunaberikut sah atau tidak:

Pencam 1 Tidak sahPencam_yang_terpanjang_sekali23-JlnDukuJalan#2

SahTidak sahTidak sahJalan#2

Kadar_Ke2RM1200$

Tidak sahSahSahTid k h$500

S100%Jalan Duku

Tidak sahTidak sahSah_

BAB10HEADLINE_TODAYMAIN

SahTidakTidakMAIN

voidint

TidakTidakTidak

22

Standard data typesStandard data types

23

Integer typesInteger types

24

Page 7: Intro to C Programming EC201 Fundamental Programming

FloatingFloating--point typespoint types

25

Some stringsSome strings

26

Null characters and null stringsNull characters and null strings

27

Formatted input and outputFormatted input and output

28

Page 8: Intro to C Programming EC201 Fundamental Programming

Format of printf statementFormat of printf statement

29

Format of Format of scanf scanf statementstatement

30