| | |
Return 0;
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
What exactly does the "Return 0;" indicate in this function?
I know my code will not even compile without it, but I have just been putting it in out of habit without realizing that I do not even know what it does. I was under the assumption that it meant this particular function returned a value of 0, or contained no parameters, but in this case that does not make sense to me.
Can somebody help me out?
I know my code will not even compile without it, but I have just been putting it in out of habit without realizing that I do not even know what it does. I was under the assumption that it meant this particular function returned a value of 0, or contained no parameters, but in this case that does not make sense to me.
Can somebody help me out?
C Syntax (Toggle Plain Text)
if (store <1 || store >3) //if store number is not 1, 2 or 3 { printf("\nPlease enter 1,2 or 3\n"); printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows retry scanf("%c", &cAgain); //scans input at INVALID prompt if (cAgain == 'y' || cAgain == 'Y')//repeats calculator { goto YES; //back to top } else if (cAgain == 'n' || cAgain == 'N')//end return 0; }
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
Sorry
Here is the entire program. Notice the last two loops. Return 0; completes them both. What does that accomplish?
Here is the entire program. Notice the last two loops. Return 0; completes them both. What does that accomplish?
C Syntax (Toggle Plain Text)
#include <stdio.h> // standard input output library //defines tax value for calculations #define DelMar 7.25 #define Encinitas 7.5 #define LaJolla 7.75 char cAgain; //variable for Y/N questions int displayMenu() //defines menu variable { // menu int selection; //defines selection variable printf("Tax Calculation for $125 Purchase\n\n"); printf("1. Del Mar \n"); printf("2. Encinitas \n"); printf("3. La Jolla \n"); printf("\n\nPlease Select Store for Which to Calculate [1-3]:"); scanf("%d",&selection); return selection; } int main() //main loop { YES://start again point { float sales = 125.00; // Sales value for calculations int store; //defines store for main loop, menu will not work without store = displayMenu(); //displays menu created above if (store == 1) //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, DelMar, sales*DelMar/100); //values are correct so I am leaving it this way. ahead of schedule here. if (store == 2) //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, Encinitas, sales*Encinitas/100); if (store == 3) //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax printf("\nLa Jolla \tSale $%.2f\tTax %.2f%%\tTax $%.2f%\t\n\n",sales, LaJolla, sales*LaJolla/100); if (store <1 || store >3) //if store number is not 1, 2 or 3 { printf("\nPlease enter 1,2 or 3\n"); printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows retry scanf("%c", &cAgain); //scans input at INVALID prompt if (cAgain == 'y' || cAgain == 'Y')//repeats calculator { goto YES; //back to top } else if (cAgain == 'n' || cAgain == 'N')//end return 0; } printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat scanf("%c", &cAgain);//scans input at store number prompt if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator { goto YES; //back to top } else if (cAgain == 'n' || cAgain == 'N')//end return 0; } }
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
the main() program can often be called by a parent program. 'return 0;' indicates to the caller (parent) that the main() program executed correctly.
so... generally speaking, the main function must be of type 'int' and therefore must return some integer value on completion. zero (0) is the standard "successful" return value (think: returned with zero errors) although it could be some other value that indicates an error state, depending on the caller
if main does not return a value (like the case of 'void main()' ) then you are now in the realm of "undefined behavior" when the program is called by another. Undefined behavior is almost always a Bad Thing.
.
so... generally speaking, the main function must be of type 'int' and therefore must return some integer value on completion. zero (0) is the standard "successful" return value (think: returned with zero errors) although it could be some other value that indicates an error state, depending on the caller
if main does not return a value (like the case of 'void main()' ) then you are now in the realm of "undefined behavior" when the program is called by another. Undefined behavior is almost always a Bad Thing.
.
Last edited by jephthah; Aug 5th, 2009 at 2:54 am.
•
•
Join Date: Mar 2009
Posts: 36
Reputation:
Solved Threads: 7
•
•
•
•
If I remove it my program does not compile. That is why I was asking. Was not sure what it was for, but I know it had to be there.
Thanks guys.
2. You can remove them(return 0) without any errors, but else if has to be managed and you can simply put a semicolon at the end of else if to tell it not to do anything.
3. There is also an issue of '\n' in the keyboard buffer with scanf, so you have to modify it as well to make the program completely executable.
4. switch case is advisable in your code.
5. avoid goto.
Last edited by 9868; Aug 6th, 2009 at 1:40 am.
![]() |
Similar Threads
- Return Array from C++ (C++)
- problem with VAT and return copmbo box (Visual Basic 4 / 5 / 6)
- Return Length of a string? (C++)
Other Threads in the C Forum
- Previous Thread: can u please solve
- Next Thread: Reading char from stdin without waiting for input
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






