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

Post on 17-Jan-2018

223 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

SCCS200: INTRODUCTION AND GETTING START IN C

PROGRAMMING

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

3

• Intro. C Programming1

• The Syntax2

• C Editor Tool 3

Today’s Overview

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

Pseudo-Code

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

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

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.

Flowchart Symbols

A First Book of ANSI C, Fourth Edition

8

Flowchart Symbols (Full)

Programming Languages

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

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

Program Translation

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.

14

The first C Program

Functions

Functions

The main() Function

Sometimes referred to as a driver function

C Libraries - #include …

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.

20

Blocks & Lines

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

with }.

21

Line Example

22

Block Example

The printf( ) FunctionFunction arguments

printf("Hello there world!");

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.

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

26

EXPANDING THE CONCEPT OF C

Company Logo

Displaying Numerical Values

Function arguments

Arguments are separated with commas

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

conversion control sequence

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’);

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

Operator Precedence

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

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

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)

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

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 (‘ ’)

ASCII Codes.

Data Types: Character

Escape Sequences

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

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’);

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

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;}

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;

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( )

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

46

? || //

47

Program:Turbo C/DevC++/Etc.

48

DevC++ Demonstration

49

YOUR TURN

Company Logo

top related