File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes C / C++ and the fly likes why there is a compile time error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "why there is a compile time error " Watch "why there is a compile time error " New topic
Author

why there is a compile time error

abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130



error : declaration terminated incorrectly

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
I presume the misplaced semicolon is intentional.
Brian Overland
author
Ranch Hand

Joined: Sep 09, 2011
Posts: 36
Hey, there should be no semi-colon after "main"!! And delcare main either "int" or "void." (See examples below.)

Hopefully my book never ever ever commits this error... I checked the code by compiling it myself many times.

BUT... I feel for you if you are a new programmer! Unlike the various forms of Basic, C++ does use semi-colons and you have to know where to put them. (Hey, no offensive joke replies to that, please. Be nice.) You put semi-colons at the ends of statements.

"main" is the name of a function. It is not, itself, a statement. However, you can have statements inside main -- you can even (technically legal though not terribly useful in most cases) have an empty statement by placing a semi-colon inside of main(). Here is a version of main that has one statement that does nothing.
OH, and by the way(!)... main must be declared either "int" or "void". If you don't want to return a value, declare it void! So the following compiles on most C++ compilers:

void main() {
;
}

Increasingly, however, it is considered more "standard" to always declare main() with a return value. So you would write this:

int main() {
return 0;
}

Why return 0? Often it does not matter, but the return value of main is a code to the operating system that indicates success or failure. 0 means success.

Hope this helps,

Brian Overland
Anthony Aj Williams
author
Ranch Hand

Joined: Jun 10, 2011
Posts: 56
Brian Overland wrote:And delcare main either "int" or "void."
... main must be declared either "int" or "void". If you don't want to return a value, declare it void!


Unfortunately, this is incorrect. 3.6.1p2 of the C++ standard (both the 2003 and 2011 editions) says:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementation-defined. All implementations shall
allow both of the following definitions of main:

and



So a return type of void is a non-standard extension, though it is supported by many C++ compilers. I would not therefore recommend it to anyone in new code.

Also, main is special in that though it has a return type of int, it does not need a return statement. Reaching the end of main is equivalent to return 0;, so a minimal main function is:



Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Brian Overland
author
Ranch Hand

Joined: Sep 09, 2011
Posts: 36
Ok, AJ, I will concede you are correct that this is most current standard all programmers ought to come up to. Yes, main() ought to be declared as having type int, with references to argument lists optional (argc, argv). My book is 100% consistent with that principle in all the code examples, and the argument list stuff is covered in Ch 9.

However, the original question on the thread was asking why this didn't work...

main(); {
}

It's certainly not going to work with a semi-colon right after "main"... and the basic use of semi-colons in C++ is a fundamental syntax issue that programmers absolutely have to get right.

But yeah, main really ought to be declared "int" even though in my experience "void" has been supported forever by all the compilers I've ever used. But ok, everyone: let's remember to declare main "int".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
It is quite possible the compiler is out of date, to permit void main().
nitin soman
Greenhorn

Joined: Nov 08, 2011
Posts: 3
Sorry to intervene. But isnt void main() legal for embedded systems(by embedded systems i mean microntrollers)? The question because, i understand that the value needs to returned back to the caller(the OS), but isnt embedded systems an exception to the rule since in some of the cases it doesnot have an OS and in some other cases the system may never return, meaning it is some kind of endless, for loop. So my question is whether it is illegal for embedded systems to use void main(void)? If so why?
Brian Overland
author
Ranch Hand

Joined: Sep 09, 2011
Posts: 36
A couple of things:

(1) Once again, the thread's original question was why this didn't work:

main; {
}

The single biggest error here is that the semi-colon must not come right after the word "main". As to the other issues...



(2) I have read and re-read the C++ spec carefully on the subject of "int main" extra-carefully. Everyone is correct that "void main" is simply not standard -- it is not supported by the spec -- but what the specification does say is this: The following two forms, and only these forms, are standard, in that every C++ compiler must support these two:

int main()

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

It then says that handling of other return types is compiler-defined -- that is, the implementation may CHOOSE to accept other types. (And yes, as has been pointed out, this is dangerous and risky, even if so many compilers accept "void main".) The question here is not one of what might seem reasonable, but rather what the spec says. The C++ specification further states that if the main function reaches the end without a return statement, the compiler is to interpret the function as if it ended with:

return 0;

In other words, returning 0 is the default behavior. What is misleading here is that if you don't read the C++ spec, you might infer that "void" function behavior is supported for main, especially as quite a few compilers do accept "void main," not flagging it as any kind of error. What these compilers are really doing is permitting the declaration and then interpreting it as if it were declared "int main". main may therefore behave like a void function even though it really has int return type! Confused yet?

The moral, as has been pointed out, is that "void main" is useless and unnecessarily risky as someday you may try porting your code to a compiler that doesn't allow "void main" and flags it as an error. In summary, here is probably what the original questioner intended:

int main {
;
}

Note the semi-colon denotes an empty statement and must come WITHIN a function, never outside. That is very, very basic, of course. And yeah, everyone should just always declare main as "int" to be safe. It turns out that there is nothing to be gained from ever using "void main", even though it happens to be accepted by a lot of compilers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
int main(){;} surely?

That shows how much easier a language is to understand if it has a more restrictive specification.
Joshua Mccartney
Greenhorn

Joined: Apr 25, 2012
Posts: 26
int main()
{
return 0;
}

or

int main(void)
{

}

main() is a function.. use main(void) if you don't want to return any value.

Find the latest job vacancies from the top employers here at QuickList.
We are the prime job site for Filipinos.
Amit Mmf Shrivastava
Greenhorn

Joined: Oct 30, 2012
Posts: 4

Hello,

I would like to suggest the same thing that is already brief by Brian as:

Brian Overland wrote:Hey, there should be no semi-colon after "main"!! And declare main either "int" or "void." (See examples below.)

Hopefully my book never ever ever commits this error... I checked the code by compiling it myself many times.

BUT... I feel for you if you are a new programmer! Unlike the various forms of Basic, C++ does use semi-colons and you have to know where to put them. (Hey, no offensive joke replies to that, please. Be nice.) You put semi-colons at the ends of statements.

"main" is the name of a function. It is not, itself, a statement. However, you can have statements inside main -- you can even (technically legal though not terribly useful in most cases) have an empty statement by placing a semi-colon inside of main(). Here is a version of main that has one statement that does nothing.
OH, and by the way(!)... main must be declared either "int" or "void". If you don't want to return a value, declare it void! So the following compiles on most C++ compilers:

void main() {
;
}

Increasingly, however, it is considered more "standard" to always declare main() with a return value. So you would write this:

int main() {
return 0;
}

Why return 0? Often it does not matter, but the return value of main is a code to the operating system that indicates success or failure. 0 means success.

Hope this helps,

Brian Overland


So the Brian is right. Thanks Brian too for good explanation.

Regards
Amit
Java Training Indore


PHP Training Indore | Java Training Indore | .Net Training Indore
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why there is a compile time error
 
Similar Threads
Regarding Interface Concept
Intercept a 404 Error
Servlet to Applet communication
String concentation
Dynamic images in jsp