Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Program Design Introduction to C Programming

Similar presentations


Presentation on theme: "C Program Design Introduction to C Programming"— Presentation transcript:

1 C Program Design Introduction to C Programming
主講人:虞台文

2 Content Background Main Parts of C Programs 標準輸出入裝置與輸出入函式 標準輸出入函式
算術運算子(Arithmetic Operators) 關係運算子(Relation Operators) 邏輯運算子(Logical Operators) 巨集  The #define Preprocessor

3 C Program Design Introduction to C Programming
Background

4 The C Language Currently, the most commonly-used language for embedded systems High-level assembly Very portable compilers exist for virtually every processor Easy-to-understand compilation Produces efficient code Fairly concise

5 The Development of the C Language
C History Developed between 1969 and 1973 along with Unix Due mostly to Dennis Ritchie Designed for systems programming Operating systems Utility programs Compilers Filters Evolved from B, which evolved from BCPL

6 Computer Architecture
ALU Control CPU Input Output Memory Device

7 C Program Design Introduction to C Programming
Main Parts of C Programs

8 Our First C Program  Hello World

9 Our First C Program  Hello World
/* Hello.c Our first program */ #include <stdio.h> /* function main begins program execution */ main() { printf("hello, world\n"); }

10 The #include Directive
include information about standard library /* Hello.c Our first program */ #include <stdio.h> /* function main begins program execution */ main() { printf("hello, world\n"); }

11 The #include Directive
include information about standard library /* Hello.c Our first program */ #include <stdio.h> /* function main begins program execution */ main() { printf("hello, world\n"); }

12 Entry Point of C Programs
/* Hello.c Our first program */ #include <stdio.h> /* function main begins program execution */ main() { printf("hello, world\n"); } define a function called main that receives no argument Function body

13 Function Body /* Hello.c Our first program */ #include <stdio.h>
/* function main begins program execution */ main() { printf("hello, world\n"); } Function body starts with { Function body Function body ends with }

14 C Statements C statements end with ; /* Hello.c Our first program */
#include <stdio.h> /* function main begins program execution */ main() { printf("hello, world\n"); } a C statement C statements end with ;

15 Comments /* . . . */ // or /* Hello.c Our first program */
for multiple lines Comments or // (c++) for single line /* Hello.c Our first program */ #include <stdio.h> /* function main begins program execution */ main() { printf("hello, world\n"); }

16 Comments /* . . . */ // or // Hello.c // Our first program
for multiple lines Comments or // (c++) for single line // Hello.c // Our first program #include <stdio.h> // function main begins program execution main() { printf("hello, world\n"); } /* Hello.c Our first program */ #include <stdio.h> /* function main begins program execution */ main() { printf("hello, world\n"); }

17 printf  Print formatted data to stdout
// Hello.c // Our first program #include <stdio.h> // function main begins program execution main() { printf("hello, world\n"); } function Parameter (string) function name

18 printf  Print formatted data to stdout
printf("Hello, World\n"); Instructs computer to perform an action Specifically, prints the string of characters within quotes (" ") Entire line called a statement All statements must end with a semicolon (;) Escape character (\) Indicates that printf should do something out of the ordinary \n is the newline character

19 Some Common Escape Sequences

20 Editing  Compiling  Linking  Execution
Ctrl-Z (EOF)

21 Editing  Compiling  Linking  Execution

22 練習 重複以上Editing  Compiling  Linking  Execution之過程
修改Hello.c程式,練習不同的Escape Sequences ,如: printf("Hello,\nWorld\n"); printf("Hello,\a\a\a\a\a\aWorld\n"); printf("Hello,\b\b\b\b\b\bWorld\n"); printf("Hello,\tWorld\n");

23 練習 作業題 在IDE環境下重做以上各練習

24 C Program Design Introduction to C Programming
標準輸出入裝置與輸出入函式

25 標準輸出入裝置 預設 ALU Control CPU Input Output Memory Device 預設 Input Output

26 標準輸出入裝置之重新導向 標準輸出裝置重新導向: > outfile

27 標準輸出入裝置之重新導向 標準輸出裝置重新導向: > outfile

28 標準輸出入裝置之重新導向 標準輸出裝置重新導向: > outfile

29 C Program Design Introduction to C Programming
標準輸出入函式

30 printf  Print formatted data to stdout

31 scanf  Read formatted data from stdin

32 Add Two Integers integer1 integer2 sum = integer1 + integer2
1 /* AddTwoInts.c 2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */ integer1 integer2 sum = integer1 + integer2

33 Add Two Integers 15 20 35 integer1 integer2 sum Enter first integer 15
Enter second integer 20 1 /* AddTwoInts.c 2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */ Sum is 35 >

34 標準輸出入裝置之重新導向 15 20 35 標準輸出裝置重新導向: > Outfile 標準輸入裝置重新導向: < Infile
integer1 integer2 sum 15 20 35 標準輸出入裝置之重新導向 標準輸出裝置重新導向: > Outfile 標準輸入裝置重新導向: < Infile 1 /* AddTwoInts.c 2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */

35 標準輸出入裝置之重新導向 15 20 35 標準輸出裝置重新導向: > Outfile 標準輸入裝置重新導向: < Infile
integer1 integer2 sum 15 20 35 標準輸出入裝置之重新導向 標準輸出裝置重新導向: > Outfile 標準輸入裝置重新導向: < Infile 1 /* AddTwoInts.c 2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */

36 標準輸出入裝置之重新導向 15 20 35 標準輸出裝置重新導向: > Outfile 標準輸入裝置重新導向: < Infile
integer1 integer2 sum 15 20 35 標準輸出入裝置之重新導向 標準輸出裝置重新導向: > Outfile 標準輸入裝置重新導向: < Infile 1 /* AddTwoInts.c 2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */

37 Definition of Variables
integer1 integer2 sum 15 20 35 Definition of Variables 1 /* AddTwoInts.c 2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */ C語言中任何變數使用前均需宣告或定義 變數名稱必需是一有效的identifier 區分大小寫(case sensitive),例A1與a1不為同一變數 變數之資料型態(int, char, float, double, …) 變數之位址與值

38 變數定義之注意事項 定義意義明確之變數名稱 範例: 變數未設定其值前,勿任意假設其初值 勿擔心變數長度過長(有效長度至少31)
定義名稱不得為C語言之保留字 範例: 變數未設定其值前,勿任意假設其初值 // Single declarations int age; float amountOfMoney; char initial; // Multiple declarations int age, houseNumber, quantity; float distance, rateOfDiscount; char firstInitial, secondInitial;

39 Single vs. Multiple Declarations
1 /* AddTwoInts.c 2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */ int integer1; int integer2; int sum; int integer1, integer2, sum;

40 變數之初值 ? ? ? integer1 integer2 sum 1 /* AddTwoInts.c
2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */

41 變數之初值 變數未設定其值前,勿任意假設其初值 ? ? ? integer1 integer2 sum 1 /* AddTwoInts.c
2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 main() 7 { 8 int integer1; /* first number to be input by user */ 9 int integer2; /* second number to be input by user */ 10 int sum; /* variable in which sum will be stored */ 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 scanf( "%d", &integer1 ); /* read an integer */ 14 15 printf( "Enter second integer\n" ); /* prompt */ 16 scanf( "%d", &integer2 ); /* read an integer */ 17 18 sum = integer1 + integer2; /* assign total to sum */ 19 20 printf( "Sum is %d\n", sum ); /* print sum */ 21 } /* end function main */ 變數未設定其值前,勿任意假設其初值

42 Basic Data Types char a single byte, capable of holding one character in the local character set int an integer, typically reflecting the natural size of integers on the host machine float single-precision floating point double double-precision floating point The type of an object determines the set of values it can have and what operations can be performed on it.

43 Program Indentation /* AddTwoInts.c Addition program */
#include <stdio.h> /* function main begins program execution */ main() { int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable in which sum will be stored */ printf( "Enter first integer\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ printf( "Enter second integer\n" ); /* prompt */ scanf( "%d", &integer2 ); /* read an integer */ sum = integer1 + integer2; /* assign total to sum */ printf( "Sum is %d\n", sum ); /* print sum */ } /* end function main */ Tab

44 Bad Format (I) 以下範例編譯後產生相同結果,但不易閱讀 /* AddTwoInts.c Addition program */
#include <stdio.h> /* function main begins program execution */ main() { int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable in which sum will be stored */ printf( "Enter first integer\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ printf( "Enter second integer\n" ); /* prompt */ scanf( "%d", &integer2 ); /* read an integer */ sum = integer1 + integer2; /* assign total to sum */ printf( "Sum is %d\n", sum ); /* print sum */ } /* end function main */

45 Bad Format (II) 以下範例編譯後產生相同結果,但不易閱讀 /* AddTwoInts.c
Addition program */ #include <stdio.h> /* function main begins program execution */ main(){ int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable in which sum will be stored */ printf( "Enter first integer\n" ); /* prompt */ scanf( "%d", &integer1 ); /* read an integer */ printf( "Enter second integer\n" ); /* prompt */ scanf( "%d", &integer2 ); /* read an integer */ sum = integer1 + integer2; /* assign total to sum */ printf( "Sum is %d\n", sum ); /* print sum */ } /* end function main */

46 練習 實作以下程式,預測及觀察其結果 將變數值改以呼叫scanf()方式要求使用者輸入 輸出入重新導向練習
#include < stdio.h > main() { int sum; float money; char letter; double pi; sum = 10; /* assign integer value */ money = 2.21; /* assign float value */ letter = 'A'; /* assign character value */ pi = 2.01E6; /* assign a double value */ printf("value of sum = %d\n", sum ); printf("value of money = %f\n", money ); printf("value of letter = %c\n", letter ); printf("value of pi = %e\n", pi ); }

47 C Program Design Introduction to C Programming
算術運算子(Arithmetic Operators)

48 Arithmetic Operators

49 Precedence of Arithmetic Operators

50 範例: y = 2x2+3x+7 y = 2 * x * x + 3 * x + 7; /* PolynormialEval.c */
#include <stdio.h> main() { int x, y; printf("x = "); /* prompt */ scanf( "%d", &x ); /* read x */ /* Evaluate y = 2x^2 + 3x + 7 */ y = 2 * x * x + 3 * x + 7; /* print y */ printf( "y = 2 * %d * %d + 3 * %d + 7 = %d\n", x, x, x, y); }

51 y = 2 * x * x + 3 * x + 7; 範例: y = 2x2+3x+7

52 C Program Design Introduction to C Programming
關係運算子(Relation Operators)

53 Relation Operators in C
The result of a relation operation is true (nonzero) or false (zero). Relation Operators in C

54 Statements Simple Statements Null Statement
lower = 0; upper = 300; step = 20; fahr = lower; Null Statement ; // a null statement Compound Statements (block statements) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } 4 simple statements 1 compound statement

55 If-Else Statement if (expression) statement1 else statement2

56 If-Else Statement if (expression) statement1 else statement2
If expression is evaluated to true (nonzero), statement1 is executed; otherwise, statement2 is executed. If-Else Statement start end expression statement1 statement2 true false if (expression) statement1 else statement2 expression

57 If-Else Statement if (expression) statement1 else statement2
start if (expression) statement1 else statement2 expression true expression false statement1 statement2 option end

58 If-Statement if (expression) statement expression start true false end

59 範例: Decision Making (I)
/* pass1.c */ #include <stdio.h> main() { int threshold = 75; int score; printf("Enter your score, please\n"); scanf("%d",&score); if (score >= threshold){ printf("Incredible, you passed with a merit\n"); }

60 Program Indentation /* pass1.c */ #include <stdio.h> main() {
int threshold = 75; int score; printf("Enter your score, please\n"); scanf("%d",&score); if (score >= threshold){ printf("Incredible, you passed with a merit\n"); }

61 Simplification   /* pass1.c */ #include <stdio.h> main() {
int threshold = 75; int score; printf("Enter your score, please\n"); scanf("%d",&score); if (score >= threshold){ printf("Incredible, you passed with a merit\n"); }

62 Simplification /* pass1.c */ #include <stdio.h> main() {
int threshold = 75; int score; printf("Enter your score, please\n"); scanf("%d",&score); if (score >= threshold) printf("Incredible, you passed with a merit\n"); }

63 範例: Decision Making (II)
/* pass2.c */ #include <stdio.h> main(void) { int threshold = 75; int score; printf("Enter your score, please\n"); scanf("%d",&score); if (score >= threshold){ printf("Incredible, you passed with a merit\n"); } else{ printf("You failed, unlucky\n");

64 Program Indentation /* pass2.c */ #include <stdio.h>
main(void) { int threshold = 75; int score; printf("Enter your score, please\n"); scanf("%d",&score); if (score >= threshold){ printf("Incredible, you passed with a merit\n"); } else{ printf("You failed, unlucky\n");

65 Simplification     /* pass2.c */ #include <stdio.h>
main(void) { int threshold = 75; int score; printf("Enter your score, please\n"); scanf("%d",&score); if (score >= threshold){ printf("Incredible, you passed with a merit\n"); } else{ printf("You failed, unlucky\n");

66 Simplification /* pass1.c */ #include <stdio.h> main(void) {
int threshold = 75; int score; printf("Enter your score, please\n"); scanf("%d",&score); if (score >= threshold) printf("Incredible, you passed with a merit\n"); else printf("You failed, unlucky\n"); }

67 While-Statement while(expression) statement expression start false end
true false

68 範例:華氏攝氏

69 範例:華氏攝氏 #include <stdio.h> /* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; }

70 範例:華氏攝氏 #include <stdio.h> /* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; }

71 範例:華氏攝氏 #include <stdio.h> /* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; }

72 Integer Arithmetic #include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; }

73 Can it be “celsius = 5/9*(fahr-32);”?
Integer Arithmetic #include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } Can it be “celsius = 5/9*(fahr-32);”? Why?

74 printf  General-Purpose Output Formatting Function
#include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; }

75 printf  General-Purpose Output Formatting Function
decimal tab decimal newline format string printf("%d\t%d\n", fahr, celsius);

76 printf  General-Purpose Output Formatting Function
#include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } Left aligned

77 printf  General-Purpose Output Formatting Function
#include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } printf("%3d %6d\n", fahr, celsius);

78 printf  General-Purpose Output Formatting Function
#include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } Right aligned printf("%3d %6d\n", fahr, celsius);

79 範例:華氏攝氏(浮點) #include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; }

80 範例:華氏攝氏(浮點) #include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; }

81 範例:華氏攝氏(浮點) #include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; }

82 More on printf

83 Program Indentation #include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; }

84 Program Indentation #include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } Statement Statement Statement Statement Statement Statement Statement

85 Program Indentation #include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } Statement Statement Statement Statement Statement Statement Statement Statement Statement

86 Program Indentation Compound Statement #include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { float fahr, celsius; float lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } Statement Statement Statement Statement Statement Statement Compound Statement

87 練習 Using Redo the same task but with the starting number, ending number, and step being given by a user.

88 練習 Write a C program which asks a user to input a positive integer. The program then reports the minimum factor of that number in 2, 3, 5, 7, 11 if available. Otherwise, reports “fail to find any factor in 2, 3, 5, 7, 11.” The same as above, but the program reports all factors of that number in 2, 3, 5, 7, 11. The same as above, but the program reports all possible factors of that number.

89 C Program Design Introduction to C Programming
邏輯運算子 (Logical Operators)

90 Logical Operators && ( logical AND ) || ( logical OR )
Returns true if both conditions are true || ( logical OR ) Returns true if either of its conditions are true ! ( logical NOT, logical negation ) Reverses the truth/falsity of its condition Unary operator, has one operand

91 Truth Tables

92 Examples if((celsius >= 25.0) && (celsius <= 28.0))
printf("It is very comfortable\n"); if((celsius < 20.0) || (celsius > 32.0)) printf("It is bad\n"); else printf("It is comfortable\n"); if(!(celsius <= 18.0)) printf("It is not very cold\n"); else printf("It is better to take a jacket\n");

93 注意事項 count若為零將產生divide by zero之exception. int total, count;
if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); count若為零將產生divide by zero之exception.

94 注意事項 Why? int total, count; . . . . . . . . .
if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); 注意事項 int total, count; if(total / count >= 60 && count > 0) printf("Passed\n"); else printf("Failed\n"); Why? int total, count; if(count > 0 && total / count >= 60) printf("Passed\n"); else printf("Failed\n");

95 注意事項 Why? int total, count; . . . . . . . . .
if(total / count >= 60) printf("Passed\n"); else printf("Failed\n"); 注意事項 int total, count; if(total / count < 60 || count == 0) printf("Failed\n"); else printf("Passed\n"); Why? int total, count; if(count == 0 || total / count < 60) printf("Failed\n"); else printf("Passed\n");

96 練習 預測以下程式將產生何種結果 實作以下程式視預測是否正確 main() { int x=1,y=2,z=3; int p,q;
p = (x>y) && (z<y); /* False i.e. 0 */ q = (y>x) || (y>z); /* True i.e. 1 */ printf(" %d && %d = %d\n",p,q,p&&q); printf(" %d || %d = %d\n",p,q,p||q); /* Can mix "logical" values and arithmetic */ printf(" %d && %d = %d\n",x,q,x&&q); printf(" %d || %d = %d\n",p,y,p||y); /* Exercise the NOT operator */ printf(" ! %d = %d\n",p,!p); printf(" ! %d = %d\n",q,!q); /* NOT operator applied to arithmetic */ printf(" ! %d = %d\n",z,!z); }

97 C Program Design Introduction to C Programming
巨集 The #define Preprocessor

98 #define for text substitution #include <stdio.h>
#define PI main() { double radius; printf("Enter the radius of a circle:"); scanf("%lf", &radius); printf("The perimeter of the circle: %lf\n", 2.0 * PI * radius); printf("The area of the circle: %lf\n", PI * radius * radius); }


Download ppt "C Program Design Introduction to C Programming"

Similar presentations


Ads by Google