exit()子程序终止函数与return()函数的差别
在main函数中我们通常使用return (0);这样的方式返回一个值。
但这是限定在非void情况下的也就是void main()这样的形式。
exit()通常是用在子程序中用来终结程序用的,使用后程序自动结束跳会操作系统。
但在如果把exit用在main内的时候无论main是否定义成void返回的值都是有效的,并且exit不需要考虑类型,exit(1)等价于return (1)
#include <iostream>
#include <string>
using namespace std;
int main()
{
exit (1);//等价于return (1);
}
如果以abort函数(此函数不带参数,原型为void abort(void))终止程序,则会在debug模式运行时弹出所示的对话框。
Debug Error!
Program: D:/vcproject/exception/exception/Debug/exception.exe
abnormal program termination
(Press Retry to debug the application)
终止 重试 忽略
断言(assert)
assert宏在C语言程序的调试中发挥着重要的作用,它用于检测不会发生的情况,表明一旦发生了这样的情况,程序就实际上执行错误了,例如strcpy函数:
char *strcpy(char *strDest, const char *strSrc)
{
char * address = strDest;
assert((strDest != NULL) && (strSrc != NULL));
while ((*strDest++ = *strSrc++) != '/0')
;
return address;
}
其中包含断言assert( (strDest != NULL) && (strSrc != NULL) ),它的意思是源和目的字符串的地址都不能为空,一旦为空,程序实际上就执行错误了,会引发一个abort。
assert宏的定义为:
#ifdef NDEBUG
#define assert(exp) ((void)0)
#else
#ifdef __cplusplus
extern "C"
{
#endif
_CRTIMP void __cdecl _assert(void *, void *, unsigned);
#ifdef __cplusplus
}
#endif
#define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0) )
#endif /* NDEBUG */
如果程序不在debug模式下,assert宏实际上什么都不做;而在debug模式下,实际上是对_assert() 函数的调用,此函数将输出发生错误的文件名、代码行、条件表达式。例如下列程序:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char * myStrcpy( char *strDest, const char *strSrc )
{
char *address = strDest;
assert( (strDest != NULL) && (strSrc != NULL) );
while( (*strDest++ = *strSrc++) != '/0' );
return address;
}
int main(void)
{
myStrcpy(NULL,NULL);
return 0;
}
在此程序中,为了避免我们的strcpy与C库中的strcpy重名,将其改为myStrcpy。程序的输出
Assertion failed:(strDest != NULL) && (strSrc != NULL). file
D:/vcproject/exception/exception/main.c line?
abnormal program termination
失败的断言也会弹出如上所示的对话框,这是因为_assert()函数中也调用了abort()函数。
一定要记住的是assert本质上是一个宏,而不是一个函数,因而不能把带有副作用的表达式放入assert的"参数"中。
errno
errno在C程序中是一个全局变量,这个变量由C运行时库函数设置,用户程序需要在程序发生异常时检测之。C运行库中主要在math.h和stdio.h头文件声明的函数中使用了errno,前者用于检测数学运算的合法性,后者用于检测I/O操作中(主要是文件)的错误,例如:
#include <errno.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
errno = 0;
if (NULL == fopen("d://1.txt", "rb"))
{
printf("%d", errno);
}
else
{
printf("%d", errno);
}
return 0;
}
在此程序中,如果文件打开失败(fopen返回NULL),证明发生了异常。我们读取error可以获知错误的原因,如果D盘根目录下不存在"1.txt"文件,将输出2,表示文件不存在;在文件存在并正确打开的情况下,将执行到else语句,输出0,证明errno没有被设置。