argc and argv are the arguments to the program itself. argc being the number of arguments, and argv being the actual arguments.
If you dont understand the concept, think of it as: The same way that a function can but doesnt have to take arguments, your program can also take arguments. These arguments are given via the command line/terminal.
If you use windows mostly, you probably dont use the command line often, if at all to start a program. In linux, this is used all the time.
The only way i can explain it is via example:
1 2 3 4 5 6 7 8 9 10
|
#include <iostream>
int main(int argc, char *argv[]){
if (argc != 2){
std::cout << "Password required" << std::endl;
}
else{
std::cout << "Password is " << argv[1] << std::endl;
}
}
|
this will execute "password required" if no argmuents are given or 3 or more agruments are given. These arguments are separated by a space (normally). However if you give one argument, it will print it out. This is not anything else other than the input that you provide.
example:
1 2 3 4 5 6 7 8 9
|
metulburr@ubuntu:~/programs/cplusplus$ g++ test2.cpp -o test2
metulburr@ubuntu:~/programs/cplusplus$ ./test2
Password required
metulburr@ubuntu:~/programs/cplusplus$ ./test2 password
Password is password
metulburr@ubuntu:~/programs/cplusplus$ ./test2 password password
Password required
metulburr@ubuntu:~/programs/cplusplus$ ./test2 b
Password is b
|
here by executing:
./test2
i gave no arguments. This is essentially what happens when you double click the executable, AKA the exe
here i gave 2 arguments:
./test2 password password
and here i bypassed the if condition because there is only one argument:
./test2 password
and here i also gave another correct number of arguments:
./test2 b
so it again printed out b
I tried putting my computer password in the "mypassowrd" area and still nothing changes. |
password is literally the string password, not the computer password.