• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Functions » Arguments to main () in C ++
Next →
← Prev

Arguments to main () in C ++

By Dinesh Thakur

Like C, C++ enables to pass arguments to the main () function also. These arguments are passed by typing them after the program name on the command line. Hence, these arguments are known as command line arguments. Command line arguments help in providing data to the program.

The syntax of passing command line arguments is

program_name <arg1 <arg2>…………. <argN>

where,

program_name = name of the program run from command line

<arg1> <argN> = name of argument passed to the program separated by a space

In C++, three arguments are passed to main () : argc, argv and envp

• The first argument argc of type int is the Argument Count, which counts the number of command line arguments passed to the program, including the program name.

• The second argument argv of the type array of char* is the Argument Vector, which holds the command line arguments. The first character array is argv [0] that stores the program name and the remaining elements represent the arguments supplied to the program.

• The third argument envp of the type array of char* is an optional argument that holds the current settings for any environment block variables.

The function header of main ()with arguments is written as

int main{
  int argc, char* argv[], char* envp[])

It can also be written as

int main{
  int argc, char *argv[], char *envp[])

To understand the concept of arguments to main(),consider this example.

Example: A program to demonstrate the concept of command line arguments

#include<iostream>
using namespace std;
int main{int argc, char *argv[], char *envp[]) {
  cout<<”Number of arguments (argc) is “<<argc<<endl;
  cout<<”Program name is "<<argv[0];
  cout<<”\nOther arguments are "<<endl;
  for (int i = 1; i < argc; i++)
   cout <<”argv["<<i<<”] = "<<argv[i] <<endl;
   cout<<”\nEnvironmental Settings are "<<endl;
   for (int i = 0; i < argc; i++)
     cout <<”envp["<<i<<"]="<<envp[i] <<endl;
     return 0i
}

Save the file as args.cpp and then run the program from command line using this statement.

args hello world

The output of the program is

Number of arguments (argc) is 3

Program name is C:\args.exe

Other arguments are

argv[l]=hello
argv[2]=world

Environmental Settings are

envp [0]== ::= ::\
envp [1]==c: =c: \
envp [2]=ALLUSERSPROFILE=C: \Documents and Settings\All Users

In this example, two strings hello and world are passed as command-line arguments with the name of the program args. The contents of argc and argv are shown in Figure.

Contents of argc and argv

You’ll also like:

  1. Command Line Arguments in C
  2. Differences Between Formal Arguments and Actual Arguments of a Function
  3. Write A C++ Program That The Function With Arguments And No Return Value.
  4. Write A C++ Program That the Function with Arguments and Return Value
  5. Write A C++ Program That The Function With No Arguments But Return Value.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW