When we run an application, we can directly run the executable file or we can run this executable file with arguments. Arguments are very useful for many reasons. This feature allows the user to define their arguments on the command line or from file info and the executable file can then use the arguments in the code to set variables or determine internal actions. When you create an application in a professional C++ Editor, generally it automatically adds argc
, argv
parameters to the main()
function automatically.
Now let’s see what is an argument.
Table of Contents
What is a command line argument?
Arguments are parameters coming after commands or executable filename that are texts separated by spaces in a command line. In an Operating System (OS), many OS commands require extra arguments when they are used in terminals (Command Prompt, Power Shell). For example, copy command can be used with source file name and destination folder name parameters as below,
This will copy test.c from its folder to D:\ folder. In this command line there are 3 arguments,
- copy
- test.c
- D:\
Now let’s see how we can use argument in C++
How do I use argc in C++?
argc
is an Argument Count number as in integer form, it holds number of command-line arguments passed by the execution including the name of the program as a first argument. The value of argc
should be over zero when you run your executable without any arguments, in this situation argc
is equal to 1. So if we pass an argument to a program (in command line or in executable fileoptions), value of argc
would be 2 (one argument is program name other one is the next argument that we pass with that program name)
How do I use argv in C++?
argv is an Argument Vector, and these vectors are used like array of characters or strings (with pointers or without pointers). Note that;
argv[]
vectors (array elements) contain pointers to string ifargc
is greater than zero.- argv[0] is the first argument which is the name of the program
- argv[1] and so on are the next arguments
How to use argv[] and argc in C++?
When we create a C++ console application in C++ Builder, our main function has two parameter argc
, and argv[]
in default. These two parameters are used to obtain arguments of the user command line.
In C++ Builder, arguments argv[] are stored as _TCHAR*, because _TCHAR mapping in C++Builder is intended to make it easier to write source code that can be used with either wide or narrow strings. You can map _TCHAR to either wchar_t
or char
so that your code uses the correct string type required by RAD Studio frameworks and libraries or the Windows API, and so that RTL functions float to the correct definition (wide or narrow, respectively). To set the _TCHAR maps to option, use the Project > Options > C++ (Shared Options) dialog box.
Your C++ application needs to deal with both wide and narrow strings:
- The C++ RTL contains routines designed for both
char
andwchar_t
. - The Windows API are typically narrow, requiring
char
. - RAD Studio frameworks and libraries use wide string data (Unicode), requiring
wchar_t
.
In interactions with RAD Studio frameworks and libraries, you need to use the wide RTL functions, or do proper conversions before passing data to RAD Studio frameworks and libraries. (See UTF-8 Conversion Routines.)
_TCHAR, which is declared conditionally in the tchar.h header file, is defined as a typedef (alias) that maps either to char
or to wchar_t
. When you want to write portable code (that can interact with the C++ RTL, the Windows API, and RAD Studio frameworks and libraries), you should use _TCHAR (instead of hard coding char
or wchar_t
data types). Then you can adjust the _TCHAR mapping option to either char
or wchar_t
(on the C++ (Shared Options) page). For example, the current _TCHAR Mapping setting controls whether your application uses either the ANSI versions or the wide versions of the RTL floating functions.
In general, argv[]
are char **
as below,
or argv[]
can be char *
as below,
if your function has no argument, you can use the main()
function without any argument parameters as below,
Is there an example of how to use argc in C++?
For example, here is an argc C++ example that checks if an execution has extra arguments,
For example when we run our app as below,
argc
value will be 2 and argv[1]
is “test” argument here. This will print out the text
Is there an example of how to use argv in C++?
Here is an example to use argv and argc together. If you really want to check if there is extra parameter, then u can use if(argc>1)
blow
Is there an example of how to use use _argc and _argv in C++?
If you want to get the global argument values by using stdlib.h , _argc and _argv[] can be used as below too
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.