Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ?

Similar presentations


Presentation on theme: "C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ?"— Presentation transcript:

1 C Program Design Supplements 主講人:虞台文

2 Content Who takes the return value of main() ?

3 C Program Design Supplements Who takes the return value of main() ?

4 main() int main( void ) { /* declaration of variable */ /* operations */ return result; /* who takes the result? */ } int main( void ) { /* declaration of variable */ /* operations */ return result; /* who takes the result? */ }

5 Example: Unix grep Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! grepExample.txt Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! grep "ould" grepExample.txt Ah Love! could you and I with Fate conspire Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! Ah Love! could you and I with Fate conspire Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire!

6 Example: Unix grep Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! grepExample.txt Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! grep "ould" grepExample.txt Ah Love! could you and I with Fate conspire Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! Ah Love! could you and I with Fate conspire Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire!

7 Pseudo Code  The Idea to Fulfill the Task count = 0; while (there's another line) if (the line contains the pattern) print it count++; return count;

8 Pseudo Code  The Idea to Fulfill the Task count = 0; while (there's another line) if (the line contains the pattern) print it count++; return count; How? Define functions to mind the detail. How? Define functions to mind the detail.

9 Pseudo Code  The Idea to Fulfill the Task count = 0; while (there's another line) if (the line contains the pattern) print it count++; return count; Pseudo Code  The Idea to Fulfill the Task int getline(char line[], int max) int strindex(char source[], char searchfor[])

10 Example: MyGrep #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int max); int strindex(char source[], char searchfor[]); char pattern[] = "ould"; /* pattern to search for */ /* find all lines matching pattern */ int main( void ) { char line[MAXLINE]; int count = 0; while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); count++; } return count; }......... #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int max); int strindex(char source[], char searchfor[]); char pattern[] = "ould"; /* pattern to search for */ /* find all lines matching pattern */ int main( void ) { char line[MAXLINE]; int count = 0; while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); count++; } return count; }.................. return information to OS

11 Example: MyGrep #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int max); int strindex(char source[], char searchfor[]); char pattern[] = "ould"; /* pattern to search for */ /* find all lines matching pattern */ main() { char line[MAXLINE]; int count = 0; while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); found++; } return count; }......... #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int max); int strindex(char source[], char searchfor[]); char pattern[] = "ould"; /* pattern to search for */ /* find all lines matching pattern */ main() { char line[MAXLINE]; int count = 0; while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); found++; } return count; }.................. while (there's another line) if (the line contains the pattern) print it forward references return information to OS

12 Batch Files

13 Example: MyGrep Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire! Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bits -- and then Re-mould it nearer to the Heart's Desire!

14 練習: 1. 完成以上程式,並測試之。 2. 撰寫一程式讀取使用者從標準輸入裝置中輸 入之字元直到發生 EOF 止,該程式會將所讀 取到的字元總數回傳給作業系統後結束執行。 3. 撰寫一批次檔驗證你能正確收到上題程式之 回傳資料,並做適當顯示。


Download ppt "C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ?"

Similar presentations


Ads by Google