Вопрос

I'm studying C.

If I've:

void fun(){
      exit(0);
}

void main(){
    /*instructions*/
    fun();
    /*other-instructions*/
}

When the function "fun" is called, I've exit(0). Once exit(0) is executed, it closes everything? Or other-instructions are executed? Which is the difference between exit(0) and exit(-1)? Do I have to #include something to use exit function?

Thanks for you help!

Это было полезно?

Решение 2

void exit (int status);

Terminate calling process

Terminates the process normally, performing the regular cleanup for terminating programs.

Note that objects with automatic storage are not destroyed by calling exit (C++).

If status is zero or EXIT_SUCCESS, a successful termination status is returned to the host environment. If status is EXIT_FAILURE, an unsuccessful termination status is returned to the host environment. Otherwise, the status returned depends on the system and library implementation.

For a similar function that does not perform the cleanup described above, see quick_exit.

Parameters

status-Status code.

   If this is 0 or EXIT_SUCCESS, it indicates success.
   If it is EXIT_FAILURE, it indicates failure. 

Return Value

none (the function never returns).

Example

/* exit example */
#include <stdio.h>      /* printf, fopen */
#include <stdlib.h>     /* exit, EXIT_FAILURE */

int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","r");
  if (pFile==NULL)
  {
    perror ("myfile.txt");
    exit (EXIT_FAILURE);
  }
  else
  {
    /* file operations here */
  }
  return 0;
}

Другие советы

Usually, exit(n) exits your program, without running any more of your code and makes the programs "status code" (the error number seen in the shell) equal to n (although the high-order bits might be ignored).

More precisely, exit() will do some clean-up stuff for the C library. In particular it will call shutdown functions that you have registered using atexit (see the documentation for more detail). Finally it calls the _exit() system call which transfers control to the kernel and tells the kernel to stop the calling process. The kernel will free the process memory, shutdown files and sockets etc.

exit() exits from the program, not just from the current function so other instructions will not be executed.
What number you pass to exit is called the return status. you can read this to know about exit function in C

It terminates the program, means that no further execution will be done. 0 means that your program did terminate without any error and anything in the range of 1-255 means that your program did terminate with errors. you have to include stdlib.h to use it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top