Programing

exit () 함수 사용

lottogame 2020. 8. 29. 11:48
반응형

exit () 함수 사용


exit()내 책의 프로그램과 같은 기능 을 언제 어떻게 사용할 수 있는지 알고 싶습니다.

#include<stdio.h>

void main()
{
    int goals;
    printf("enter number of goals scored");
    scanf("%d",&goals);

    if(goals<=5)
        goto sos;
    else
    {
        printf("hehe");
        exit( );
    }
    sos:
    printf("to err is human");
}

실행하면 ERROR : call to undefined function exit () 표시 됩니다.

또한 프로그램이 실행되는 창을 닫는 옵션을 만드는 방법을 알고 싶습니다. 예를 들어, 몇 가지 옵션이있는 메뉴 기반 프로그램을 만들었으며 그중 하나는 "메뉴 종료" 였습니다. 프로그램을 종료하려면 어떻게해야합니까 (예 : 창 닫기)?


exit(0);대신 사용해보십시오 . exit함수는 정수 파라미터를 기대한다. 그리고 잊지 마세요 #include <stdlib.h>.


exit함수는 stdlib 헤더에 선언되어 있으므로

#include <stdlib.h>

를 사용할 수 있도록 프로그램 상단에 있습니다 exit.

또한 exit정수 인수를 사용하므로로 호출 할 수 없으므로 또는 exit()로 호출해야합니다 . 0은 일반적으로 프로그램이 성공적으로 완료되었음을 의미하며 0이 아닌 값이 오류 코드로 사용됩니다.exit(0)exit(42)

사전 정의 된 매크로 EXIT_SUCCESSEXIT_FAILURE, 예도 있습니다.exit(EXIT_SUCCESS);


exit(int code);에 선언되어 stdlib.h있으므로

#include <stdlib.h>

또한 :
-에 대한 매개 변수가 없으므로 매개 변수를 제공 exit()해야합니다 int.
-이 책을 태워라. 그것은 goto(리눅스 커널 해커를 제외한 모든 사람에게) 나쁘고, 매우, 매우 , 매우 나쁘다.

편집 :
아, 그리고

void main()

또한 나쁘다.

int main(int argc, char *argv[])

man exit를 시도하십시오 .


아, 그리고 :

#include <stdlib.h>

int main(void) {
  /*  ...  */
  if (error_occured) {
    return (EXIT_FAILURE);
  }
  /*  ...  */
  return (EXIT_SUCCESS);
}

The exit() function is a type of function with a return type without an argument. It's defined by the stdlib header file.

You need to use ( exit(0) or exit(EXIT_SUCCESS)) or (exit(non-zero) or exit(EXIT_FAILURE) ).


The following example shows the usage of the exit() function.

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

int main(void) {
    printf("Start of the program....\n");
    printf("Exiting the program....\n");
    exit(0);
    printf("End of the program....\n");
    return 0;
}

Output

Start of the program....
Exiting the program....


You must add a line with #include <stdlib.h> to include that header file and exit must return a value so assign some integer in exit(any_integer).


In addition to return an exit code to parent process -

In UNIX, an important aspect that I think has been left out is, that exit() at first calls (in reverse order) all those functions, which were registered by atexit() call.

Please refer to SUSv4 for details.


on unix like operating systems exit belongs to group of system calls. system calls are special calls which enable user code (your code) to call kernel code. so exit call makes some OS specific clean-up actions before returning control to OS, it terminates the program.

#include <stdlib.h>

// example 1
int main(int argc, char *argv){
  exit(EXIT_SUCCESS);
}

// example 2
int main(int argc, char *argv){
  return 0;
}

Some compilers will give you the same opcode from both of these examples but some won't. For example opcode from first function will not include any kind of stack positioning opcode which will be included in the second example like for any other function. You could compile both examples and disassemble them and you will see the difference.

You can use exit from any part of your code and be sure that process terminates. Don't forget to include integer parameter.


Write header file #include<process.h> and replace exit(); with exit(0);. This will definitely work in Turbo C; for other compilers I don't know.


Bad programming practice. Using a goto function is a complete no no in C programming.
Also include header file stdlib.h by writing #include <iostream.h>for using exit() function. Also remember that exit() function takes an integer argument . Use exit(0) if the program completed successfully and exit(-1) or exit function with any non zero value as the argument if the program has error.


Include stdlib.h in your header, and then call abort(); in any place you want to exit your program. Like this:

switch(varName)
{
    case 1: 
     blah blah;
    case 2:
     blah blah;
    case 3:
     abort();
}

When the user enters the switch accepts this and give it to the case 3 where you call the abort function. It will exit your screen immediately after hitting enter key.


Use process.h instead of stdlib and iostream... It will work 100%.

참고URL : https://stackoverflow.com/questions/2425167/use-of-exit-function

반응형