1

I am running a C++ program from the command line on Bash, which is in a Linux environment. I am curious how you pass in a parameter from the command line. Here is my program:

#include <iostream>
using namespace std;

int large_pow2( int n );

int main()
{
   int value = 15;
   int largest_power = large_pow2(value);

   cout << "The highest power of 2 in " << value << " is " << large_power << "." << endl;

   return 0;
} 

int large_pow2( int n )
{
   int i = n
   int j = i & (i - 1);
   while( j != 0)
   {
      i = j;
      j = i & (i - 1);
   }
   return j;
}

After I compile the program I want to be able to use the command line to pass in a number to use for value. For instance, to run the program you type ./"program_name" where "program_name" is the name of my program without quotes. Is there a way to set value = n or something? When I run the program let's say I want n to be 20 so on the command line I type something like ./"program_name" 20. Then the program would run with n = 20. Is there a way to do that? I am completely new to a Linux environment and Bash so don't quite know how to do things in it yet.

1

2 Answers 2

2

Use argc and argv in int main(int argc, char *argv[]) and modify your code accordingly.

The argc arguments tracks the number of arguments passed to your program from CLI and is always >=1. When 1 it is it name of program. So argc[0] is program name.

argv holds the command line arguments, other than program name and is always char string. Hence we need to use appropriate converter like atoi, if you don't want string.

So your code will look like, error checking not done for simplicity

int main(int argc, char *argv[])
{
    //Now we expect argc ==2, program value, This will when argc != 2
    // We should use if in real scenario
    assert(argc == 2);
    int value = atoi(argv[1])
    int largest_power = large_pow2(value);

    cout << "The highest power of 2 in " << value << " is " << large_power << "." << endl;

    return 0;
} 
Sign up to request clarification or add additional context in comments.

5 Comments

Awesome, thank you. I am curious though, if I am just trying to pass in an integer parameter, then why do I need a char []?
Updated in the answer. Please have a look
Okay that makes a lot of sense. Another question (Sorry for the many questions, I am just trying to understand this for future reference), I get that argc tacks the number of arguments passed into the program, but why is it never used? For instance, argv is in the header of main, but then we use it in the initialization of value. Does my question make sense? I apologize if it is worded in a confusing way.
No problem for many question, In fact, we do use argc in real program, Ignore here for simplicity. May be I should update the answer. Adding an assert now
So the name of the program is itself 1 argument and the value I am passing into it is a second argument hence why argc = 2?
1

Your main method can take (int argc, char** argv) which are the count of arguments and the NUL terminated args. The program path is argv[0] so atoi(argv[1]) is probably what you want. Check argc ==2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.