Prashant | Sun, 14 Jun, 2020 | 130
In this tutorial we will learn what is the difference bertween exit() and return() statement ? Is bith statement same in main() function ?
No, exit() is a pre-define library function of stdlib.h, whereas return is a jumping statement and it is a keyword which is defined in the compiler.
exit() terminates the program's execution and returns the program's control to the operating system or thread which is calling the program (main() function).
return returns the program's control to the calling function from called function.
If you use, return in the main() function, it transfers program's control from main() (called function) to the operating system (calling function).
So, in case of main() function, exit() and return, both will work same.
An example with exit()
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("statement-1\n");
printf("statement-2\n");
exit(0);
printf("statement-N\n");
return 0;
}
//output
statement-1
statement-2
An example with return
#include <stdio.h>
int main()
{
printf("statement-1\n");
printf("statement-2\n");
return -1;
printf("statement-N\n");
return 0;
}
//Output
statement-1
statement-2
1. Introduction And Getting Started With C
3. Why We Should Use C Language
4. Applications Of C Programming
5. Basic Rules For Writting C Program
8. Tokens In C
9. Difference Between Int Main And Void Main
11. Variables In C
13. Difference Between Local And Global Variable
14. Difference Bwtween Auto / Extern / Static Variable
15. Constant In C
16. How To Access Global Variable Using Extern Keyword In C
17. Exit And Return Staterment In C
18. Print Float Value Upto N Decimals In C Programming
19. How To Print Multiline Message Using Single Printf In C Programming ?
20. What Value Returned By Scanf Function In C Language ?
21. What Value Is Returned By Printf And Scanf In C