69,373
社区成员
发帖
与我相关
我的任务
分享
/***
*assert.h - define the assert macro
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* Defines the assert(exp) macro.
* [ANSI/System V]
*
* [Public]
*
****/
#include <crtdefs.h>
#undef assert
#ifdef NDEBUG
#define assert(_Expression) ((void)0)
#else
#ifdef __cplusplus
extern "C" {
#endif
_CRTIMP void __cdecl _wassert(_In_z_ const wchar_t * _Message, _In_z_ const wchar_t *_File, _In_ unsigned _Line);
#ifdef __cplusplus
}
#endif
#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
#endif /* NDEBUG */
#undef NDEBUG //取消定义
#include <assert.h> //assert定义
int main(void)
{
int a = 0;
assert(1);
assert(a == 0);
assert(a != 1);
#define NDEBUG
#include <assert.h>
assert(a == 0);
assert(a != 1);
#undef NDEBUG //取消定义
#include <assert.h> //定义
assert(a == 0);
assert(a != 1);
assert(0);
return 0;
}
在最后一次包含assert.h的时候由于取消定义了NDEBUG,所辖下面的预处理操作被包含在main函数体中,这是错误的.
#undef assert
#ifdef NDEBUG
#define assert(_Expression) ((void)0)
#else
#ifdef __cplusplus
extern "C" { //在main函数中extern "C" 语法错误.
#endif