Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chen Yi Fen anny@fg.tp.edu.tw The C Language Chen Yi Fen anny@fg.tp.edu.tw.

Similar presentations


Presentation on theme: "Chen Yi Fen anny@fg.tp.edu.tw The C Language Chen Yi Fen anny@fg.tp.edu.tw."— Presentation transcript:

1 Chen Yi Fen anny@fg.tp.edu.tw
The C Language Chen Yi Fen

2 History of C 西 元 1960 年 Algo 60 語 言 ( International Committe )
西 元 1963 年 CPL 語 言 ( 劍 橋 與 倫 敦 大 學 ) 西 元 1966 年 BCPL 語 言 下 ( 劍橋大學 Martin Richards ) 西 元 1970 年 B 語 言 ( AT&T Ken Thompson ) 西 元 1972 年 C 語 言 ( AT&T Dennis Ritchie )

3

4 Program I:C語言程式結構 #include <stdio.h> #include <stdlib.h>
int main( ) { printf("This is output from my first program!\n"); system("PAUSE"); return 0; }

5 Program II: 變數與資料型態 #include <stdio.h> #include <stdlib.h>
int main() { int a, b, c; a = 5; b = 7; c = a + b; printf("%d + %d = %d \n", a, b, c); system("PAUSE"); return 0; } a b c 5 7 12 5+7

6 計算圓的周長

7 計算兩項成績平均

8 轉換華氏溫度變成攝氏溫度

9 計算長方形面積

10 基本資料型態 資料型態 位元組 表示範圍 long int 4 -2147483648到2147483647 int
unsigned int 0到 short int 2 -32768到32767 unsigned short int 0到65535 char 1 0到255 (共256個字元) float 1.2e-38到3.4e38 double 8 2.2e-308到1.8e308

11 How printf() work? printf("Hello"); printf("Hello\n");
printf("%d", b); printf("The temperature is %d degrees\n", b); printf("%d + %d = %d\n", a, b, c);

12 How printf() work? int (integer values) uses %d
float (floating point values) uses %f double(double values) uses %lf char (single character values) uses %c character strings (arrays of characters, discussed later) use %s

13 變數名稱 可依個人喜好決定,但不能使用到C語言的關鍵字。通常會用變數所代表的意義來取名,可提高程式的可讀性。

14 變數名稱的限制 長度不要超過8個字元。 變數名稱的字元可以是英文字母、數字或底線。 名稱中不能有空白字元。 第一個字元不能是數字。
不可以為C語言的保留字(Keyword) 不可以為C語言中已定義的函數名稱。

15 不合法的變數名稱 例如: 4ab7 calculate total while C語言的保留字有:

16 變數的設值 可以在宣告時設值。如: int num=2; 宣告後設值。 int num1, num2; char ch; num1=2;
ch = ‘m’;

17 互動式鍵盤輸入 -- scanf()

18 輸入兩數,並計算兩數乘積

19 Program III: (see execution)
#include <stdio.h> #include <stdlib.h> int main() { int a, b, c; printf("Enter the first value:"); scanf("%d", &a); printf("Enter the second value:"); scanf("%d", &b); c = a + b; printf("%d + %d = %d\n", a, b, c); system("PAUSE"); return 0; }

20 計算圓的周長(pg2-03.c)(see execution)
#include <stdio.h> #include <stdlib.h> int main() { float radius, circumference; printf("請輸入半徑(cm):"); scanf("%f", &radius); circumference = 2.0 * * radius; printf("The circumference of the circle is %f \n", circumference); system("PAUSE"); return 0; }

21 計算兩項成績平均 (pg2-04.c)(see execution)
#include <stdio.h> #include <stdlib.h> int main() { float grade1; float grade2; float total; float average; scanf("%f", &grade1); scanf("%f", &grade2); total = grade1 + grade2; average = total / 2.0; printf("The average grade is %f \n", average); system("PAUSE"); return 0; }

22 轉換華氏溫度變成攝氏溫度 (convertF_C.c)
#include <stdio.h> #include <stdlib.h> int main() { float celsius; float fahrenheit; scanf("%f", &fahrenheit); celsius = 5.0/9.0 * (fahrenheit ); printf("%.0f degree F = %.0f degree C \n", fahrenheit, celsius); system("PAUSE"); return 0; }

23 How scanf() work? scanf("%d", &b); scanf(%d %d",&a, &b); int uses %d
float uses %f double uses %lf char uses %c character strings (discussed later) use %s scanf(%d %d",&a, &b);

24 Advanced--Homework 改寫上述程式,使之轉換攝氏溫度變成華氏溫度 (convertC_F.c)
改寫(pg1-06.c),可由鍵盤輸入長、寬,計算長方形面積(rectangle.c) 試寫各種可能的面積(寫愈多,加愈多)。

25 請在程式最上方為程式加上作者等相關資訊註解
/* Date: 97/12/12 Problem: 轉換華氏溫度變成攝氏溫度 FileName: Convert(F_C).c Auther: Mary(no.20)—Driver Jane(no.25)—Navigator */ #include <stdio.h> #include <stdlib.h> int main() { float celsius; float fahrenheit; scanf("%f", &fahrenheit); celsius = 5.0/9.0 * (fahrenheit ); printf("%.0f degree F = %.0f degree C \n", fahrenheit, celsius); system("PAUSE"); return 0; }


Download ppt "Chen Yi Fen anny@fg.tp.edu.tw The C Language Chen Yi Fen anny@fg.tp.edu.tw."

Similar presentations


Ads by Google