• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

why there is a compile time error

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



error : declaration terminated incorrectly

 
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I presume the misplaced semicolon is intentional.
 
author
Posts: 36
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
Brian Overland
author
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is quite possible the compiler is out of date, to permit void main().
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int main(){;} surely?

That shows how much easier a language is to understand if it has a more restrictive specification.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int main()
{
return 0;
}

or

int main(void)
{

}

main() is a function.. use main(void) if you don't want to return any value.
 
Greenhorn
Posts: 4
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic