Reputation: 8152
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
Reputation: 18009
exit()
function causes process termination.exit()
function does not return. return 1
;Upvotes: 1
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
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
Reputation: 15229
The program will exit before encountering the return 1;
statement.
Upvotes: 1