Importance of return value from main function in C

By | 03/09/2012

Most of the times people who are new to software programming know the importance of a return value from any normal function as it can be easily seen how a returned value is being handled by the caller function but generally these newbies are not aware of the importance of returning appropriate value from main function in C as one does not have the visibility of returned value from main() function. So, In this brief article, we will understand the importance of return value from main function in C by using a practical example.

Return value from main function in C

The example code

Following is the example C code that we will use in this article:

#include<stdio.h>
#include<string.h>

#define SUCCESS 0
#define FAILURE -1

int main(void)
{
   char pass_buff[50] = {0};
   printf("\n Enter the password...");

   //Get the password from user
   fgets(pass_buff,sizeof(pass_buff)-1,stdin);

   //Make sure that the extra(last) character
   //picked up from stdin is washed off 
   //from buffer.
   pass_buff[strlen(pass_buff)-1] = '\0';

   if(! (strcmp(pass_buff,"Linux")))
   {
       // Passwords match
       printf("\n Passwords Match..SUCCESS\n");
       return SUCCESS;
   }
   else
   {
       // Passwords do not macth
       printf("\n Passwords do not match...FAILURE\n");
       return FAILURE;
   }
}

The code above is a very basic logic of a password screening software that matches the password entered by user with the actual password. On the basis of the matching result it either returns 0 or returns 1 from main() function.

Now if we compile and execute it using gcc, a sample run may look like :

$ gcc -Wall return.c -o return
$ ./return 

 Enter the password...Linux

 Passwords Match..SUCCESS

So we see that by providing a correct password, success is returned.

In Linux system programming, a general thumb rule is followed that if a function is successful then it returns 0 otherwise it returns a non-zero value. This rule is just a thumb rule and not a mandatory one as there are some standard functions that do not follow this rule but still, most of the standard functions follow it. In our case too, the main() function returns 0 in case of success and a non-zero value in case of failure.

In Linux, whatever value the main() function returns becomes the return value of the executable. This means that the value returned by the main() function of an executable can be accessed to determine whether the executable passed or failed.

This value can be accessed on shell using the ‘echo $?‘ command which outputs the return value of the last command run on shell. Lets use this command in case of our executable. Following is the output :

$ ./return 

 Enter the password...Linux

 Passwords Match..SUCCESS
$ echo $?
0

$ ./return 

 Enter the password...Unix

 Passwords do not match...FAILURE
$ echo $? 
1

$

So we see that the ‘echo $?’ command returned exactly the same value what main() would have returned in case of those inputs.

The most popular use of return values of executables is when they are used from within the scripts. A script when executes an executable, it mostly needs to determine whether the executable ran successfully or not.

A sample bash script for checking return value of our executable may look like :

#!/bin/bash
./return
if [ $? -eq 0 ] ; then
        echo "SCRIPT :: Program ran successfully"
else
        echo "SCRIPT :: Program failure"
fi

Now, when this script is run :

$ ./script.sh 

 Enter the password...Linux

 Passwords Match..SUCCESS
SCRIPT :: Program ran successfully

So we see that script was able to detect success of executable on the basis of its return value. So its always advisable to keep track of what is being returned from the main() function of an executable.

Note : I use Linux Mint with gcc compiler version 4.4.3

Leave a Reply

Your email address will not be published. Required fields are marked *