1

As we know that:-

exit() performs following operations.

  • Flushes unwritten buffered data.
  • Closes all open files.
  • Removes temporary files.
  • Returns an integer exit status to the operating system.

And

abort() may not close opened files. It may also not delete temporary files and may not flush stream buffer. Also, it does not call functions registered with atexit().

Source:- https://www.geeksforgeeks.org/understanding-exit-abort-and-assert/

So my question is What happens when a program terminates without these two functions (normally)?

I hope you got my question...

Abhishek Jaiswal
  • 288
  • 2
  • 14

1 Answers1

0

According to the C11 standard (well, the n1570 draft linked in another question), Section 5.1.2.2.3 Program termination states that the behavior is unspecified.

Quoting the C11 draft:

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;11) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

As far as I know, this behavior has been the same since pretty much the inception of C.

If you're ever faced with this situation, it's better to treat this scenario as a bug that needs fixing, unless you're willing to dive into whatever is the behavior specified by your compiler/toolchain/framework.

RAM
  • 2,257
  • 2
  • 19
  • 41
  • I think this is a good answer. The part I think that answers the question (as I read it) is that "a return from the initial call to the main function is equivalent to calling the exit function ..." Returning a type that's not compatible with int doesn't seem to be relevant to the question as far as I can see. – Paul Hankin May 02 '21 at 10:07
  • My take is that a C program is either terminated with a call to `exit()`, an explicit `return` from `main`, or its control flow just drops from `main` without an explicit call to either `exit()` or `return`. The reference I've provided explains the latter case, which is the only possibility left open by the question.. – RAM May 02 '21 at 10:11
  • 1
    OK, but the standard text that you've quoted says that dropping off the end of main is the same a returning 0. That's not a bug or unspecified behavior. – Paul Hankin May 02 '21 at 10:16
  • @PaulHankin you might be right here. The semicolon bundled with the ref led me to read the paragraph as it all referred to the explicit call to `exit()`, leading me to interpret it as anything that does't involve explicit returns or exits is unspecified. – RAM May 02 '21 at 10:20
  • 2
    Claiming the behaviour is unspecified is wrong. The first sentence of the quote specifies the behavior precisely. It's only if the return type of `main()` is not compatible with `int` that there is unspecified behaviour. The quote is good. The interpretation is wrong. – Jonathan Leffler May 02 '21 at 13:05