“Hello world”不簡(jiǎn)單
由Kernighan和Ritchie合著的經(jīng)典教程《The C Programming Language》的開篇***個(gè)C程序例子是打印簡(jiǎn)單的“hello world”。從此之后,“hello world”就成了描述一個(gè)人編寫的***個(gè)程序的代名詞——不論是什么語(yǔ)言技術(shù),即使實(shí)際上程序并沒有在字樣上輸出“hello world”幾個(gè)字。
對(duì)于初學(xué)者來(lái)說(shuō),這“hello world”程序是讓人恐怖的。他會(huì)想“我一定非常笨,連這入門的hello world程序都覺得難。照這樣下去,我一定不會(huì)喜歡上編程。”
其實(shí),這問題的原因是我們把“***個(gè)”和”最簡(jiǎn)單的一個(gè)“混淆了。“hello world”程序可以是任何的程序,沒有難易限制。當(dāng)你***次編程時(shí),你不知道該用哪種編譯器、不知道代碼文件應(yīng)該放到哪里、不知道它們應(yīng)該是什么格式,等等。你需要去學(xué)。大量的知識(shí)在你真正能夠編程前都需要學(xué)習(xí)、慢慢的學(xué)會(huì) 。
本文的作者 John D. Cook
當(dāng)我最初開始學(xué)習(xí)編程時(shí),我總希望能盡快的越過寫“hello world”程序的階段,希望能夠立刻開始編寫真正有用的程序。但事實(shí)上,我發(fā)現(xiàn)我大半輩子時(shí)間都在寫“hello world”程序,而且看不到結(jié)束的盡頭。
每當(dāng)討論起“hello world”程序,幾乎避免不了的要說(shuō)一說(shuō)這世界上最恐怖的“hello world”程序:Charles Petzold在他的《Programming Windows》一書中描述的***個(gè)Windows程序。我只能找到這本書的Windows 98版的。不知道它跟最初的原版有多大區(qū)別,但我印象里原版里的代碼會(huì)比現(xiàn)在這個(gè)更恐怖。
- /*------------------------------------------------------------
 - 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) ;
 - }
 















 
 
 





 
 
 
 