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















 
 
 





 
 
 
 