Site icon Learn C++

Learn To Use argc argv in C++

Learn To Use argc argv in C++

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.

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,

[crayon-673fbf41d78ad561883433/]

This will copy test.c from its folder to D:\ folder. In this command line there are 3 arguments,

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;

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.

[crayon-673fbf41d78b5186096194/]

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:

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,

[crayon-673fbf41d78b7785107325/]

or argv[] can be char * as below,

[crayon-673fbf41d78bb652465647/]

if your function has no argument, you can use the main() function without any argument parameters as below,

[crayon-673fbf41d78c5853052560/]

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,

[crayon-673fbf41d78c6638536012/]

For example when we run our app as below,

[crayon-673fbf41d78c7136504572/]

argc value will be 2 and argv[1] is “test” argument here. This will print out the text

[crayon-673fbf41d78c8215279693/]

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

[crayon-673fbf41d78c9936396081/]

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

[crayon-673fbf41d78ca246345645/]

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.

Exit mobile version