教你用VS2008/VS2010写简单C/C++语言main函数程序

来源:互联网 发布:qq飞车指挥官改装数据 编辑:程序博客网 时间:2024/06/05 10:08

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;
}

原创粉丝点击