Home » C programming language

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



Learn: What is the difference between exit() and return statement in C language? Is both statements are 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





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.