8

I wonder what the abbreviation envp stands for, for example here:

int main(int argc, char **argv, char **envp);

I also wonder what the v in argv initially stood for. Was the v for "value"? Or maybe "vector"?

Lucas
  • 523
  • 2
  • 10
  • 20
nngm
  • 129
  • 1
  • 1
  • 8

4 Answers4

12

The meaning is:

  • argv stands for argument vector
  • argc for argument count, and
  • envp for environment pointer.

We can discuss about the good or bad naming convention, but it's a historic usage that dates back to the beginning of the C language: B.W.Kernighan and D.Ritchie used already argc and argv for main() in their first edition of The C Programming language in 1978.

The envp was added later from the UNIX development and is an alternative for using the environ pointer. I found a reference to it in a book from 1986, but it's certainly even older. It doesn't need a count, because it's null terminated. Note that it is not portable and therefore the use of getenv() should be preferred.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • names are just historical textbook names, any identifiers could be used instead. – Serge Feb 16 '19 at 13:42
  • 1
    @Serge Indeed. It's just a convention. OP wanted just to know why it was called like that. – Christophe Feb 16 '19 at 13:45
  • 3
    In Kernighan and Ritchie, *The C Programming Language*, “argv” is shown in the index as “argument vector”, not “argument value”. Suggested formatting: **arg**ument **v**ector, **arg**ument **c**ount, **env**ironment **p**ointer. – Eric Postpischil Feb 16 '19 at 13:56
  • 1
    @EricPostpischil Excellent idea ! Thanks ! Done ✅ – Christophe Feb 16 '19 at 14:02
  • argv[argc] is also NULL (so argv is also null-terminated). Hence argc is also redundant. Isn't it? – Sourav Kannantha B Jul 14 '21 at 14:50
  • @SouravKannanthaB interesting. Can you clarify your source that states this equality? – Christophe Jul 14 '21 at 16:13
  • @Christophe [this docs](https://learn.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160#standard-command-line-arguments). – Sourav Kannantha B Jul 14 '21 at 16:18
  • @SouravKannanthaB This is a very valid and interesting remark. Having checked further, it appears that argv[argc]==NULL is guaranteed by POSIX and by the C standard since the first C89. However, in the original K&R nothing as such was ever mentioned, and all their examples (that have been copied by hundreds of thousands of programmers afterwards) use loops on argc without ever going out of bounds. This is how inertia is created: since everybody was using argc, even if it's redundant, it had to be kept for backwards compatibility. (I learned C with K&R and never went beyond argc-1 ;-) ) – Christophe Jul 14 '21 at 21:09
  • Note that `argv` is also null terminated, despite the count. – Aykhan Hagverdili May 04 '23 at 14:26
5

In Kernighan and Ritchie, The C Programming Language, 1978, argv is shown in the index, on page 221, as:

argv argument vector 110

The text on pages 110-114 contains no further indication of the derivation of argc or argv, other than their semantics. envp does not appear in the index.

In the second edition, 1988, the text in the corresponding section, on page 114, says:

… two arguments. The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is…

Again in the second edition, envp does not appear in the index.

The Unix specification, IEEE Std 1003.1-2008, 2016, The Open Group Base Specifications Issue 7, says in the section on environ, execl, and related routines:

Some implementations provide a third argument to main() called envp. This is defined as a pointer to the environment.

This is consistent with envp standing for environment pointer.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    Interesting! I have only the first version, where *vector* only appears in the index. FYI, the use of envp is much older, since I already found a mention of it as ***pointer to** an array of character pointers that are **the environment** of the executed program* in Maurice Bach's 1986 book *The design of the UNIX® operating system* – Christophe Feb 16 '19 at 14:48
1

From C language spec:

J.5.1 Environment arguments
1 In a hosted environment, the main function receives a third argument, char *envp[], that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.2.1).

Note that this third argument is not listed in C++ language spec, though some implementations are still fine with it.

user7860670
  • 35,849
  • 4
  • 58
  • 84
0

I haven't studied the etymology of conventional argument names, so please excuse my speculative answer.

'env' rather obviously means environment or environment variables.

The 'p' in envp could be initialism for parameter(s) or, perhaps pointer(s) referring to the type. Perhaps even environment variables of the Program.

Etymology isn't really important for the meaning of the program, so I recommend not to worry about it too much.

eerorika
  • 232,697
  • 12
  • 197
  • 326