+ 3

what if I choose to return none zero value to main in cpp

i tried to return 1 and -1 with return statment in cpp their is nothing affect my simple programm which is just work fine only the excution window tells me that the main had returned the pre specified value i tried to re build the file before excute everytime i change the value of return my question is where or when none zero vlaue will cause a problem to my code??? and is there a place where i need to use this none zero value in programming ?? my simple lines #include <iostream> using namespace std; int main (void) { /* this programm include my attempt day one in my cpp quest */ /*calculate the pages per day for cpp essentials book */ int total_pages = 311; int total_quest_days =7; float total_days_per_day; total_days_per_day = total_pages/total_quest_days; cout<< "pages per day is =" << total_days_per_day << endl; return -1; }

10th Jul 2020, 4:13 PM
Elaph Alabasy
Elaph Alabasy - avatar
8 Answers
+ 10
Return 0 means the program has executed successfully and return 1 means the program terminated with an error. This is returned to the parent process. Usually any non zero value in return statement means the program has terminated with an error. The value returned by main is called as exit status of that program. It is customary practice used since many years that if a program reaches end of the program with out any difficulty, its exit status is made as 0 otherwise it will be made as a negative number. Usually, for each possible reason for a program’s failure some number is assigned and reported in the manual page. Also, for each possible failure, a symbolic constant is defined in the header files that are related to that program. For example, in unix/Linux echo $? command can be used to know the exit status of most recent program. .......... Thankyou.........
12th Jul 2020, 3:42 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
If you do not return a zero then you will have to keep on coding because returning a zero in the main is a sign of showing that l now have zero work to do and everything you write after the zero won't be read by the compiler Note returning zero just tells the compiler you are done hope it helps
11th Jul 2020, 11:54 PM
Pjc
Pjc - avatar
+ 2
The application which ran your program (for example a command prompt) has to explicitly test your returned value to detect it. In sololearn playground It Is not possibile.
10th Jul 2020, 5:01 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 2
Bilbo Baggins i use codeblocks ide and gcc compiler i like always when learning new things is play around and try to make the more mistakes i can to explore everything possible
10th Jul 2020, 6:40 PM
Elaph Alabasy
Elaph Alabasy - avatar
+ 1
Yes,it will not effect your code. We use return 0 if the code successfully run. For debugging, if the code not successfully run we use return 1.Other option we normally don"t use.
10th Jul 2020, 5:01 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
Elaph Alabasy IMHO you are following the best way to learn👍
10th Jul 2020, 6:53 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
For more info, anyway, there is a dedicated entry on Wikipedia: https://en.m.wikipedia.org/wiki/Exit_status
10th Jul 2020, 7:01 PM
Bilbo Baggins
Bilbo Baggins - avatar