C++C++17Introduction to C++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,

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 if argc 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 and wchar_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

Learn To Use argc argv in C++ the C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?