Happy New Year!

I

Ioannis Vranos

#include <iostream>


int main()
{
using namespace std;

cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
}
 
J

Jon Wilson

Ioannis said:
#include <iostream>


int main()
{
using namespace std;

cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
}


This should give a compiler warning. You declare int main(), but have
no return statement.
 
M

Mike Wahler

Jon Wilson said:
This should give a compiler warning. You declare int main(), but have
no return statement.

No return statement is required. When it is not specified,
the program performs an implicit 'return 0'. This is the case
only for the 'main()' function.

-Mike
 
K

KPB

Florian said:
gcc without options wouldn't give a warning, but what about -Wall?

No. As others have said, the return statement is not needed.

This was one of those weird facts that I didn't know about until
recently. I just include the return statement without thinking about it.
I think I read this fact in one of the Josuttis books.

Also, I did try out the -Wall just to make sure.

To me, this is similar to the variable initialization issue. Some are
init'd implicitly (if you don't initialize them explicitly) and some
aren't. I just explicitly initialize everything then I don't have to
remember which are and which aren't.

KPB
 
V

Victor Bazarov

Jon Wilson said:
This should give a compiler warning. You declare int main(), but have no
return statement.

Actually that's not a problem (see other replies). However, a strictly
conforming implementation has to complain about 'endl' being undefined.
To find it, include <ostream> as well.

Happy New Year!

Victor
 
D

David Harmon

#include <iostream>


int main()
{
using namespace std;

cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
}

This should give a compiler warning. You declare int main(), but have
no return statement.[/QUOTE]

The compiler may give any warning it wants, but it shouldn't for
that. main() has a special exemption and is allowed to have no
return statement.

The output line should be
cout<<"Best wishes for a Great Happy New Year to all!\n";

It is useless and wasteful to use endl to flush the stream,
immediately before the program is going to end and flush it anyway.
 
M

Mike Wahler

KPB said:
No. As others have said, the return statement is not needed.

Correct (this only applies to 'main()'
This was one of those weird facts that I didn't know about until
recently. I just include the return statement without thinking about it.

Actually, I and others feel that this constitutes 'good
practice' anyway. But it is incorrect to state that
omitting it is invalid.
I think I read this fact in one of the Josuttis books.

It's also cited in many others.
Also, I did try out the -Wall just to make sure.

That's the wrong way to determine how the language should
work.
To me, this is similar to the variable initialization issue. Some are
init'd implicitly (if you don't initialize them explicitly) and some
aren't. I just explicitly initialize everything then I don't have to
remember which are and which aren't.

Aa matter of 'habit', I will usually initialize every
object I define, regardless of its storage duration.
But the issue of which objects are automatically
initialized and which are not is straightforward:

1) Static storage duration objects are automatically default initialized.
2) Objects with any other storage duration are not automatically
initialized.
3) ("Special case") If one or more of an array's elements are explicitly
initialized, any elements for which there are no corresponding
initializers
are default inititalized.


-Mike
 
A

adbarnet

...and - without some kind of <stdheader> include the compiler doesn't know
anything about the namespace 'std' in any case - so the using statement
should blow!

Indeed - Happy New Year!
 
I

Ioannis Vranos

Victor said:
Actually that's not a problem (see other replies). However, a strictly
conforming implementation has to complain about 'endl' being undefined.
To find it, include <ostream> as well.


I just checked the 2003 standard about endl. In all examples where it is
used, no <ostream> is included.
 
I

Ioannis Vranos

David said:
The output line should be
cout<<"Best wishes for a Great Happy New Year to all!\n";

It is useless and wasteful to use endl to flush the stream,
immediately before the program is going to end and flush it anyway.


It is redundant, however I do not think it is wasteful. :)
 
K

KPB

Mike said:
Correct (this only applies to 'main()'

Yes. That's what we're talking about here, right?

Actually, I and others feel that this constitutes 'good
practice' anyway. But it is incorrect to state that
omitting it is invalid.

Where did I state that it was incorrect to omit this?

It's also cited in many others.

Ok. I'm just telling you where I found it.

That's the wrong way to determine how the language should
work.

Yes but it answered a question from a previous poster. You took this out
of context as you seem to have done to the rest of my post.

Aa matter of 'habit', I will usually initialize every
object I define, regardless of its storage duration.
But the issue of which objects are automatically
initialized and which are not is straightforward:

1) Static storage duration objects are automatically default initialized.
2) Objects with any other storage duration are not automatically
initialized.
3) ("Special case") If one or more of an array's elements are explicitly
initialized, any elements for which there are no corresponding
initializers
are default inititalized.

Ok. You remember your rules and I'll just keep initializing everything.

KPB
 
M

Mike Wahler

KPB said:
Yes. That's what we're talking about here, right?



Where did I state that it was incorrect to omit this?



Ok. I'm just telling you where I found it.



Yes but it answered a question from a previous poster.

Which question?
You took this out
of context as you seem to have done to the rest of my post.

I don't think so. Whatever.
Ok. You remember your rules and I'll just keep initializing everything.

I also usually initialize everything. But imo it is good to
know the rules, especially when reading others' code.

-Mike
 
I

Ioannis Vranos

Jon said:
This should give a compiler warning. You declare int main(), but have
no return statement.


As others said, this is valid code, and thus no warning should be provided.
 
M

Mike Wahler

adbarnet said:
..and - without some kind of <stdheader> include the compiler doesn't know
anything about the namespace 'std' in any case

- so the using statement
should blow!

It's fine.

BTW please do not top-post.

-Mike
 
K

KPB

Mike said:
Which question?

Someone wondered if using the -Wall switch in gcc would generate a
warning. I told him no after trying it out. I knew it would'nt but there
was no harm in making sure.
I don't think so. Whatever.

Still don't think so? Ok, whatever.
I also usually initialize everything. But imo it is good to
know the rules, especially when reading others' code.

-Mike

I read other people's code all the time.

I have Stroustrup's book if I need to deal with anything like this. This
is just one thing that isn't important for me to remember because:

1. I don't see it enough in code to have to
2. Like I said.. I have reference materials if I do.

KPB
 
E

E. Robert Tisdale

Ioannis said:
#include <iostream>

int main()
{
using namespace std;

cout<<"Best wishes for a Great Happy New Year to all!"<<endl;
}
cat main.cc
#include <iostream>

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

std::cout << "And Happy New Year to you too Ioannis."
<< std::endl;

return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main
And Happy New Year to you too Ioannis.
 
I

Ioannis Vranos

KPB said:
This was one of those weird facts that I didn't know about until
recently. I just include the return statement without thinking about it.
I think I read this fact in one of the Josuttis books.

As I had read in some newsgroup, this implicit return 0; statement has
its roots in K&R 2.

In K&R2 the programs were in the style:


main()
{
/* ... */
}


which was not a valid C90 code anyway, but Kernighan used this style.

The C90 valid code was


// But not valid C++ and C99.
main()
{
/* ... */

return 0;
}


or


int main()
{
/* ... */

return 0;
}


Someday (probably in the C99 committee) one suggested, wouldn't it be
nice K&R 2 programs to become valid C99 programs?

Everyone agreed, making the return 0; implicit if no return statement is
provided.


However later, they decided that return types should be explicitly
provided (while in C90 const x; was equivalent to const int x; for
example), and thus we are today with int main() and no return statement
in both C99 and C++98.
 
K

KPB

Ioannis said:
As I had read in some newsgroup, this implicit return 0; statement has
its roots in K&R 2.

In K&R2 the programs were in the style:


main()
{
/* ... */
}


which was not a valid C90 code anyway, but Kernighan used this style.

The C90 valid code was


// But not valid C++ and C99.
main()
{
/* ... */

return 0;
}


Yes, I've seen these in some old C code and even some old C++ code
(old.. bad... code.. that I've corrected).

I've also seen the void main(). Have you?

Thanks,
KPB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top