UtkarshPramodGupta
UtkarshPramodGupta

Reputation: 8152

If exit(0) is there before return 1, will return1 execute?

What happens in the follwing code, does program executes 'return 1' or exits before?

int cmd_quit(tok_t arg[]) {
  printf("Bye\n");
  exit(0);
  return 1;
}

Upvotes: 0

Views: 98

Answers (4)

sergej
sergej

Reputation: 18009

  • The exit() function causes process termination.
  • The exit() function does not return.
  • Your program will not reach return 1;

Upvotes: 1

Weather Vane
Weather Vane

Reputation: 34575

The function exit will be executed before return but it is usually used for an abnormal exit and returns a failure code to the caller. So it would be better to reverse the exit values

exit(1);
return 0;

But in the code you provide, it would make sense to detect an actual failure.

if (printf("Bye\n") <= 0)
    exit(1);
return 0;

Upvotes: 0

user2371524
user2371524

Reputation:

exit() ends the program, no matter what. So: no. In practice, a C runtime will always do something similar of

exit(main(argc, argv));

to execute your program.

(meaning: if main() returns, exit() will be called automatically with its return value)

Upvotes: 3

cadaniluk
cadaniluk

Reputation: 15229

The program will exit before encountering the return 1; statement.

Upvotes: 1

Related Questions