1 First Step to C Language 2 Online Programming Language Introduction of C Language Simple Example.

Post on 21-Dec-2015

223 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

Transcript

1

First Step to

C Language

2

Online

• Programming Language

• Introduction of C Language

• Simple Example

3

Programming Language

High-LevelLanguages

Assembly Languageor Low-levelLanguage

Machine Language

Machine

4

Program Execution

程式語言( Programming language )

• 我們可以定義一種比較類似人類說話的語言( Language ),用來撰寫程式,取代機器語言。

• 機器語言: 0 , 1 所組成的表示法– Example : 20A3 ( R0A3 )

• 低階語言:與處理器( Processor )相關– Example : Assembly , load R0,#A3H

• 高階語言:更接進人類語言或視覺傳達 – Example : C 、 VB , a=163

轉譯程式( Translator )• 轉譯程式( Translator ):一種系統軟體, 將輸入的原始程式轉換成另一種相對應的程式語言。– 原始程式( Source program )– 目的程式( Object program )

原始程式(Source Program)

Text file

轉譯程式

Translator

目的程式(Object Program)

Text file orExecution file

ex:20A3 ex: a=163

7

Low Level Language

• Every CPU has its machine language.

• CPU only understand its machine language.

• Usually, the CPU manufactory creates an assembly language for it.– Example: 8051, 8088 assembly language

• Of course, we need a translator -- assembler -- to translate the program written by the assembly language to the machine code.

8

High Level Language

• Algo (1960)

• FORTRAN (1957)

• C (1970)

• Pascal (1971)

• Basic 、 VB 、 VC........

• The translator for high level language is called compiler or interpreter.

Compiler (編譯程式)• 將高階語言所寫的程式 (included a file) 翻譯成為機器語言程式 (another execution file)

• Example : C 、 C++ 、 Fortran 、 Pascal

高階語言程式sample1.c(a text file)

編譯程式Compiler

g++

目的程式h1*

(an executable file)

10

Building a Program in C

檔案為sample1.c

Interpreter (直譯程式)• 將程式一行一行讀入,每讀入一行便將其翻譯成為機器碼並執行之。執行完畢再進行下一行的解譯。

• Example : BASIC 、 Shell 、 VB

高階語言程式project.vbp

直譯程式Interpreter

VB

執行結果

VB 程式Sum = Val(Text1.Text) + Val(Text2.Text)

+ Val(Text3.Text)

Ave = Sum /3

Print " 平均分數 = "; Ave

Preprocessor (前置處理程式)• 將某一種高階語言所寫的程式翻譯成為另一種高階程式語言程式

高階語言程式Fortran 77

前置處理程式

Preprocessor

目的程式Fortran IV

目的程式Object Code

目的程式Fortran IV

Fortran IV 編譯程式

14

Introduction of C

15

History of C Language (1/2)• In 1960 , Algo 60 ( International Committee )

• In 1963 ,劍橋大學和倫敦大學以 ALGOL 60 為基礎,共同推出了 CPL (Combined Programming Language) 語言。

• In 1966 ,劍橋大學的 Martin Richards 對 CPL 語言做了簡化,推出了 BCPL (Basic Combined Programming Language) 語言。

• In 1970 , AT&T 的 Ken Thompson 以 BCPL 語言為基礎簡化了 BCPL 語言,設計出 B 語言,並利用 B 語言寫出早期的 UNIX 作業系統。

16

History of C Language (2/2)• In 1972, AT&T. 的 Dennis Ritchie 以 B 語言為基礎,設計出 C 語言 ( 取 BCPL 的第二個字母 ) 。之後 Ken Thompson 和 Dennis Ritchie 兩人又合力利用 C 語言寫出了新版的 UNIX 作業系統。

• In 1983 , AT&T. 的 Bjarne Sroustrup 於 C 語言中加入 class ( 類別 ) 的概念,以及物件導向的概念,產生出 C++ 語言。

17

C & C++

• C++ is an extension of C language.– C++ has object-oriented concept– 在 C 中使用 class 的語法表示 object.– C++ is called as “C with a class”

• No object: 即使是針對同件事物 , 每一個參數各自獨立運作 .

• Object-oriented: 針對同件事物 , 把所有的參數 , 與其上的操作包裹起來 .

18

Rectangle (1)

width

height

• Attribute: – Width=2– Height=1– You can’t access them directly.

• Function:

– area()=width * height– perimeter()=2(width+height)

width=2,height=1

area(),perimeter()

如此就可以避免粗心大意的程式設計師改掉 attribute 中的資料 !

19

Rectangle (2)

width

height

• Attribute: – width– height– You can’t access them directly.

• Function: – Set width– Set height– area()=width * height– perimeter()=2(width+height)

width,height

area(),perimeter()

20

Object

• An object = the data + member functions– Data owns the value or attributes.– Only member function can access the data.

• In C, we use “class” to define the data structure of your object.

• A class can create a copy of object.

Data: x

member functions: add x : x=x+1 sub x : x=x-1

an Object

or a member of a class

21

Class and Subclass• 長方形

– 正方形長方形 is a class

正方形是長方形的subclass

• 動物– 昆蟲

• 蝴蝶• 蟬

– 鳥– 爬蟲類

• 交通工具– 汽車

• 轎車• 跑車

– 火車交通工具 is a class

汽車是交通工具的 subclass

汽車 is a class

轎車是汽車的 subclass

22

Class – Car Example

23

Inheritance

• Classes has inheritance.

• Base class Derived class– Derived class has all attributes of base class and

itself attributes.

traffic tools

car

train

如此程式碼就可以reuse!

24

Square

width

height

• Square is a rectangle

• Attribute: – width– height=width– You can’t access them directly.

• Function: – Set width– area()=width * height– perimeter()=2(width+height)

width,height

area(),perimeter()

width

width

area()perimeter()

25

Overload

• “+” is the add operation for integers.– “a++” means “a=a+1”.

• However, these operations is not defined for other classes.– You can define the functions for these symbols.– Example: a object “WaitTime”, define that

“WaitTime++” means a random number between 0 and 30. 所有的運算 (*,+,...) 本

來就“有”明確定義出其的使用的資料範圍 !

內訂運作於 integer!

26

Overload Square

• A is a square.

• Function: – area(A)=width * height– A**=area(A)– perimeter(A)=2(width+height)– A++ = perimeter(A)

width

27

sample1.c

#include < cstdio> /* or #include <stdio.h> */

main( )

{ int i=0 ; /* variable declaration */

i=i+1; /* variable operation */ printf("i=%d\n",i) ; /* output to screen */}

28

Notes

1. { 與 } 符號表示程式或敘述的開始與結 束 .

2. ; 符號表示變數宣告或指令敘述的段落 .

3. /* 與 */ 符號表示程式內註解 .

4. 主程式為 C 程式執行之進入點 .

5. 上述所提到之副程式不一定存在於整個程式中 , 但是主程式必需存在 .

6. C compiler 不管空格 , 但請保持良好的寫作格式 .

29

C Compiler

C program C Compiler Executable Output File

sampe1.c h1g++

pure text file

an execution file for translation

execution file

30

Compile Your Program 1

>g++ -o h1 sample1.c

C complier output file Your C program

> h1 Begin to execute

i=1

>

31

sample2.c#include <iostream>

using namespace std;

main()

{

int num = 2;

char sym_1 = 'c';

cout << "Give me "<< num << " scoops of \n";

cout << " " << sym_1 << "ho" << sym_1 << "olate, ";

cout << " " << sym_1 << "ho" << sym_1 << "olate, \n";

cout << "Please!\n";

}

32

Compile Your Program 2

>g++ -o h2 sample2.c

> h2

Give me 2 scoops of

chocolate, chocolate,

Please!

>

33

Header Files

#include <iostream>

using namespace std;– This command tells C compiler to include the file

“iostream.h” before compiling.– The files included are called as header files.

#include "event.h" – If you use “...” rather than <...>, then C compiler

will find this included file “event.h” from the directory of the main program.

34

C Preprocessor

• The C preprocessor and compiler combine together.

• C preprocessor parses the C program first. It translates some commands and still gives another C programs as output.

• For example: #include < stdio.h > or

#include <cstdio>– C preprocessor will find the file stdio.h and include it

to your C program.

35

Output in C++

#include <iostream>

using namespace std;

cout << “C out” ;– cout is an object.– cout reads data put after << commands.– << is an insert operator of cout.– Data is sent to the standard output.– Usually, your monitor is the standard output.

36

Errsample1.c

#include < cstdio> main( ) { int i=0 ;

i=i+1 /* ; is lost */ printf("i=%d\n",i) ; }

37

Syntax Error Occurs

>g++ -o h3 Errsample1.c

Errsample1.c: In function `int main()':

Errsample1.c:7: parse error before `( '

>

All of the errors list

and no output file h3 exists

• Syntax :語法 ==> 程式的寫法

38

Errsample2.c

#include < cstdio > main( ) { int i=0 ;

i=i+1; /* variable operation */ printf("i=%d\n", &i) ; /* output error */}

39

Semantic Error Occurs

>g++ -o a.out Errsample2.c

>a.out

i = -1073743372

>

output file a.out exists

but output i’s address -> Error!

• Semantic :語意 ==> 程式的意義• Semantic error! Run time error!

40

Set Path

• If you can’t use g++?

• copy .cshrc to your home directory– We use tcsh shell.

• Set path – set path = (/bin /usr/bin /usr/ucb

/usr/local/bin /usr/etc /etc .)

• You can find the directory of g++ by “whereis g++”.

41

C Instructions

42

Variable

• Variables are temporary memory to save some important values.

• Each variable needs a name as well as its data type.– Ex: i is an integer.

name data type

43

Variable Name

• C allows a variable-length name. However, only the first 32 characters is useful.

• You can use alphabet, number, underline for your name.

• First character must be an alphabet. – Example: var1, a, string_name, StringName

• Capital and small alphabet is different in C.– Example: “Var” and “var” are different.

• Keyword can’t be variable name.

44

Data type

• A data type is an abstract representation of stored data.

• Every data type has it scope. – int: -32768 32767 (16 bits)– long: -2147483648 2147483647 (32 bits)– unsigned int: 0 65535 (16 bits)– unsigned long: 0 4294967295 (32 bits)– float: (+-)10^-38 10^38 (32 bits)– double: (+-) 10^-308 10^308 (64 bits)– char: 0 255 (8 bits ASCII)

45

Declaration

int i; /* i is an integer */

double abc; /* abc is a floating point. */

char A1; /* A1 is a character */– We need to declare the variable before it is

used.– That is , bind its name and its data type.– We can change the values of variables by

assignment.

46

Assignment

i=20; /* i has a new value 20 */

abc=3.14; /* abc has value 3.14 */

A1=‘a’; /*A1 has value 61h */

B1=‘\a’; /* B1 has value 07h */– Assign the value to a variable that has been

declared.

47

Constant

#define PI 3.141592

#define FileName “sample1.c”– We can’t change the value of constant if it has

been defined.

48

Escape Sequence

• Some characters used to represent special functions are called as escape sequence.\a beep \\ backslash \

\b backspace \’ single quote ’

\f form feed \’’ double quote ’’

\r carriage return \xhh hexadecimal

\t tab \n new line (endl)– Ex: cout << “Hi! \n” ;

cout << “\‘‘ Hi!\’’ she said” << endl;

49

Output in C

#include <cstdio>

int i =1;

printf ("Hi");

printf(“i=%d" , i); – printf send the string to the standard output.– If you want to send the value of variable to

screen, you need to use the string format to denote the variable.

50

String Format

%c char

%s char [n]

%d int

%i int

st=‘A’;

printf(“The character is %c \n”, st);

printf(“PI is %f, i is %d. \n”, PI, i);

%f float

%ld long

%lf double

%Lf long double

51

Format Modify

printf("a=%.2f\n",a) ;

printf("a=%10.2f\n",a);

printf("i=%4d\n",i);

printf("i=%04d\n",i);

a=12.35

a= 12.35

i= 12

i=0012                     

52

Output Format

• Operator “setw” can set the format of output string.

#include <iomanip>

cout << setw(8) << "LOC:" << setw(10) << "Taiwan" << setw(3) << i << endl;

Loc: Taiwan 1

8 chars 10 chars 3 chars

53

Input in C++

#include <iostream>

using namespace std;

double abc;

cin >> abc ; /* read and save abc */– cin is an object:– cin get data value from the standard input.– Usually, your keyboard is the standard input.

54

Input in C

#include <cstdio>

printf("Please input an integer :") ;

scanf("%d", &i);

printf("Please input a floating point number:") ;

scanf("%f", &a);– & is called as “address-of operator”.– &a means the address of a.

55

Arithmetic Operators

• a=7+1; /* a=8 */

• b=5-3; /* b=2 */

• c=a*b; /* c=16 */

• d=a/b; /* d=4 */

• e=a%b; /* e=0 */

56

Homework• 尋找 C 語言的由來與其演進• Please write a program to read two floating

point numbers, add them and show the result on screen.

• Please write a program to read the temperature in Fahrenheit and show it in Celsius degree.– You must have commands and beautiful format.– Print your program and results.

top related