Tuesday, November 17, 2009

Writing your first program - Hello World


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

Enough words. Now it is time for some code. We are going to write our first program. Fire up your favorite text editor. We will write code in this simple editor till our work grows to several files. If you already have some IDE for the intended language, you can use them as well. Here we go-
  • C++
  • Java
  • C#
#include
using namespace std;

int main()
{
cout << "Hello World!\n"; }

namespace HelloWorldApp
{
public class HelloWorld
{
    public static void Main()
    {
        System.Console.WriteLine("Hello World!");
    }
}
}

Each program, when compiled, specifies a special function as the Entry Point. When a program starts, the platform looks for this function and all other functionalities must be reachable from this point. C++, Java and C# differ how they specify the entry point function, even though the standard entry point function is named main1 in all these languages.
  • C++
  • Java
  • C#
int main(void);
int main(int argc, char *argv[]);
public static void main(String[] args)
public static void main(String... args)
static void Main();
static void Main(string[] args);
static int Main();
static int Main(string[] args);
1. You can specify other function as Entry Point though this is very rare.

Compilation:
I have already discussed various compilers and IDEs for C++/Java/C# in the previous post. Now its time to compile and run our first program.
  • C++
  • Java
  • C#
Windows:
cl.exe helloworld.cpp
Run the program
helloworld.exe

Linux (GCC): There are 3 variants to compile a C++ program with gcc -
  1. gcc helloworld.cpp -lstdc++ -o helloworld
    gcc is usually used for C compilation.
    -lstdc++: compile in C++ mode.
    -o: indicates the output filename. If omitted, output filename is always a.out
  2. g++ helloworld.cpp -o helloworld
    uses gcc to compile in C++ mode.
  3. c++ helloworld.cpp -o helloworld
    Most systems install this program which is identical to g++.
Run the program
./helloworld
Sun Java:

javac HelloWorld

GCC (very rare):

gcj --main=HelloWorld HelloWorldApp.java -o HelloWorldApp

Run the program (Remember Java as well as Linux-File-System is case sensitive)
java HelloWorld
Windows (MS SDK):
csc.exe HelloWorld.cs
Run the program HelloWorld.exe

Linux(Mono):
gmcs HelloWorld.cs
Run the program ./HelloWorld.exe


Source Code for the Posts:
I have hosted a project in google code. The project page is cpp-java-csharp. Click the source tab. You can browse the source code by clicking browse tab or can download to your machine by clicking Downloads tab.

Structure of the Source Code:

As discussed in the previous post, I will use the following compilers and IDEs for our exercise works-

C++
  1. Windows:
    Visual C++ 2008 Express Edition
    Just open the solution file on Visual C++ 2008 Expression Edition or Visual Studio 2008. Compile and run.

    Command Line
    A batch (.bat) file will accompany each project. You need to run this batch file which will generate the executable .exe file. I assume that you already have the C++ compiler (cl.exe) in the path.
  2. Linux:
    Command Line
    A shell script (.sh) will accompany each project which will generate the executable file. The gcc must be in the path. Change the mode of the .sh file to executable with the following command:

    sudo chmod +x xyz.sh

    Run the file with the following command if you are in the project directory:
    ./xx.sh

Java
Eclipse:
  1. First create a new Java project in your workspace.
  2. Click "File->Import..." menu item.
  3. Import the project from the downloaded package
Netbeans:
Open the project from the downloaded package with "File->Open Project" menu item.


C#
Windows:
Visual C# 2008 Express Edition
Same as C++

Command Line
Same as C++

Linux:
MonoDevelop
Open the monodevelop project in the IDE. Compile and Run.

Command Line
A shell script (.sh) will be provided with the project. Consult C++ section to know how to run shell script in console.

3 comments:

  1. Program In C.
    #include "stdio.h"
    #include "conio.h"
    void main()
    {
    clrscr();
    printf("Hello World!");
    getch();
    }

    Output:
    Hello World!

    ReplyDelete
  2. @Zinc: Thanks for the code snippet. I was originally considering to include C for this comparison. But as you know, it will be difficult to implement the hexviewer for our case study (http://mainulhossain.blogspot.com/2008/03/case-study.html) with interactive GUI using raw C, because I don't want to program the console output. Anyway, if you want to contribute the C code, I will be happy.

    ReplyDelete
  3. Thanks for the post. I am intimately following your blog. You are really experimenting with a bunch of compilers and IDEs. I have tested your code. Just a little suggestion. The source code is shown as plain text. Can you please highlight the code block a bit more? You can use some stylesheet for that.

    ReplyDelete