top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What should main() return in C?

+4 votes
437 views

AFAIK it can have something void main or int main, the question is what is recommended way and why?

posted Oct 8, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

you can do something like this from the shell -

a = ./a.out

and the program which pass the return value of the a.out or the main function which can be helpful on deciding the success or failure. So int main is the right choice.

answer Oct 8, 2014 by Basabdatta Mukherjee
+1 vote

Return value of main() tells how the program exited.
If the return value is zero(EXIT_SUCCESS, define in "stdlib.h") it means that the execution was successful while any non zero value will represent that something went bad in the execution.

The default return type of main() is an "int" so its recommend to avoid the use of or better don't use it.
you can use or <main()> as the default return type is int.

For example:

 int main()
 {
      printf("Here to check\n");

      return 0;    // to validate change the return value to different ..
 }

compile the program.

 ./a.out      //execute your program.
 echo $?  //execute this after executing the above it will show the exit value of "a.out" 
answer Oct 13, 2014 by Arshad Khan
Similar Questions
+2 votes

If I correct , by default main () function has integer return type.
I want to know what happens in the system internally when its return type gets changed from integer to void ?

+1 vote

We have two types of main declaration i.e.

void main()
and
int main()

I want to know the difference between these two and when to use what?

+3 votes
#include <stdio.h>
int main()
{
    char vowels[5]={'a','e','i','o','u'};
    int x;

    printf("Vowel Letters");
    for(x=0;x<=5;x++)
    {
         printf("\n %C",vowels[x]);
    }
}

Output

Vowel Letters
a
e
i
o
u
♣
Process exited after 0.1132 seconds with return value 3

What is the meaning of return value 3

...