“Hello world”不简单

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

[[124819]]

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

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

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

[[124820]]

本文的作者 John D. Cook

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

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

  1. /*------------------------------------------------------------ 
  2.    HELLOWIN.C -- Displays "Hello, Windows 98!" in client area 
  3.                  (c) Charles Petzold, 1998 
  4.   ------------------------------------------------------------*/ 
  5.    
  6. #include  
  7.    
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; 
  9.    
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  11.                     PSTR szCmdLine, int iCmdShow) 
  12.      static TCHAR szAppName[] = TEXT ("HelloWin") ; 
  13.      HWND         hwnd ; 
  14.      MSG          msg ; 
  15.      WNDCLASS     wndclass ; 
  16.    
  17.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ; 
  18.      wndclass.lpfnWndProc   = WndProc ; 
  19.      wndclass.cbClsExtra    = 0 ; 
  20.      wndclass.cbWndExtra    = 0 ; 
  21.      wndclass.hInstance     = hInstance ; 
  22.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ; 
  23.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ; 
  24.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; 
  25.      wndclass.lpszMenuName  = NULL ; 
  26.      wndclass.lpszClassName = szAppName ; 
  27.    
  28.      if (!RegisterClass (&wndclass)) 
  29.      { 
  30.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  31.                       szAppName, MB_ICONERROR) ; 
  32.           return 0 ; 
  33.      } 
  34.         
  35.      hwnd = CreateWindow (szAppName,                  // window class name 
  36.                           TEXT ("The Hello Program"), // window caption 
  37.                           WS_OVERLAPPEDWINDOW,        // window style 
  38.                           CW_USEDEFAULT,              // initial x position 
  39.                           CW_USEDEFAULT,              // initial y position 
  40.                           CW_USEDEFAULT,              // initial x size 
  41.                           CW_USEDEFAULT,              // initial y size 
  42.                           NULL,                       // parent window handle 
  43.                           NULL,                       // window menu handle 
  44.                           hInstance,                  // program instance handle 
  45.                           NULL) ;                     // creation parameters 
  46.         
  47.      ShowWindow (hwnd, iCmdShow) ; 
  48.      UpdateWindow (hwnd) ; 
  49.         
  50.      while (GetMessage (&msg, NULL, 0, 0)) 
  51.      { 
  52.           TranslateMessage (&msg) ; 
  53.           DispatchMessage (&msg) ; 
  54.      } 
  55.      return msg.wParam ; 
  56.    
  57. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
  58.      HDC         hdc ; 
  59.      PAINTSTRUCT ps ; 
  60.      RECT        rect ; 
  61.         
  62.      switch (message) 
  63.      { 
  64.      case WM_CREATE: 
  65.           PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ; 
  66.           return 0 ; 
  67.              
  68.      case WM_PAINT: 
  69.           hdc = BeginPaint (hwnd, &ps) ; 
  70.              
  71.           GetClientRect (hwnd, &rect) ; 
  72.              
  73.           DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect, 
  74.                     DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; 
  75.              
  76.           EndPaint (hwnd, &ps) ; 
  77.           return 0 ; 
  78.              
  79.      case WM_DESTROY: 
  80.           PostQuitMessage (0) ; 
  81.           return 0 ; 
  82.      } 
  83.      return DefWindowProc (hwnd, message, wParam, lParam) ; 
责任编辑:张伟 来源: 程序师
相关推荐

2017-11-23 17:45:46

Yii框架IntelYii框架深度剖析

2009-08-11 10:32:23

什么是Groovy

2009-07-30 13:21:17

Scala入门Hello World

2011-06-08 14:39:06

Qt 教程

2023-01-06 08:18:44

2023-09-04 07:30:03

Wasm汇编语言

2009-09-16 17:15:19

OSGi Bundle

2012-02-20 14:26:48

JavaPlay Framew

2012-06-26 09:40:14

部署开发管理

2023-05-23 08:01:10

Netty网络通信

2009-08-14 16:54:19

C# Hello Wo

2011-08-05 09:48:46

iPhone Interface

2014-04-11 11:36:42

NDKAndroid开发终端

2024-04-11 13:13:27

2021-11-26 08:22:01

Java动态开发

2009-07-20 10:06:47

虚拟化思杰操作系统

2010-01-19 10:10:28

2010-01-07 13:27:22

Linux驱动程序

2017-06-26 08:55:52

2013-12-12 17:30:03

Lua例子
点赞
收藏

51CTO技术栈公众号