來源:互聯網網民 2008-06-01 01:59:09
評論1、調試標記
適用預處理#define定義一個或多個調試標記,在代碼中把調試部分使用#ifdef和#endif進行治理。當程序最終調試完成後,只需要使用#undef標記,調試代碼就會消失。常用的調試標記爲DEBUG, 語句序列:
#define DEBUG
#ifdef DEBUG
調試代碼
#endif
2、運行期間調試標記
在程序運行期間打開和關閉調試標記。通過設置一個調試bool標記可以實現。這對命令行運行的程序更爲方便。例如下面代碼:
#include<iostream>
#include <string>
using namespace std;
bool debug =false;
int main(int argc,char*argv[])
{
for(int i=0;i<argc;i++)
if(string(argv[i])==「--debug=on「)
debug = true;
bool go=true;
while(go)
{
if(debug)
{
調試代碼
}else {}
}
}
3、把變量和表達式轉換成字符串
可是使用字符串運算符來實現轉換輸出定義
#define PR(x) cout<<#x」=」<<x<<'\n'
4、c語言的assert()
該宏在中,,當使用assert時候,給他個參數,即一個判讀爲真的表達式。預處理器産生測試該斷言的代碼,假如斷言不爲真,則發出一個錯誤信息告訴斷言是什麽以及它失敗一會,程序會終止。
#include< assert>
using namsapce std;
int main()
{
int i=100;
assert(i!=100);
//Fails
}
當調試完畢後在#include<assert>前
加入#define NDEBUG即可消除紅産生的代碼
} 更多內容請看C/C++進階技術文檔 Office技巧專區專題,或
1、調試標記
適用預處理#define定義一個或多個調試標記,在代碼中把調試部分使用#ifdef和#endif進行治理。當程序最終調試完成後,只需要使用#undef標記,調試代碼就會消失。常用的調試標記爲DEBUG, 語句序列:
#define DEBUG
#ifdef DEBUG
調試代碼
#endif
2、運行期間調試標記
在程序運行期間打開和關閉調試標記。通過設置一個調試bool標記可以實現。這對命令行運行的程序更爲方便。例如下面代碼:
#include<iostream>
#include <string>
using namespace std;
bool debug =false;
int main(int argc,char*argv[])
{
for(int i=0;i<argc;i++)
if(string(argv[i])==「--debug=on「)
debug = true;
bool go=true;
while(go)
{
if(debug)
{
調試代碼
}else {}
}
}
3、把變量和表達式轉換成字符串
可是使用字符串運算符來實現轉換輸出定義
#define PR(x) cout<<#x」=」<<x<<'\n'
4、c語言的assert()
該宏在中,,當使用assert時候,給他個參數,即一個判讀爲真的表達式。預處理器産生測試該斷言的代碼,假如斷言不爲真,則發出一個錯誤信息告訴斷言是什麽以及它失敗一會,程序會終止。
#include< assert>
using namsapce std;
int main()
{
int i=100;
assert(i!=100);
//Fails
}
當調試完畢後在#include<assert>前
加入#define NDEBUG即可消除紅産生的代碼
} [url=/bbs/detail_1785132.html][img]http://image.wangchao.net.cn/it/1323424617719.gif[/img][/url] 更多內容請看C/C++進階技術文檔 Office技巧專區專題,或