top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Do we always need to return from the main function?

+1 vote
462 views

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?

posted Jul 3, 2014 by Meenal Mishra

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

2 Answers

+1 vote

Default signature of main function is
int main(void) or int main(int argc, char *argv[])
when user use one of the above mentioned prototype, it tells that main return exist status to Operating system.
If programmer has nothing to return to O.S, he/she can typecast explicitly. I mean to say in place of int main , can use void main.

answer Jul 4, 2014 by Rupam
+1 vote

When your program becomes a process then OS/script like to see the return value of the process so that it can take the corrective action for that purpose you can return as return 0 or return 1. That is the reason why we have return type as int in the main function.

FYI 1 is treated as success and 0 as failure.

answer Jul 4, 2014 by Salil Agrawal
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

This was asked today in my interview and I was not having any clue. Any pointer would be helpful.

+2 votes

1,1,2,2,2,6,6,6,7,7,7,7,7,7,7,8,8,9,9,9

Example:
Input = 1 Output=0 (First index of 1).
Input = 2 Output=2 (First index of 2).
Input = 6 Output= 5 (First index of 6).
Input = 7 Output= 8 (First index of 7).
Input = 8 Output=15 (First index of 8).
Input = 9 Output=17 (First index of 9).

+7 votes

Where we need to use volatile variable? If possible please provide some real time example ?

...