Can we use "system("pause")" in place of "return0" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we use "system("pause")" in place of "return0"

18th Nov 2016, 1:27 PM
Haider Zaidi
Haider Zaidi - avatar
4 Answers
+ 3
They are two completely different things. Return from main essentially means you're ending the program, and the value returned indicates whether or not the program ran as intended (0 is default, meaning success). system("pause") is a Windows OS function in the windows.h header, thus will only work when run on devices running Windows. Like Shupack said, if you're running your program as a console application (under project > properties), then it will always add a "press any key to continue" statement, otherwise what's the point in having a console application if it just instantly disappears? Generally, you'd want to stay away from any functions relating to system unless absolutely necessary. These are functions, well, built in to the system, in this case Windows, and can pose a security threat if used too often. An example of why this is, is if the computing running the program was infected with a malware that has modified certain system files. You could tell your program to run system("pause"), but if the malware has changed the definition of this function to something completely different, then generally that's not something you want chilling in your programs on a daily basis. This is moreso if you plan to distribute your program, as you cannot be sure that users of your program are malware-free, AND running Windows.
18th Nov 2016, 3:43 PM
Cohen Creber
Cohen Creber - avatar
+ 1
Never use void main. On *most* compilers, it won't even let you. int main is C++ standard; just stay with it. Return and system("pause") do not even even perform a similar function, so I don't understand how you're saying one works and the other doesn't?
18th Nov 2016, 4:21 PM
Cohen Creber
Cohen Creber - avatar
0
System is a function that takes the string of the command and forwards it to your terminal. Depending on the terminal you are using "pause" command could be valid or not. Anyway, pausing the console is done automatically on most compilers and return 0 should be enough and it is good practice to always state what value is main function returning. But on windows machines you can use system( "PAUSE") although i would add return 0 at the end.
18th Nov 2016, 2:17 PM
shupack
0
I was making a program using void command so return value isn't working but system pause works.
18th Nov 2016, 4:16 PM
Haider Zaidi
Haider Zaidi - avatar