This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
C++ void return type of main()
What is the proper declaration of main?
Simple question, really.
My friend and I are perusing the Powerpoint slides of a professor we are supposed to be hearing next semester. It will be a Java course. For some reason, he has this C++ code snippet
#include <iostream.h>
main ()
{ cout << "Hello, World\n"; }
I have told my friend, "No, this won't work with any modern C++ compiler."
My question is now, can this compile at all?
It could, sure.
Consider, for example, if <iostream.h> was a header with the following contents:
#include <iostream>
using std::cout;
#define main int main
That is not standard C++. The list of issues is quite long for a piece of code that short... probably because it comes from ages ago and an old non-conforming compiler.
The proper name of the include header is #include <iostream>, the .h was dropped during the ANSI standarization.
Types must be explicitly stated in C++. You cannot declare a function without return type and get a default int (that is C, not C++).
The standard signatures for main are: int main(), and int main( int argc, char** ) (implementations can provide extra arguments, but the return type must be int)
cout is defined inside the std namespace and cannot be used without qualification unless you add a using.
The equivalent code in proper C++ would be
#include <iostream>
int main() {
std::cout << "Hello world\n";
}
This will compile, though any decent compiler should raise a warning. Since you're using #include <iostream.h>, the compiler assumes that this is old code and compiles it in backwards-compatible mode. On my machine, gcc says:
In file included from /usr/include/c++/4.2.1/backward/iostream.h:31,
from oldcpp.cpp:1:
/usr/include/c++/4.2.1/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated header.
Please consider using one of the 32 headers found in section 17.4.1.2 of the C++
standard. Examples include substituting the <X> header for the <X.h> header for C++
includes, or <iostream> instead of the deprecated header <iostream.h>.
To disable this warning use -Wno-deprecated.
But it still compiles it fine. On running the code, I get exactly what is expected: i.e.:
Hello, World
Is this even worth questioning? Why write corner-case C/C++? What's wrong with keeping with the standards? main() is the entry point of the program, and the OS expects a return code from the program. Now there are arguments whether void main() is acceptable, but why even defend it? Just use int main() and be a happy dude that writes good code:
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello World!\n";
return 0;
}
I start every program with this (without the HW! then) and never ever had any issues.
Related
I've been puzzled by this for a while. To test this out, I made a simple program that just creates a std::string variable and prints it out to the screen. However, it doesn't include <string.h>.
#include <iostream>
using namespace std;
int main()
{
string name = "Test";
cout << name << endl;
return 0;
}
What confuses me is that this program compiles and runs perfectly. Right now I'm using the clang compiler that came with the XCode Developer Tools. Is this intended behavior? I hope this question isn't too ridiculous since I just started learning C++.
The reason you do not need to include the #include <string.h> header file is because when you include the #include <iostream> header file it includes std::string.
However, do not rely on it. What may work on your compiler may not work on another. Always include the proper header files.
To edit your example this is how you should use it:
#include <iostream>
#include <string>
int main()
{
std::string name = "Test";
std::cout << name << std::endl;
return 0;
}
Also note: why you should not use using namespace std;.
Why does my program compile successfully if I don't include <string.h>?
Because you don't use any definition / declaration from <string.h>.
program compiles and runs perfectly ... Is this intended behavior?
It is incidental behaviour.
There are no guarantees that one standard header wouldn't include other standard headers. It just so happens that <iostream> included <string> in this particular version of the standard library. Since there is no guarantee for this, it would be a mistake to rely on such transitive inclusion.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No such file iostream.h when including
Even after naming the source file with .cpp extension. my compiler gives this error, both in command prompt and Codeblocks. How can I fix this issue?
#include <iostream.h>
int main(){
cout<<"Hello World!\n";
return 0;
}
That header doesn't exist in standard C++. It was part of some pre-1990s compilers, but it is certainly not part of C++.
Use #include <iostream> instead. And all the library classes are in the std:: namespace, for example std::cout.
Also, throw away any book or notes that mention the thing you said.
Using standard C++ calling (note that you should use namespace std for cout or add using namespace std;)
#include <iostream>
int main()
{
std::cout<<"Hello World!\n";
return 0;
}
You should be using iostream without the .h.
Early implementations used the .h variants but the standard mandates the more modern style.
This question already has answers here:
When using C headers in C++, should we use functions from std:: or the global namespace?
(8 answers)
Closed 5 years ago.
To my knowledge, headers of the form cxyz are identical to xyz.h with the only difference being that cxyz places all of the contents of xyz.h under the namespace std. Why is it that the following programs both compile on GCC 4.9 and clang 6.0?
#include <cstdio>
int main() {
printf("Testing...");
return 0;
}
and the second program:
#include <cstdio>
int main() {
std::printf("Testing...");
return 0;
}
The same goes for the FILE struct:
FILE* test = fopen("test.txt", "w");
and
std::FILE* test = std::fopen("test.txt", "w");
both work.
Up until now, I always thought that it was better to use cstdio, cstring, etc, rather than their non-namespaced counterparts. However, which of the following two programs above are better practice?
The same goes for other C functions such as memset (from cstring), scanf (also from cstdio), etc.
(I know some people will ask why I am using C IO in a C++ program; the issue here is not specifically C IO, but whether or not this code should compile without specifically specifying std:: before calling a namespaced C function.)
The standard permits the compiler to also inject the names into the global namespace.
One reason for this is that it permits the implementation of <cstdio> to be:
#include <stdio.h>
namespace std
{
using ::printf;
using ::fopen;
// etc.
}
so the compiler/library vendor does not have to write and maintain so much code.
In your own code, always use std:: or using namespace std; etc. so that your code is portable to compilers which do not inject the names into global namespace.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No such file iostream.h when including
Even after naming the source file with .cpp extension. my compiler gives this error, both in command prompt and Codeblocks. How can I fix this issue?
#include <iostream.h>
int main(){
cout<<"Hello World!\n";
return 0;
}
That header doesn't exist in standard C++. It was part of some pre-1990s compilers, but it is certainly not part of C++.
Use #include <iostream> instead. And all the library classes are in the std:: namespace, for example std::cout.
Also, throw away any book or notes that mention the thing you said.
Using standard C++ calling (note that you should use namespace std for cout or add using namespace std;)
#include <iostream>
int main()
{
std::cout<<"Hello World!\n";
return 0;
}
You should be using iostream without the .h.
Early implementations used the .h variants but the standard mandates the more modern style.
I want to use "Dev-C++" for compile c++ codes.
So I download and install it, and write this code:
#include <iostream.h>
main () {
cout << "124";
}
but when I compiled it, it said:
In file included from
E:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
from [myfile path]\Untitled1.cpp:1:
E:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2:
warning: #warning This file includes
at least one deprecated or antiquated
header. Please consider using one of
the 32 headers found in section
17.4.1.2 of the C++ standard. Examples include substituting the header
for the header for C++ includes,
or instead of the
deprecated header . To
disable this warning use
-Wno-deprecated.
After I saw errors, I change my code to this code:
#include <iostream>
main () {
cout << "124";
}
but it said again that errors.
I compile first code easily in Turbo C++, BUT in Dev-C++ ...
What can I do?
First, make sure you write out the full definition of main, including the int return type. Leaving out the return type is an old, antiquated practice which doesn't fly these days.
Second, in the new-style headers—the ones missing the .h extension—the standard library is under the std namespace. There are two ways to make your program work:
1. Add an std:: qualifier to cout.
#include <iostream>
int main () {
std::cout << "124";
}
2. Add a using declaration to allow unqualified references to the std namespace.
#include <iostream>
using namespace std;
int main () {
cout << "124";
}
Make sure you put int in front of main () {
I believe any C/C++ program's main() function is required by POSIX and the appropriate language standards to return an int (someone correct me if I'm wrong).
EDIT: Also, be sure to include using namespace std; above int main ().