C++基础积累 _tmain(int argc,_CHAR*arg[])参数的具体意义

 

原文链接:http://hi.baidu.com/quvia/item/9536d20a90b7f3eefe240d66

 

_tmain(int argc, _TCHAR* argv[])函数中第一个参数表示包括函数执行路径参数在内的参数个数

argv[]第一个参数用来存储函数的执行路径,后面用来存储自己写入的参数。

例如入参设置如下: 分别输入参数 1,和abc

关于int _tmain(int argc, _TCHAR* argv[]) - 逍遥十四少 - 逍遥十四少

这是argc的值为3 ,argv[0]为程序执行的路径,argv[1]存储的为用户自己输入的第一个参数

argv[2]存储的为用户自己输入的第二个参数,依次类推;

现写程序证明如下:

#include "stdafx.h"
#include <stdio.h>
//#include <windows.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])

printf("%d\nthe argv[0] is :%s\nthe argv[1] is:%s\nthe argv[2] is%s\n" ,argc,argv[0],argv[1],argv[2]);
system("pause");
return 0;
}

结果如下:

3
the argv[0] is :e:\practice\flash\test1\test1\Debug\test1.exe
the argv[1] is:1
the argv[2] isabc
请按任意键继续. . .

 

 

 

 

int _tmain(int argc, _TCHAR* argv[]) 与 int main(int argc, char *argv[]) 类似,可以接收命令行位置参数 _TCHAR 可以接收 中文等双字节编码字符书写的命令行。 _tmain 中文等双字节编码 编码 主程序名当然兼容 单字节编码 ASCII码 程序.

这个是接收外部参数的数组。 
如在可以运行c:\notepad.exe c:\xxlog.txt就可以用notepad打开c:\xxlog.txt文件,在这里argc等就是接收c:\xxlog.txt这个参数用的。

_TCHAR类型是宽字符型字符串,和我们一般常用的字符串不同,它是32位或者更高的操作系统中所使用的类型。

 

 

 

 

在visual c++ 2005/8 中,当选择编辑一个32位Win32控制台应用程序时.初始状态下系统自带函数:

   int _tmain( int argc, _TCHAR * argv[])
  {
   return 0 ;
  }


  上述Win32控制台应用程序的入口程序是用来存放机器的一个环境变量的,如:机器名,系统信息等.
  其中:
  int argc //表示参数个数
  char *argv[] //表示各个参数,字符串数组的每个单元是char*类型的,指向一个c风格字符串。
  //_TCHAR类型是宽字符型字符串,和我们一般常用的字符串不同,它是32位或者更 高的操作系统中所使用的类型.
  出处:

复制代码
大气象
  #include  < iostream >
  #include  < string .h >
   using namespace std;
   void main( int argc, char * argv[], char * envp[]) // envp[] 表述环境变量数组
  {
   int iNumberLines = 0 ;  // Default is no line numbers.
   // If more than .EXE filename supplied, and if the /n command-line option is specified, the listing
   // of environment variables is line-numbered.
   if (argc == 2 && stricmp(argv[ 1 ], " /n " ) == 0 )
  {
  iNumberLines = 1 ;
  }  // Walk through list of strings until a NULL is encountered.
   for ( int i = 0 ;envp[i] != NULL; ++ i )
  {
   if ( ! iNumberLines)
  cout << i << " : " << envp[i] << " \n " ;
  }
  }
复制代码


  The envp parameter is a pointer to an array of null-terminated strings that represent the values set in the user’s environment variables
  _tmain:
  1. Main是所有c或c++的程序执行的起点,_tmain是main为了支持unicode所使用的main的别名。_tmain()不过是unicode版本的的main().
  2. _tmain需要一个返回值,而main默认为void.
  3. _tmain的定义在<tchar.h>可以找到,如#define _tmain main,所以要加#include <tchar.h>才能用。_tmain()是个宏,如果是UNICODE则他是wmain()否则他是main().
  4. (一般_t、_T、T()这些东西都是宏都和unicode有关系),对于使用非unicode字符集的工程来说,实际上和main没有差别(其实就算是使用unicode字符集也未必有多大的差别)。
  5. 因此_tmain compile后仍为main,所以都可以执行.
  main()是WINDOWS的控制台程序(32BIT)或DOS程序(16BIT).
  WinMain()是WINDOWS的GUI程序.
wmain也是main的另一个别名,是为了支持二个字节的语言环境
  -----------------------
  int main( int argc[ , char *argv[ ] [, char *envp[ ] ] ] );
  wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
  int _tmain(int argc, _TCHAR* argv[])

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值