Home Interview Questions and AnswersTechnical Interview Questions and AnswersC Programming C Functions Interview Questions and Answers For Freshers Part-3

c-functions9. Is using exit() the same as using return?

No. The exit() function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit() function are similar. Here is an example of a program that uses the exit() function and return statement:

#include <stdio.h>

#include <stdlib.h>

int
main(
int
,
char
**);

int
do_processing(
void
);

int
do_something_daring();

int
main(
int
argc,
char
** argv)

{

int
ret_code;

if
(argc <
3
)

{

printf(
“Wrong number of arguments used!\n”
);

/* return 1 to the operating system */

exit(
1
);

}

ret_code = do_processing();

/* return 0 to the operating system */

exit(
0
);

}

int
do_processing(
void
)

{

int
rc;

rc = do_something_daring();

if
(rc == ERROR)

{

printf(
“Something fishy is going on around here…”
\n);

/* return rc to the operating system */

exit(rc);

}

/* return 0 to the calling function */

return

0
;

}

In the main() function, the program is exited if the argument count (argc) is less than 3. The statementexit(1); tells the program to exit and return the number 1 to the operating system. The operating system can then decide what to do based on the return value of the program.

You may also like

Leave a Comment