Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to the C Programming Language

Similar presentations


Presentation on theme: "Introduction to the C Programming Language"— Presentation transcript:

1 Introduction to the C Programming Language
Preprocessor (前端處理器)

2 Preprocessor (前端處理器) Preprocessor (前端處理器)主要之三項功能: 巨集 #define 指令
常數代換 字串代換 定義簡易函數 包含檔案 (include) : 將某個檔案包含於目前的檔案下工作, 通常包含進來的檔案都以 h 為副檔名, 如: stdio.h, stdlib.h等 #include <檔案名稱> #include “檔案名稱” 條件式編譯 (在此不介紹) 在此僅討論前兩項

3 範例一 : #define 利用 #define 方式撰寫輸入圓周半徑計算圓面積 define 使用方式
#define PI #define Area(r) PI * (r) * (r) void main() { int r; printf("Enter radius : "); scanf(" %d", &r); printf("The area = %10.4f \n", Area(r)); } define 使用方式

4 範例二: #define 利用 #define 方式撰寫判斷輸入數值為奇數或偶數
#define odd(x) ((x) % 2 ==1) ? 1 : 0 void main() { int x; printf("Enter one integer value : "); scanf(" %d", &x); if( odd(x)) printf("%d is odd number. \n", x); else printf("%d is even number. \n", x); }

5 範例三: #define 依照輸入數值列出其平方值, 若數值小於50時, 則程式結束 #define TRUE 1
#define FALSE 0 #define SQ(x) (x * x) void main() { int num; int again = 1; printf("\1: Program will stop if input value less then 50. \n"); while(again) printf("\1: Please input number ==> "); scanf("%d", &num); printf("\2: The square for this number is %d \n", SQ(num)); if(num >= 50) again = TRUE; else again = FALSE; }

6 範例四: #include 使用定義在conio.h內的clrscr()函數 清除螢幕函數 #include <conio.h>
Void main() { clrscr(); } 清除螢幕函數

7 C語言標頭檔 alloc.h assert.h bios.h conio.h ctype.h dir.h dos.h errno.h
fcntl.h float.h graphics.h io.h limits.h math.h mem.h process.h setjmp.h share.h signal.h stdarg.h stddef.h stdio.h stdlib.h string.h time.h values.h

8 範例五: 常用函數介紹 簡易數學函數使用 #include <math.h> void main() {
double x = 8.0; printf("\2: exp(x) is --> %f \n", exp(x)); printf("\2: log(x) is --> %f \n", log(x)); printf("\2: log10(x) is --> %f \n", log10(x)); printf("\2: sqrt(x) is --> %f \n", sqrt(x)); }

9 範例六: 常用函數介紹 應用亂數函數設計猜數字遊戲 #include <stdlib.h>
#include <time.h> void main() { int num, guess, count=0; randomize(); num = random(80) + 1; do printf("Enter your guess (1-80) : "); scanf("%d", &guess); count ++; if( guess > num ) printf(" Too high ...\n"); else if( guess < num) printf(" Too low ... \n"); else printf(" Bingo ! \n"); } while (( guess != num) && ( count < 5)); if (guess != num) printf(" Too bad. The number is %d \n", num); printf(" You are lucky !! \n"); 應用亂數函數設計猜數字遊戲


Download ppt "Introduction to the C Programming Language"

Similar presentations


Ads by Google