教你用VS2008/VS2010寫簡單C/C++語言main函數程序

1.首先打開VS2008/VS2010,選擇創建工程。

2.Project種類下面選擇Visual C++/Win32/Win32 Console application.

3.接下來的嚮導中你可以選擇ATL / MFC,根據需要選擇。

這樣就創建了一個帶有main函數的文件,如果你程序要輸出到終端的話,你根本看不到。因爲main的函數返回值返回後程序就結束了,mian函數的返回值0表示正常結束,返回0意外的值都代表一種錯誤。

如果你要看到命令行輸出內容的話,有以下三種方法:

①加上一句系統暫停命令system("pause");即可

②或者你在return 0;語句前前設個斷點),

或者在程序無錯的情況下直接按Ctrl+F5(開始執行(不調試))。


示例代碼如下:

// Console application(Start from the main funtion).cpp : コンソール アプリケーションのエントリ ポイントを定義します。
#include "stdafx.h"

#include "Console application(Start from the main funtion).h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 唯一のアプリケーション オブジェクトです。

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
     int nRetCode = 0;

     // MFC を初期化して、エラーの場合は結果を印刷します。
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
         // TODO: 必要に応じてエラー コードを変更してください。
          _tprintf(_T("致命的なエラー: MFC の初期化ができませんでした。\n"));
          nRetCode = 1;
     }
     else
     {
          // TODO: アプリケーションの動作を記述するコードをここに挿入してください。

          cout<<"Start from here"<<endl;

          // Add your codes here.

          cout<<"End to here"<<endl;

         // The program will stop at here,so that you can see the output message on the black window.
         system("pause");
     }
     return nRetCode;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章