Find the answer to your Linux question:
Results 1 to 7 of 7
how come sometimes I see "int main()", sometimes I see "void main()" or just plain "main()" .. what's the difference? also, "return (0)" defines the errorlevel when the program finishes. ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    14

    Question about C

    how come sometimes I see "int main()", sometimes I see "void main()" or just plain "main()" .. what's the difference?

    also, "return (0)" defines the errorlevel when the program finishes. right?

    thanks

  2. #2
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    the variable type in front of a function is what kind of variable the program returns.
    This should be int since you want to return Exit codes (eg 0 successfull run)
    if you do void you always return 0 and therefore this is bad practice.

    Hope that helped
    New Users, please read this..
    Google first, then ask..

  3. #3
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    don't forget about your parameters

    Code:
    int main(int argc, char **argv)

  4. #4
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    However the int argv, char *argv[] isnt required. its recommended
    New Users, please read this..
    Google first, then ask..

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,961
    Back in the "old" days of K&R C, you could define main() as returning void, but that is now deprecated and should be defined to return an int value - the exit code for the application. The following are valid signatures for main() these days:

    int main(void);
    int main(int argc, char** argv);
    int main(int argc, char* argv[]);
    int main(int argc, const char** argv);
    int main(int argc, const char* argv[]);

    The second and third versions of the argument list with char* types is allowed, provided you want to modify the actual contents of the arguments at runtime (not recommended). So, the first and last two versions are preferred - the first only when you don't allow any arguments, and just want to ignore them if passed from a shell.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Linux User
    Join Date
    Jan 2006
    Posts
    414
    void main() is still ok if you're writting a kernel though, as main should never return in a kernel, or at the very least returning anything is pointless.

  7. #7
    Just Joined!
    Join Date
    Nov 2009
    Posts
    1
    Quote Originally Posted by oospill View Post
    how come sometimes I see "int main()", sometimes I see "void main()" or just plain "main()" .. what's the difference?

    also, "return (0)" defines the errorlevel when the program finishes. right?

    thanks
    It is bcoz in linux OS it takes int main () defaiult as int return type to the OS when main terminates.
    Also if you not specify any return type i,e main() only then compiler will explicitly add int data type to the program and compiler......

    And also return(0) is very common that main() also a function and it has int return type so, return(0) tells the linux kernel that program is terminated normally.........

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...