It's UWAweek 49


This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.
Displaying the 3 articles in this topic
Showing 3 of 1168 articles.
Currently 5 other people reading this forum.

 UWA week 33 (2nd semester, week 4) ↓
SVG not supported

Login to reply

4:01am Wed 16th Aug, Christopher M.

Someone sent in this question by email:
> I observe that we always include exit(EXIT_SUCCESS) and exit(EXIT_FAILURE) in the main function in our C programs. Is it a common practice as well as a requirement for our project assignments that we should always include these two exits in our main function?
The main() function may return (to the operating system) by calling the exit() function, simply calling return, or 'dropping off' the bottom curly bracket. In the last 2 cases, no call to exit() is required at all. We an assume that the anticipated exit status of a process is success - which we can indicate with exit(EXIT_SUCCESS) or even return EXIT_SUCCESS. A program that can never fail, presumably a very simple one or one that doesn't check enough things, will not even have a call to exit(EXIT_FAILURE). And there's a program, named false, that only ever exits with failure, never with success. So there's no general rule, nor requirement, about how any calls to exit() a program must have.

SVG not supported

Login to reply

12:47pm Sat 19th Aug, Rory C.

Is there a reason to use exit() over return to terminate a program, or are they equivalent in behaviour?

 UWA week 34 (2nd semester, week 5) ↓
SVG not supported

Login to reply

7:40am Sun 20th Aug, Christopher M.

"Rory Cusworth" <23*7*5*4@s*u*e*t*u*a*e*u*a*> wrote:
> Is there a reason to use exit() over return to terminate a program, or are they equivalent in behaviour?
There is a (rare) case where you may need to consider their difference: - exit() terminates the current process, no matter where (from which function) it's called. If exit() is called from main() the whole process terminates. - return shifts execution to the function that called the current one. If main() was (seemingly) called from the operating-system then the return acts as if to terminate the current process. However, if main() was called by another function in the same process, perhaps a recursive call, then control returns to that calling function.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  5:07AM Sep 06 2023
Privacy policy