1. 程式人生 > >用內聯取代宏代碼

用內聯取代宏代碼

truct clu ogr his esp 匯編語言 using program 預處理

用內聯取代宏代碼

C++ 語言支持函數內聯,其目的是為了提高函數的執行效率(速度)。 在 C 程序中,可以用宏代碼提高執行效率。宏代碼本身不是函數,但使用起來象函 數。預處理器用復制宏代碼的方式代替函數調用,省去了參數壓棧、生成匯編語言的 CALL 調用、返回參數、執行 return 等過程,從而提高了速度。使用宏代碼最大的缺點是容易 出錯,預處理器在復制宏代碼時常常產生意想不到的邊際效應。

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop 
*/ 4 using namespace std; 5 int main(int argc, char** argv) { 6 //定義結構類型 7 struct human { 8 char name[10]; 9 int sex; 10 int age; 11 }; 12 13 //聲明結構數組和結構指針變量,並初始化 14 human x[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL; 15 16 //用下標變量的輸出結構數組的元素
17 for (int i=0;i<3;i++) 18 { 19 cout<<x[i].name<<\t; 20 cout<<x[i].sex<<\t; 21 cout<<x[i].age<<endl; 22 } 23 cout<<"----------------"<<endl; 24 25 //用結構指針輸出結構數組的元素 26 for (p=x;p<=&x[2];p++) 27
{ 28 cout<<p->name<<\t; 29 cout<<p->sex<<\t; 30 cout<<p->age<<endl; 31 } 32 return 0; 33 }

用內聯取代宏代碼