|
功 能 |
|
|
檔案的含入 |
#include |
|
字串的置換/巨集定義 |
#define / #undef |
|
條 件 編 譯 |
#if .... #elif .... #else .... #endif
#ifdef (#ifndef) .... #else .... #endif |
| Example: #include < stdio.h > #include "def.h" main() { printf("PI=%2.5f\n",PI); printf("a+b=%d\n",a+b); } |
-- def.h 的檔案內容 -- #define PI 3.14159 #define a 10 #define b 20 ---------------------- 執行結果 : PI=3.14159 a+b=30 |
| Example1: #include < stdio.h >#define PI 3.14159 main() { printf("PI=%2.5f",PI); } |
執行結果 : PI=3.14159 |
| Example2 : #include < stdio.h > #define add(a,b) a+b void main(void) { int i,j,k; i=2; j=3; k=add(i,j); printf("i+j=%d\n",k); } 執行結果 : i+j=5 |
Example3: #include < stdio.h >#define pstring "This is my first C program.\n" void main(void) { printf(pstring,1); } 執行結果 : This is my first C program. |
| Example4 : #include < stdio.h > #define compare(a,b) \ if ( a > b ) \ printf("a > b"); \ else \ if ( a < b ) \ printf("a < b");\ else \ printf("a = b"); void main(void) { int a,b; a=5; b=10; compare(a,b); } 執行結果 : a < b |
Example5 : #include < stdio.h > #define TURBOC void main(void) { #ifdef TURBOC printf("Borland C compiler.\n"); #endif #undef TURBOC #ifndef TURBOC printf("MircoSoft C compiler.\n"); #endif } 執行結果 : Borland C compiler.MircoSoft C compiler. |
| Example1: #include < stdio.h > #define value 99 void main(void) { #if value < 100 printf("value < 100"); #else printf("value >= 100"); #endif } 執行結果 : value < 100 |
Example2: #include < stdio.h > #define value 100 void main(void) { #if value < 100 printf("value < 100"); #elif value > 100 printf("value > 100"); #else printf("value = 100"); #endif } 執行結果 : value = 100 |
| 歡迎光臨 中央論壇 - CENTER BBS (https://centerbbs.com/) | Powered by Discuz! X3 |