Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:51:33+00:00 2026-06-05T06:51:33+00:00

Possible Duplicate: What should main() return in C/C++? Difference between void main and int

  • 0

Possible Duplicate:
What should main() return in C/C++?
Difference between void main and int main?

I have always been using the main method in C like

void main(){ // my code }

and it works pretty well for me.
I also know about the other int return type:

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

But I have not been able to find any resource that says that I can use void as a return type. Every book suggests that the return type must be int or else it be omitted. Then why does void main() work?

Is this dependent on the version of C that I am using? Or does it work because I use a C++ IDE? Please reply specific to C and not C++.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:51:35+00:00Added an answer on June 5, 2026 at 6:51 am

    Only book authors seem to be privy to the place where a return type of void for main() is allowed. The C++ standard forbids it completely.

    The C standard says that the standard forms are:

    int main(void) { ... }
    

    and

    int main(int argc, char **argv) { ... }
    

    allowing alternative but equivalent forms of declaration for the argument types (and the names are completely discretionary, of course, since they’re local variables to the function).

    The C standard does make small provision for ‘in some other implementation defined manner’. The ISO/IEC 9899:2011 standard says:

    5.1.2.2.3 Program termination

    If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument;11) reaching the } that terminates the
    main function returns a value of 0. If the return type is not compatible with int, the
    termination status returned to the host environment is unspecified.

    11) In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in main
    will have ended in the former case, even where they would not have in the latter.

    This clearly allows for non-int returns, but makes it clear that it is not specified. So, void might be allowed as the return type of main() by some implementation, but you can only find that from the documentation.

    (Although I’m quoting C2011 standard, essentially the same words were in C99, and I believe C89 though my text for that is at the office and I’m not.)

    Incidentally, Appendix J of the standard mentions:

    J.5 Common extensions

    The following extensions are widely used in many systems, but are not portable to all
    implementations. The inclusion of any extension that may cause a strictly conforming
    program to become invalid renders an implementation nonconforming. Examples of such
    extensions are new keywords, extra library functions declared in standard headers, or
    predefined macros with names that do not begin with an underscore.

    J.5.1 Environment arguments

    In a hosted environment, the main function receives a third argument, char *envp[],
    that points to a null-terminated array of pointers to char, each of which points to a string
    that provides information about the environment for this execution of the program
    (5.1.2.2.1).

    Why does void main() work?

    The question observes that void main() works. It ‘works’ because the compiler does its best to generate code for programs. Compilers such as GCC will warn about non-standard forms for main(), but will process them. The linker isn’t too worried about the return type; it simply needs a symbol main (or possibly _main, depending on the system) and when it finds it, links it into the executable. The start-up code assumes that main has been defined in the standard manner. If main() returns to the startup code, it collects the returned value as if the function returned an int, but that value is likely to be garbage. So, it sort of seems to work as long as you don’t look for the exit status of your program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Difference between void main and int main? Alright, so I'm using bloodshed
Possible Duplicate: What should main() return in C/C++? What does main return? I've been
Possible Duplicate: Should Usings be inside or outside the namespace sa1200 All using directives
Possible Duplicate: Should C++ eliminate header files? In languages like C# and Java there
Possible Duplicate: Why should I bother about serialVersionUID? I am using a class that
Possible Duplicate: When to use try/catch blocks? Main method code entirely inside try/catch: Is
Possible Duplicate: void * arithmetic Hi guys I have a small question regarding pointer
Possible Duplicate: When should I do certain SQLite operations on another thread(not the main
Possible Duplicate: Error handling in C code What return value should you use for
Possible Duplicate: Using global exception handling with “setUncaughtExceptionHandler” and “Toast” I have implemented UncaughtExceptionHandler

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.