Latest web development tutorials

C 庫函數– abort()

C 標準庫 - <stdlib.h> C標準庫- <stdlib.h>

描述

C庫函數void abort(void)中止程序執行,直接從調用的地方跳出。

聲明

下面是abort() 函數的聲明。

void abort(void)

參數

  • NA

返回值

該函數不返回任何值。

實例

下面的實例演示了abort() 函數的用法。

#include <stdio.h>
#include <stdlib.h>

int main ()
{
   FILE *fp;
   
   printf("准备打开 nofile.txt\n");
   fp = fopen( "nofile.txt","r" );
   if(fp == NULL)
   {
      printf("准备终止程序\n");
      abort();
   }
   printf("准备关闭 nofile.txt\n");
   fclose(fp);
   
   return(0);
}

讓我們編譯並運行上面的程序,這將產生以下結果,因為我們嘗試打開的文件nofile.txt是不存在的:

准备打开 nofile.txt
准备终止程序

C 標準庫 - <stdlib.h> C標準庫- <stdlib.h>