C語言中 EXIT_FAILURE和EXIT_SUCCESS

一、C語言中 宏EXIT_FAILURE和EXIT_SUCCESS定義在頭文件stdlib.h中,是一個符號常量,定義以下:spa

#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0

二、做用操作系統

  EXIT_FAILURE 能夠做爲exit()或return的參數來使用,表示沒有成功的執行一個程序。當你在程序中輸入一個非法值,他可使計算機退出整個程序,終止進程,並返回1給操做系統。code

  EXIT_SUCCESS做爲exit()或return的參數來使用,表示成功地執行一個程序,並返回0給操做系統。blog

三、程序案例進程

 

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5         float m;
 6         float f_a=1;
 7         float l_a;
 8         printf("Enter a number:");
 9         scanf("%f",&m);
10         if(m<0)
11         {       printf("Cann't compute the square root of a negative number!\n");
12                 return EXIT_FAILURE;
13         }
14 
15         do{
16                 l_a=(f_a+m/f_a)/2;
17                 printf("%f.15\n",l_a);
18         }while(l_a!=f_a);
19 
20         printf("Square root of %f is %f\n",m,l_a);
21 
22         return EXIT_SUCCESS;
23 }

運行結果:it

輸入非法值:Enter a number:-5
        Cann't compute the square root of a negative number!


io

相關文章
相關標籤/搜索