main 函数第三个参数

原创 2007年09月18日 17:21:00
This is G o o g l e's cache of as retrieved on Aug 8, 2007 04:35:28 GMT.
G o o g l e's cache is the snapshot that we took of the page as we crawled the web.
The page may have changed since that time. Click here for the current page without highlighting.
This cached page may reference images which are no longer available. Click here for the cached text only.
To link to or bookmark this page, use the following url:

Google is neither affiliated with the authors of this page nor responsible for its content.
These search terms have been highlighted:  main  envp 

Node:Environment variables, Next:Compiling multiple files, Previous:Processing command-line options, Up:Putting a program together

Environment variables

Sometimes it is useful to communicate with a program in a semi-permanent way, so that you do not need to specify a command-line option every time you type the command to execute the program. One way to do this is to generate a configuration file, in which you can store data that will be used by the program every time it is run. This approach is typically useful if you have a large amount of data that you want to pass to a program every time it runs, or if you want the program itself to be able to change the data.

However, environment variables provide a more lightweight approach. Environment variables, sometimes called shell variables, are usually set with the export command in the shell. (This section assumes you are using the GNU Bash shell.) Standard environment variables are used for information about your home directory, terminal type, and so on; you can define additional variables for other purposes. The set of all environment variables that have values is collectively known as the environment.

Names of environment variables are case-sensitive, and it is good form to use all upper-case letters when defining a new variable; certainly this is the case for all system-defined environment variables.

The value of an environment variable can be any string that does not contain a null character (since the null character is used to terminate the string value).

Environment variables are stored in a special array that can be read by your main function. Here is the skeleton for a main function that can read environment variables; notice we have added a third parameter to main, called envp, which comes after argc and argv.

#include <stdio.h>

/* To shorten example, not using argp */
int main (int argc, char *argv[], char *envp[])

  return 0;

Notice that envp is an array of strings, just as argv is. It consists of a list of the environment variables of your shell, in the following format:


Just as you can manually process command-line options from argv, so can you manually process environment variables from envp. However, the simplest way to access the value of an environment variable is with the getenv function, defined in the system header stdlib.h. It takes a single argument, a string containing the name of the variable whose value you wish to discover. It returns that value, or a null pointer if the variable is not defined.

#include <stdio.h>
#include <stdlib.h>

/* To shorten example, not using argp */
int main (int argc, char *argv[], char *envp[])
  char *home, *host;

  home = getenv("HOME");
  host = getenv("HOSTNAME");

  printf ("Your home directory is %s on %s./n", home, host);

  return 0;

When you run this code, it will print out a line like the following one.

Your home directory is /home/rwhe on linnaeus.

Note: Do not modify strings returned from getenv; they are pointers to data that belongs to the system. If you want to process a value returned from getenv, copy it to another string first with strcpy. (See Strings.) If you want to change an environment variable from within your program (not usually advisable), use the putenv, setenv, and unsetenv functions. See Environment Access, for more information on these functions.




本文首发于个人博客, 转载需或者作者授权!main函数两种常见形式在C/C++ 程序中, 操作系统通过main函数来调用程序,main函数是从程序的入口。常见的ma...


在 C 启动过程中,传递main()函数三个参数:argc,argv和env. * argc: 整数,为传给main()的命令行参数个数。包括程序自身这个字符串.批处理下是 %0 *...


零、序 一、自定义Style 二、在XML中为属性声明属性值   1. 在layout中定义属性   2. 设置Style   3. 通过Theme指定 三、在运行时获取属性值   1....


零、序 一、自定义Style 二、在XML中为属性声明属性值   1. 在layout中定义属性   2. 设置Style   3. 通过Theme指定 三、在运行时获取属性值   1....


Android中自定义样式与View的构造函数中的第三个参数defStyle的意义 零、序 一、自定义Style 二、在XML中为属性声明属性值   1. 在l...


1.数组的基本概念(1)数组的定义 int a[] = {1, 2}; int b[100] = {1, 3}; int c[200] = {0}; //编译的时候 就已经确定 所有的值 为零 对一维...
  • Daycym
  • Daycym
  • 2017年11月02日 17:41
  • 33


零、序   系统自带的View可以在xml中配置属性,对于写的好的Custom View同样可以在xml中配置属性,为了使自定义的View的属性可以在xml中配置,需要以下4个步骤: ...

对理解ajax第三个回调函数的封装和 null设置少传的那个参数默认

var a=function(msg){ alert( msg); } a(1); 之前看视频对ajax封装回调函数总不太理解,知道上边例子跑通就理解了一个函数作为参数,其实函数后边再加括号里边...


原文链接:转载只为自己珍藏~作者写的太好) Android中自定义样式与View的构造...
  • ji0k890
  • ji0k890
  • 2016年07月21日 18:38
  • 59
您举报文章:main 函数第三个参数