The C Language Chen Yi Fen annyfg tp

  • Slides: 25
Download presentation
The C Language Chen Yi Fen anny@fg. tp. edu. tw

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

History of C n n n 西 元 1960 年 Committe ) 西 元

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

Program I: C語言程式結構 #include <stdio. h> #include <stdlib. h> int main( ) { printf("This

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

Program II: 變數與資料型態 #include <stdio. h> #include <stdlib. h> int main() a b c

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

基本資料型態 位元組 表示範圍 long int 4 -2147483648到 2147483647 unsigned int 4 0到 4294967295 short

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

How printf() work? n n n printf("Hello"); printf("Hellon"); printf("%d", b); printf("The temperature is %d

How printf() work? n n n printf("Hello"); printf("Hellon"); printf("%d", b); printf("The temperature is %d degreesn", b); printf("%d + %d = %dn", a, b, c);

How printf() work? n n n int (integer values) uses %d float (floating point

How printf() work? n n n 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

#include <stdio. h> #include <stdlib. h> int main() { int a, b, c; Program

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

計算圓的周長(pg 2 -03. c)(see execution) 1. 2. 3. 4. 5. #include <stdio. h> #include

計算圓的周長(pg 2 -03. c)(see execution) 1. 2. 3. 4. 5. #include <stdio. h> #include <stdlib. h> int main() { float radius, circumference; 6. printf("請輸入半徑(cm):"); scanf("%f", &radius); 7. 8. 9. circumference = 2. 0 * 3. 1416 * radius; 10. 11. 12. printf("The circumference of the circle is %f n", circumference); 13. 14. 15. 16. } system("PAUSE"); return 0;

計算兩項成績平均 (pg 2 -04. c)(see execution) #include <stdio. h> #include <stdlib. h> int main()

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

轉換華氏溫度變成攝氏溫度 (convert. F_C. c) #include <stdio. h> #include <stdlib. h> int main() { float

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

How scanf() work? n scanf("%d", &b); n n n int uses %d float uses

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

請在程式最上方為程式加上作者等相關資訊註解 /* Date: 97/12/12 Problem: 轉換華氏溫度變成攝氏溫度 File. Name: Convert(F_C). c Auther: Mary(no. 20)—Driver Jane(no.

請在程式最上方為程式加上作者等相關資訊註解 /* Date: 97/12/12 Problem: 轉換華氏溫度變成攝氏溫度 File. Name: 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 - 32. 0); printf("%. 0 f degree F = %. 0 f degree C n", fahrenheit, celsius); } system("PAUSE"); return 0;