“Hello world”不简单

jopen 9年前

“Hello world”不简单

由Kernighan和Ritchie合著的经典教程《The C Programming Language》的开篇第一个C程序例子是打印简单的“hello world”。从此之后,“hello world”就成了描述一个人编写的第一个程序的代名词——不论是什么语言技术,即使实际上程序并没有在字样上输出“hello world”几个字。

对于初学者来说,这“hello world”程序是让人恐怖的。他会想“我一定非常笨,连这入门的hello world程序都觉得难。照这样下去,我一定不会喜欢上编程。”

其实,这问题的原因是我们把“第一个”和”最简单的一个“混淆了。“hello world”程序可以是任何的程序,没有难易限制。当你第一次编程时,你不知道该用哪种编译器、不知道代码文件应该放到哪里、不知道它们应该是什么格式, 等等。你需要去学。大量的知识在你真正能够编程前都需要学习、慢慢的学会 。

“Hello world”不简单
本文的作者 John D. Cook

 

当我最初开始学习编程时,我总希望能尽快的越过写“hello world”程序的阶段,希望能够立刻开始编写真正有用的程序。但事实上,我发现我大半辈子时间都在写“hello world”程序,而且看不到结束的尽头。

每当讨论起“hello world”程序,几乎避免不了的要说一说这世界上最恐怖的“hello world”程序:Charles Petzold在他的《Programming Windows》一书中描述的第一个Windows程序。我只能找到这本书的Windows 98版的。不知道它跟最初的原版有多大区别,但我印象里原版里的代码会比现在这个更恐怖。

/*------------------------------------------------------------     HELLOWIN.C -- Displays "Hello, Windows 98!" in client area                   (c) Charles Petzold, 1998    ------------------------------------------------------------*/     #include      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) ;  }

英文原文:“Hello world” is the hard part
中文翻译:程序师网