国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言調試手段:鎖定錯誤的實現方法

2020-02-24 14:27:00
字體:
來源:轉載
供稿:網友

在編程過程中,有時我們編寫的程序在發生邏輯錯誤時不能運行正確的結果,這就要求我們調試一些容易出錯的語句,下文是武林技術頻道小編為大家介紹的C語言調試手段:鎖定錯誤的實現方法,一起來看看吧!
首先來了解一下文件默認的輸出信息的函數吧:
文件信息函數:

?

printf("line : %d/n", __LINE__);?????????????????? //當前行數
printf("filename : %s/n", __FILE__);???????????? //當前文件名
printf("function : %s/n", __FUNCTION__);? //當前函數
printf("time : %s/n", __TIME__);????????????????? //當前時間
printf ("date : %s/n",? __DATE__);????????????? //當前日期
輸出:
line : 10
filename : test.c
function : main.c
time : 14:13:51
date : Oct 13 2012


理論已足,那就來看看如何鎖定錯誤吧:
一、源文件:

?

?

?


[root@localhost for_test]# cat erroutput.c
#include <stdio.h>
#include <assert.h>
#define _DEBUG(msg...)??? printf("[ %s,%s, %d ]=>",__FILE__, __FUNCTION__, __LINE__);? printf(msg);printf("/r/n")
#define _ERROR(msg...)??? printf("[ error: %s, %d]=>", __FILE__,? __LINE__);printf(msg); printf("/r/n")
#define _ASSERT(exp)????? /
??????????????????????? do {/
??????????????????????????????? if (!(exp)) {/
??????????????????????????????? printf( "[ %s ]? ",#exp);printf("/r/n");/
??????????????????????????????? assert(exp);/
??????????????????????????????? }/
??????????????????????? } while (0)
int main(void)
{
??????? char *p = NULL;
??????? _DEBUG("DEBUG!");
??????? _ERROR("ERROR!");
??????? _ASSERT(NULL != p);
??????? return 0;
}


二、輸出:

?

?

?


[root@localhost for_test]# gcc erroutput.c
[root@localhost for_test]# ./a.out
[ erroutput.c,main, 17 ]=>DEBUG!
[ error: erroutput.c, 18]=>ERROR!
[ NULL != p ]
a.out: erroutput.c:19: main: Assertion `((void *)0) != p' failed.
已放棄


TI處理:

?

?

?


#ifdef DEBUG
??? #define DBG(fmt, args...)? printf("Debug " fmt, ##args)// ##運算符用于把參數連接到一起。預處理程序把出現在##兩側的參數合并成一個符號。
#else
??? #define DBG(fmt, args...)
#endif
#define ERR(fmt, args...)? printf("Error " fmt, ##args)
[root@localhost for_test]# cat debug_err.c
#include <stdio.h>
//#define DEBUG
int main(void)
{
?????? DBG("xxxx/n");
?????? ERR("xxxx/n");
?????? return 0;
}
[root@localhost for_test]# ./a.out
Error xxxx

#ifdef __DEBUG
??? #define DBG(fmt, args...) fprintf(stderr,"Encode Debug: " fmt, ## args)
#else
??? #define DBG(fmt, args...)
#endif
#define ERR(fmt, args...) fprintf(stderr,"Encode Error: " fmt, ## args)

以上就是C語言調試手段:鎖定錯誤的實現方法,相信這些內容對大家都很有用,建議你可以好好收藏我們的專業的網站js.Vevb.com。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 潼南县| 内丘县| 遂溪县| 滦南县| 抚州市| 喀什市| 甘谷县| 乌恰县| 涟水县| 汉中市| 孟连| 尉氏县| 高台县| 突泉县| 东兴市| 新乡市| 珲春市| 水城县| 华坪县| 安塞县| 钟祥市| 于都县| 黎城县| 普格县| 杭锦后旗| 沭阳县| 崇阳县| 曲水县| 苏尼特左旗| 安阳市| 利川市| 壤塘县| 曲阳县| 格尔木市| 额济纳旗| 四子王旗| 耒阳市| 鸡泽县| 磴口县| 疏附县| 太仆寺旗|