s ccs 200: i ntroduction and g etting s tart in c p rogramming lect. napat amphaiphan

49
SCCS200: INTRODUCTION AND GETTING START IN C PROGRAMMING Lect. Napat Amphaiphan

Upload: eric-thomas

Post on 17-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

Intro. C Programming 1 The Syntax 2 C Editor Tool 3 Today’s Overview 3

TRANSCRIPT

Page 1: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

SCCS200: INTRODUCTION AND GETTING START IN C

PROGRAMMING

Lect. Napat Amphaiphan

Page 2: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Overview (Midterm)Midterm Examination

30 %

Data Type,Declaration,

Display

InteractiveInput Selection Repetition

Analyze

ProblemSolving

intfloatdoublechar

printf()

scanf()if…elseswitch…case

while statementfor statementdo-while statement

Page 3: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

3

• Intro. C Programming1

• The Syntax2

• C Editor Tool 3

Today’s Overview

Page 4: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Algorithms• A procedure for solving a problem in terms of the actions

to be executed and the order.

Algorithm can be written in 2 different ways• Pseudo-code – English-like steps that describes the solution• Flowcharts – Picture with specific blocks detailing out the logical flow of the

solution

• Problem solving• Understand problem statement and analysis• Develop a high-level algorithm• Detail out a low-level algorithm

Page 5: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Pseudo-Code

• An artificial and informal language that helps programmers develop algorithms.

• Pseudo code is a "text-based" detail (algorithmic) design tool.

Page 6: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Flow Chart

• A formalized graphic representation of a logic sequence, work or manufacturing process, organization chart, or similar formalized structure.

• Simple geometric symbols and arrows to define relationships.

Page 7: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Flowchart Symbols

Page 8: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

A First Book of ANSI C, Fourth Edition

8

Flowchart Symbols (Full)

Page 9: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Programming Languages

Page 10: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Programming Languages

• A programming language is a way for humans to write instruction code for computers. Computer communicate in binary that is ones and zeros. An example would look like this...

Page 11: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Programming Languages

• This is very hard to read and under stand so we write code that looks more like human words and would look like this...

Page 12: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Program Translation

Page 13: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

13

What is C ?

• C is a programming language originally developed for developing the Unix operating system (by Dennis Ritchie).

• The pure programming language with no additional concept.

Page 14: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

14

The first C Program

Page 15: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Functions

Page 16: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Functions

Page 17: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

The main() Function

Sometimes referred to as a driver function

Page 18: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

C Libraries - #include …

Page 19: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

C Libraries - #include …(cont.)

Library file Functions

#include <stdio.h> Standard I/O functions – printf, scanf, getc, fopen, fclose

#include <io.h> Traditional file operations – lock, create, filelength, read, write

#include <math.h> Arithmetic – abs, floor, pow, sqrtLogarithmic – log, exp,Trignometric – sin, tan, cos, atanFloating point – fabs, fmod

#include <string.h> String operations – strcpy, strcat, strcmp, strlen, strrev, strlwr, strupr

Without including these libraries, you cannot write C programs that need to use these standard function. Check your reference for details on the C libraries, their functions and how they can be used.

Page 20: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

20

Blocks & Lines

• Each line of code end with ; (Only Instruction)• Blocks are things that start with { and end

with }.

Page 21: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

21

Line Example

Page 22: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

22

Block Example

Page 23: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

The printf( ) FunctionFunction arguments

printf("Hello there world!");

Page 24: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

24

Type of Functions

• 1. Standard-Library Function (built-in function): Already store in C library, a programmer can instantly use it.

• 2. Write by Programmer: A function created by individual programmer.

Page 25: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

25

Explaining your Code - Comment

• Programmers can forgot their own code.• Use them to explain sections of code• To create a comment in C, you surround the

text with /* and then */ or using //

Page 26: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

26

EXPANDING THE CONCEPT OF C

Company Logo

Page 27: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Displaying Numerical Values

Function arguments

Arguments are separated with commas

printf("The total of 6 and 15 is %d", 6 + 15);

conversion control sequence

Page 28: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Displaying Numerical Values (cont.)

Example

printf("The average of 5 ,10 and 11 is %f",(5+10+11)/3);printf("The average of 5 ,10 and 11 is %f", avg);printf(" %d \n %f \n %c ", 10 , 10.11 , ‘a’);

Page 29: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Arithmetic Operations

• Arithmetic operators: operators used for arithmetic

operations:

– Addition +

– Subtraction -

– Multiplication *

– Division /

– Modulus Division %

– minus sign (-) used in front of a single numerical operand negates the

number

Page 30: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Operator Precedence

Page 31: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Operator Precedence (cont.)

• Find the result of 6 + 4 / 2 + 3

6 + 2 + 3

8 + 3

11

• Find the result of 8 + 5 * 7 % 2 * 4

8 + 35 % 2 * 4

8 + 1 * 4

8 + 4

12***Suggestion: Use () to ensure your result. ***

Page 32: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

32

Using Variables// Version 4 What will happen (Variable Concept)

/*int main(){ int sum = 5+6+7; int multi = 10*20; int total = sum+multi; printf("%d", total); getch(); return 0; }*/

Declaration

Page 33: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Data Types

• Data type: define how the compiler identifies variables.

• Four basic data types used in C– Integer (int)– Floating point (float)– Double precision (double)– Character (char)

Page 34: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Data Types: Integer• are any positive or negative number without a decimal point• may be signed (+ or -) or unsigned • no commas, decimal points, or special symbols

• Valid integer constants5 -10 +25 1000 253 -26351 +36

• Invalid integer constants$255.62 2,523 3. 6,243,892 1,492.89 +6.0

Page 35: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Data Types: Character• Characters are letters of the alphabet (uppercase &

lowercase)

• char Data Type is used to store individual characters. Include the letters of the alphabet (both uppercase and lowercase), the ten digits 0-9, and special symbols such as +,&,!

• Examples of valid character constants‘A’ ‘$’ ‘b’ ‘7’ ‘y’ ‘!’ ‘M’ ‘q’

• enclosed by single quotes (‘ ’)

Page 36: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

ASCII Codes.

Page 37: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Data Types: Character

Page 38: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Escape Sequences

Page 39: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Data Types: Floating Point & Double Precision umbers• are any signed or unsigned numbers having a decimal

point• no special symbols

• Valid floating point and double precision constants+10.6255 5. -6.2 3251.92 0.0 0.33 -6.67 +2.

• Invalid floating point and double precision constants5,326.25 24 123 6,459 $10.29

• Floating Point VS Double Precision Numbers

Page 40: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Displaying Numerical Values (cont.)

Example

printf("The average of 5 ,10 and 11 is %f",(5+10+11)/3);printf("The average of 5 ,10 and 11 is %f", avg);printf(" %d \n %f \n %c ", 10 , 10.11 , ‘a’);

Page 41: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Variables and Declarations

• Variables are names given by programmers

• Variable names are case sensitive• Rules of variable names

– begin with letter or underscore (_) and may contain only letters, underscores, or digits

– cannot contain any blanks, special symbols– use capital letters to separate names consisting of multiple words– cannot be a keyword– no more than 31 characters

Page 42: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Variables and Declarations(cont.) general form

function name( ){

declaration statements;

other statements;}

simplest formdataType variableName;

Examplevoid main(){

int X,Y;float HOLA;char MyChar, mychar;

other statements;}

Page 43: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Initialization andAssign value

• When a declaration statement provides an initial value, the

variable is said to be initialized

– int x = 10;

– float y = 1.2;

• Assign value: right hand side to left hand side

– int x;

– x = 10;

– x = 5+10;

Page 44: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Common Programming Errors

• Omitting the parentheses after main

main main( )

• Omitting or incorrectly typing the opening brace { that signifies the

start of a function body

• Omitting or incorrectly typing the closing brace } that signifies the

end of a function

• Misspelling the name of a function

print( ) printf( )

Page 45: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

Common Programming Errors (cont.)

• Forgetting to close the message to printf( ) with a

double quote (“ ”) symbol

• Omitting the semicolon (;) at the end of each

statement

• Forgetting the \n to indicate a new line

Page 46: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

46

? || //

Page 47: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

47

Program:Turbo C/DevC++/Etc.

Page 48: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

48

DevC++ Demonstration

Page 49: S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan

49

YOUR TURN

Company Logo