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

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 statement

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

C Language Tutorial »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.