|
C++ Primer For Java Programmers The Main Function<< | Table of Contents | Variables And Data Types >>
All programs must have a starting point for execution. In both Java and C++, the first statement executed is the first statement within the program's
// Hello world program in C++.
#include <cstdio>
int main( int argc, char *argv[] )
{
printf( "Hello World!\n" );
}
and the equivalent in Java. (:source lang=java:)
// Hello world program in Java.
class MyProgram {
public static void main( String [] args )
{
system.out.println( "Hello World!" );
}
}
The two arguments to the main C++ routine are used to access the command-line arguments. If your program has no need for command-line arguments, you can use the following version of the main routine instead. (:source lang=cpp:)
// Alternate form of the main() C++ routine.
#include <cstdio>
int main()
{
printf( "Hello World!\n" );
}
|