Thread: Return value

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    Return value


    i would like to know what it really means when the program did not return a value of ZERO? my friend said that it has an errors in that case. but the program is running fine, only that it did not return a value of zero, and in case it means errors, how to debug?


    Code:
    #include <stdio.h>
    #include <conio.h>
      
    main() 
    {
     	int var1,var2,cntr;
    
            printf("INPUT A NUMBER: ");
     	scanf("%d", &cntr);
    
            for(var1=1;var1<=cntr;var1++) 	
            {
                 for(var2=1;var2<=cntr;var2++)
    		{
                          printf("X"); 		
                    }
                    printf("\n");
     	} 	
            getch(); 
    }
    below is the image, showing that the program exited with a return value of 13
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,388
    Rep Power
    1871
    Try being explicit and ending main with

    return 0;

    rather than just falling off the end with whatever garbage value happens to be around at that moment.

    FWIW, it seems your return value from main is the return value from getch.

    While you're at it, get used to writing main like this
    Code:
    int main ( ) {
        // your code
        return 0;
    }
    The 'implicit int' rule of C is gone in the latest standard.
    Last edited by salem; March 3rd, 2013 at 03:31 AM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by salem
    Try being explicit and ending main with

    return 0;

    rather than just falling off the end with whatever garbage value happens to be around at that moment.

    FWIW, it seems your return value from main is the return value from getch.

    While you're at it, get used to writing main like this
    Code:
    int main ( ) {
        // your code
        return 0;
    }
    The 'implicit int' rule of C is gone in the latest standard.


    im just about to post that i have already figure it out but then you happened to post first XD

    thank you for answering my query anyway,very much appreciated =)
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    The value returned from main is passed to the operating system, this allows tools and scripts that run a program to determine whether the program ran successfully or otherwise. By convention returning zero means success, the meaning of any other value is application specific.

    It is important to conform to the convention since you don't necessarily know how your program might be used. The utility make for example relies on this convention to detect build failures.

IMN logo majestic logo threadwatch logo seochat tools logo