Command line Arguments in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Command line Arguments in C++

Can somebody please briefly explain me about (int argc , char* argv[]) used in c++. I tried to learn it from some YouTube tutorials but didn’t quite get it.

6th Sep 2019, 5:13 PM
Raja Shahvez
Raja Shahvez - avatar
5 Answers
+ 4
int main(int argc, char* argv[]){ cout << argv[1] << endl; } ---- *compile code* in the commandline: yourcode.exe hello output: hello
6th Sep 2019, 6:05 PM
Cat Sauce
Cat Sauce - avatar
+ 4
Google your IDE and what type of executable it generates. Then you can run the executable the way Cat Sauce said.
6th Sep 2019, 7:02 PM
Zeke Williams
Zeke Williams - avatar
+ 3
Yes! This is something that took a while for me to get too. C++ can get compiled on the command line using a compiler program like GCC. When you compile, the output is an executable binary that you can run with arguments. Example: g++ main.cpp -o exec g++ is just a way you can run GCC ./exec arg1 arg2 arg1 and arg2 will be the 2nd and 3rd elements in the argv array. The first element is the program name itself
6th Sep 2019, 5:52 PM
Zeke Williams
Zeke Williams - avatar
+ 3
Raja Shahvez if you still have trouble, feel free to dm me
6th Sep 2019, 6:16 PM
Cat Sauce
Cat Sauce - avatar
+ 3
Cat Sauce I litteraly didn’t get it. can you plz explain with another example
6th Sep 2019, 6:29 PM
Raja Shahvez
Raja Shahvez - avatar