Home Do you know int main takes three arguments
Post
Cancel

Do you know int main takes three arguments

Many of us know how to read command line arguments from the program in C/C++

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main(int argc, char** pArgv)
{
    for (int i = 0; i < argc; i++) {
        std::cout << i << " - " << pArgv[i] << "\n";
    }

    return 0;
}

Output:

1
2
3
4
bhavith@bhavith:$ ./a.out first second 
0 - ./a.out
1 - first
2 - second

But do you know int main takes one more argument, i.e. pointer to an list of environment variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main(int argc, char** pArgv, char** pEnv) {
    
    char** pTmp = pEnv;

    while (*pTmp != nullptr) {
        std::cout << *pTmp << "\n";
        pTmp++;
    }
    
    std::cout << "End of the list \n";
    return 0;
}

I ran the program in https://www.programiz.com/cpp-programming/online-compiler/ and this is the output :)

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
KUBERNETES_PORT=tcp://10.0.0.1:443
KUBERNETES_SERVICE_PORT=443
PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT_80_TCP_ADDR=10.0.14.233
HOSTNAME=programiz-compiler-deployment-755649f487-8zgzd
PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT_80_TCP_PORT=80
HOME=/home/compiler
PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT_80_TCP_PROTO=tcp
PROGRAMIZ_COMPILER_SERVICE_HOST=10.0.10.151
PS1=
PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT_80_TCP=tcp://10.0.14.233:80
PROGRAMIZ_COMPILER_SERVICE_PORT=80
PROGRAMIZ_COMPILER_PORT=tcp://10.0.10.151:80
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PROGRAMIZ_COMPILER_PORT_80_TCP_ADDR=10.0.10.151
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
PROGRAMIZ_COMPILER_PORT_80_TCP_PORT=80
PROGRAMIZ_COMPILER_PORT_80_TCP_PROTO=tcp
PROGRAMIZ_COMPILER_WEB_UI_SEVICE_SERVICE_HOST=10.0.14.233
GCC_COLORS=
KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.0.0.1
PWD=/app
PROGRAMIZ_COMPILER_PORT_80_TCP=tcp://10.0.10.151:80
PROGRAMIZ_COMPILER_WEB_UI_SEVICE_SERVICE_PORT=80
PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT=tcp://10.0.14.233:80
End of the list 
This post is licensed under CC BY 4.0 by the author.

Adapter design pattern

Is const reference always thread safe when used as function parameter?