“Hello world” is the hard part

Kernighan and Ritchie’s classic book The C Programming Language began with a sample C program that printed “hello world.” Since then “hello world” has come describe the first program you write with any technology, even if it doesn’t literally print “hello world.”

Hello-world programs are often intimidating. People think “I must be a dufus because I find hello-world hard. At this rate I’ll never get to anything interesting.”

The problem is that we confuse the first task with the easiest task. Hello-world programs are almost completely arbitrary. You can’t deduce what a compiler is named, where files must be located, how they must be formatted, etc. You have to be told. The amount of arbitrary material you need to learn is greatest up-front and slowly decreases.

When I started programming I thought I’d quickly get past the hello-world stage and only write substantial programs from then on. Instead, it seems I’ve spent a good chunk of my career writing hello-world programs with no end in sight.

* * *

No discussion of hello-world programs would be complete without mentioning possibly the most intimidating hello-world program: the first Windows program in Charles Petzold’s Programming Windows book. I was only able to find the program from the Windows 98 edition of his book. I don’t recall how it differs much from the program in his first edition, but I vaguely remember the original being worse.

/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT ("HelloWin") ;
    HWND hwnd ;
    MSG msg ;
    WNDCLASS wndclass ;

    wndclass.style = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc = WndProc ;
    wndclass.cbClsExtra = 0 ;
    wndclass.cbWndExtra = 0 ;
    wndclass.hInstance = hInstance ;
    wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName = NULL ;
    wndclass.lpszClassName = szAppName ;

    if (!RegisterClass (&wndclass))
    {
        MessageBox (NULL, TEXT ("This program requires Windows NT!"),
        szAppName, MB_ICONERROR) ;
        return 0 ;
    }

    hwnd = CreateWindow (szAppName, // window class name
    TEXT ("The Hello Program"), // window caption
        WS_OVERLAPPEDWINDOW, // window style
        CW_USEDEFAULT, // initial x position
        CW_USEDEFAULT, // initial y position
        CW_USEDEFAULT, // initial x size
        CW_USEDEFAULT, // initial y size
        NULL, // parent window handle
        NULL, // window menu handle
        hInstance, // program instance handle
        NULL) ; // creation parameters

    ShowWindow (hwnd, iCmdShow) ;
    UpdateWindow (hwnd) ;

    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg) ;
        DispatchMessage (&msg) ;
    }
    return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc ;
    PAINTSTRUCT ps ;
    RECT rect ;

    switch (message)
    {
        case WM_CREATE:
            PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
            return 0 ;

        case WM_PAINT:
            hdc = BeginPaint (hwnd, &ps) ;

            GetClientRect (hwnd, &rect) ;

            DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
            DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

            EndPaint (hwnd, &ps) ;
            return 0 ;

        case WM_DESTROY:
            PostQuitMessage (0) ;
            return 0 ;
    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
}

* * *

For a daily dose of computer science and related topics, follow @CompSciFact on Twitter.

CompSciFact twitter icon

13 thoughts on ““Hello world” is the hard part

  1. Then again, there’s something to be said for using a language where Hello World is say "Hello, world!"

  2. Even python where Hello World is literally print “Hello World” is not a trivial thing to do when you factor in finding, downloading, installing, and running python. Especially if you are a novice doing it for the first time. In my position as a Developer, I find that one of the most difficult things to do is just get a new development environment up and running.

  3. Currently I find the most stripped down way of programming ANYTHING is to write javascript in a single static HTML file. But even that requires some HTML boilerplate (which is NOT javascript) in order to give javascript “enough environment” to run…

  4. I’m amused to see that APL, which I think has the steepest learning curve of any programming language I’ve ever run across, has the simplest “Hello World!” program I’ve yet found. It is 'Hello, world!'

  5. Codi, and if you’ve downloaded Python 3, as most novices would, print “hello, world” doesn’t work!

  6. Great article Mr. John D. Cook, enjoyed the trip down memory lane ๐Ÿ™‚

    I think in the 5th edition of Mr. Petzolds book “Programming Windows” the hello world is rather great ๐Ÿ™‚

    Remembering how scary entering the world of Windows C through the first hello world tests was when I was starting to get into Windows programming (it was around 2000) I decided that if I ever survive through the struggle of learning this magic, I’ll do a write up for others to follow so they could have a slightly easier time easing into all those structs and unicode that was introduced in the “possibly the most intimidating hello-world program” example ๐Ÿ™‚

    Here’s a link to the article for fellow Win C enthusiasts. It doesn’t do the hello world part anymore though since I decided it would be more useful as a ‘whole program’ for future coders to dip their feet in, but its basically a condensed version of the 1998 example Mr. John D. Cook here demonstrated.

  7. I wonder how many others went into stores that had C=64’s on display and typed the following into the keyboard

    10 PRINT “Hello World ”
    20 GOTO 10

Leave a Reply

Your email address will not be published. Required fields are marked *