Exit And Return Staterment In C

Prashant | Sun, 14 Jun, 2020 | 130

Is exit() and return statements are same in C language?

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()

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

return returns the program's control to the calling function from called function.

In case of main() 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

 

 

 

 

 

 

Related Articles :- 

1Introduction And Getting Started With C

2Basic Of C Language

3. Why We Should Use C Language

4. Applications Of C Programming

5. Basic Rules For Writting C Program

6. Keywords In C Language

7. Comments In C Programming

8. Tokens In C

9. Difference Between Int Main And Void Main

10. C Processor And Macros

11. Variables In C

12. Types Of 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

22. What Do 'Lvalue' And 'Rvalue' Mean In C/C++?

23. Errors In C Programming ?

0 comments
Leave a comment