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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.