無論是身處學(xué)校還是步入社會(huì),大家都嘗試過寫作吧,借助寫作也可以提高我們的語言組織能力。范文怎么寫才能發(fā)揮它最大的作用呢?以下是小編為大家收集的優(yōu)秀范文,歡迎大家分享閱讀。
c語言assert的用處篇一
c語言中的assert(斷言)宏是嵌入式軟件開發(fā)人員可以使用的最好的'調(diào)試工具之一。以下是百分網(wǎng)小編搜索整理的關(guān)于c語言中assert的用法,需要的朋友可以參考一下!想了解更多相關(guān)信息請持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!
assert宏的原型定義在<assert.h>中,其作用是如果它的條件返回錯(cuò)誤,則終止程序執(zhí)行,原型定義:
assert的作用是現(xiàn)計(jì)算表達(dá)式 expression ,如果其值為假(即為0),那么它先向stderr打印一條出錯(cuò)信息,
然后通過調(diào)用 abort 來終止程序運(yùn)行。
代碼如下:
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
int main( void )
{
file *fp;
fp = fopen( "", "w" );//以可寫的方式打開一個(gè)文件,如果不存在就創(chuàng)建一個(gè)同名文件
assert( fp ); ? ? ? ? ? ? ? ? ? ? ? ? ? //所以這里不會(huì)出錯(cuò)
fclose( fp );
fp = fopen( "", "r" );//以只讀的方式打開一個(gè)文件,如果不存在就打開文件失敗
assert( fp ); ? ? ? ? ? ? ? ? ? ? ? ? ? //所以這里出錯(cuò)
fclose( fp ); ? ? ? ? ? ? ? ? ? ? ? ? ? //程序永遠(yuǎn)都執(zhí)行不到這里來
return 0;
}
功 能: 測試一個(gè)條件并可能使程序終止
用 法: void assert(int test);
代碼如下:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct item {
int key;
int value;
};
/* add item to list, make sure list is not null */
void additem(struct item *itemptr) {
assert(itemptr != null);
/* add item to list */
}
int main(void)
{
additem(null);
return 0;
}
assert宏的原型定義在<assert.h>中,其作用是如果它的條件返回錯(cuò)誤,則終止程序執(zhí)行,原型定義:
代碼如下:
#include <assert.h>
void assert( int expression );
assert的作用是先計(jì)算表達(dá)式expression,如果其值為假(即為0),那么它先向標(biāo)準(zhǔn)錯(cuò)誤流stderr打印一條出錯(cuò)信息,然后通過調(diào)用abort來終止程序運(yùn)行;否則,assert()無任何作用。宏assert()一般用于確認(rèn)程序的正常操作,其中表達(dá)式構(gòu)造無錯(cuò)時(shí)才為真值。完成調(diào)試后,不必從源代碼中刪除assert()語句,因?yàn)楹阯debug有定義時(shí),宏assert()的定義為空。
s("content_relate");
【c語言中assert的用法有哪些】相關(guān)文章:
c語言中assert用法
10-03
c語言中的assert用法
10-07
c語言assert的用法有哪些
11-24
c語言中int的用法有哪些
10-05
c語言中indexof的用法有哪些
10-04
assert用法(c語言)
11-24
c語言中default的用法
10-05
c語言中free的用法
10-04
c語言中bit的用法
10-04
c語言中sscanf的用法
09-28
【本文地址:http://www.aiweibaby.com/zuowen/2797650.html】