This question already has answers here:
What does int argc, char *argv[] mean?
(12 answers)
Closed 9 years ago.
I was messing around with projects in C/C++ and I noticed this:
C++
#include <iostream.h>
int main (int argc, const char * argv[]) {
// insert code here...
cout << "Hello, World!\n";
return 0;
}
and
C
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
return 0;
}
So I've always sort of wondered about this, what exactly do those default arguments do in C/C++ under int main? I know that the application will still compile without them, but what purpose do they serve?
They hold the arguments passed to the program on the command line. For example, if I have program a.out and I invoke it thusly:
$ ./a.out arg1 arg2
The contents of argv will be an array of strings containing
[0] "a.out" - The executable's file name is always the first element
[1] "arg1" - The other arguments
[2] "arg2" - that I passed
argc holds the number of elements in argv (as in C you need another variable to know how many elements there are in an array, when passed to a function).
You can try it yourself with this simple program:
C++
#include <iostream>
int main(int argc, char * argv[]){
int i;
for(i = 0; i < argc; i++){
std::cout << "Argument "<< i << " = " << argv[i] << std::endl;
}
return 0;
}
C
#include <stdio.h>
int main(int argc, char ** argv){
int i;
for(i = 0; i < argc; i++){
printf("Argument %i = %s\n", i, argv[i]);
}
return 0;
}
If you want to accept arguments through commandline ,then you need to use the arguments in the main function .argc is the argument count and array of charecter pointers list the arguments.
refer this link http://www.cprogramming.com/tutorial/c/lesson14.html
Those are for command-line arguments. argc is the number of arguments, and the arguments are stored as an array of null-terminated strings (argv). Normally, a program with no command-line arguments passed in will still have one stored in argv; namely, the name used to execute the program (which won't always be there, depending on how the program is executed, but I can't remember what the circumstances are for that).
argc and argv are how command line arguments are passed to main() in C and C++.
argc will be the number of strings pointed to by argv, this will usually be one more than the number of arguments you pass from terminal, as usually the first is the name of the program.
Related
I am not familiar with using command line arguments. I am trying to take a vector input as a command line argument. Please refer the below code and help me with the error.
#include<bits/stdc++.h>
#include<vector>
using namespace std;
int main(int argc, char *argv[]){
vector<int> arr;
cout<<"Arguments are:\n";
for(int i = 1 ; i < argc ; i++){
arr.push_back(argv[i]);
}
for(int i = 0 ; i < arr.size() ; i++){
cout<<arr[i]<<" ";
}
}
The code is giving below error.
The problem is, that your vector contains ints, but the command line arguments are strings (see error).
You should convert the argv[i] to int, than use the push_back().
You can use stoi() which converts string to int, but keep in mind that it could happen when not just digits appear in the command line argument.
I wanna sort an array of command line arguments. All arguments are integer.
Here is my code, but it does not work.
#include <iostream>
using namespace std;
int main (int argc, char *argv[]) {
for (int i=0; i<argc-1; ++i) {
int pos = i;
for (int j=i+1; j<argc; ++j) {
if (argv[j] - '0' < argv[pos] - '0') {
pos = j;
}
}
char *tempt = argv[i];
argv[i] = argv[pos];
argv[pos] = tempt;
}
for (int i=0; i<argc; ++i) {
cout << argv[i] <<endl;
}
}
After compiling, when I called ./a.out 4 3 2 1, it still printed 4 3 2 1 to the screen instead of 1 2 3 4.
What's wrong?
Thanks in advance.
Try std::sort from <algorithm> with a custom comparator
std::sort(argv, argv + argc, [](char * const & a, char * const & b) {
return atoi(a) < atoi(b);
});
In modern c++ you can use auto types for lambdas. For string to int convertion I would prefer stoi function over atoi (you can look for differences here). Also worth noting that first argument (argv[0]) is a program name, e.g. ./a.out, so you need to skip it from sorting. Final result could be looks like this:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
int main (int argc, char *argv[]) {
std::sort(argv + 1, argv + argc, [](auto l, auto r){ return std::stoi(l) < std::stoi(r); } );
std::copy(argv + 1, argv + argc, std::ostream_iterator<const char*>(std::cout, " "));
}
If all of command line arguments a unsigned number with fixed digits count you could also sort them like string, i.e. without explicit converting to numbers via std::stoi. In this case std::vector<std::string> could be used:
std::vector<std::string> v(argv + 1, argv + argc);
std::sort(v.begin(), v.end());
There is no need to use lambda or another custom comparator for std::sort.
i am trying to use command line argument in Linux(Ubuntu) in c++ . but it generates run time error : segmentation fault.this program runs with no error in windows .here is my code
#include<iostream>
using namespace std;
int main(int argc , char **argv){
char **ss;
for(int i=0;i<argc;i++){
ss[i] = argv[i];
}
for(int i=0;i<argc ;i++)
cout<<ss[i];
return 0;
}
what is wrong with this code. please help me . thanks in advance.
Your program has undefined behaviour because you did not initialize pointer ss and allocate memory where you are going to copy elements pointed by argv
char **ss; // What value does it have?
for(int i=0;i<argc;i++){
ss[i] = argv[i];
You could do the following way
char **ss = new char *[argc];
for(int i=0;i<argc;i++){
ss[i] = argv[i];
The better way is to use std::vector<std::string>. In this case you could also copy not only pointers to arguments but and also the arguments. For example
#include<iostream>
#include <vector>
#include <string>
int main(int argc , char **argv)
{
std::vector<std::string> v( argv, argv + argc );
for ( const std::string &s : v ) std::cout << s << std::endl;
return 0;
}
If your compiler does not support the range based for statement then you can substitute it for
for ( std::vector<std::string>::size_type i = 0; i < v.size(); i++ )
{
std::cout << v[i] << std::endl;
}
As already answered, you haven't allocated any memory for ss.
Since you're using c++ and not c, you should have the c++ standard library at your disposal:
std::vector<std::string> ss;
ss.reserve(argc); // not necessary
for(int i=0;i<argc;i++)
ss.push_back(argv[i]);
Use the following declaration for ss
#include<iostream>
using namespace std;
int main(int argc , char **argv){
char *ss[argc]; // <--allocate argc count of pointers
for(int i=0;i<argc;i++){
ss[i] = argv[i];
}
for(int i=0;i<argc ;i++)
cout<<ss[i];
return 0;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the proper declaration of main?
I just attended my first c++ exam.
First question was
int main(◊, char ** argv)
Which one of the following suggestions will not work (as first formal parameter substitute for ◊):
a) char argc
b) int argc
c) double argc
d) bool argc
The answer counts 2% in a 4 hour purely handwritten individual exam.
All tools are allowed, accept any programmable device or any means of communication
Who can get this right :)?
Define what "works" means. Any of those might work, but a valid, standard-compliant, well-formed C++ program has either of the following signatures:
int main()
int main(int argc, char** argv)
int main(int argc, char* argv[])
So a), c) and d) are wrong.
Define "will not work" ?
int main(char argc, char ** argv)
{
printf("%d\n", argc);
return 0;
}
./a.out 1 2 3
Output: 4
int main(int argc, char ** argv)
{
printf("%d\n", argc);
return 0;
}
./a.out 1 2 3
Output: 4
int main(double argc, char ** argv)
{
printf("%d\n", *(int*)&argc);
return 0;
}
./a.out 1 2 3
Output: 4
int main(bool argc, char ** argv)
{
printf("%d\n", argc);
return 0;
}
./a.out 1 2 3
Output: 4
Given that the question is asking which one will not work. It would have to be double all the others are integers.
I believe this would be the correct answer because you can't index an array with anything other than an integral value. But that assumes that you actually want to index the argv array.
But what a bad question to ask on a C++ exam.
"int argc" is the correct usage. argc represents the number of parameters passed to the main. So its only int.
This is my code :
#include <iostream>
using namespace std;
int main (int argc, char* argv[])
{
int frstNumb = atoi (argv[1]);
int scndNumb = atoi (argv[2]);
int sum = 0;
sum = frstNumb + scndNumb;
}
Okay, now its working for integers. What do I must do so I can put e.g. "2.5 and 1.2" in my parameters? This program won't calculate that kind of numbers. Help?
Thanks
The arguments are always passed as strings. First of all, change the main function declaration to
int main (int argc, char* argv[])
Note that the return value of main MUST be int, otherwise it is nonstandard.
Second convert argv[1] and argv[2] to integers with either atoi , which, to my taste, is a bit C-ish, although the simplest, or by boost::lexical_cast
e.g.
int i1 = atoi(argv[1]); //#include <cstdlib>
int i1 = boost::lexical_cast<int>(argv[1]); //#include <boost/lexical_cast.hpp>
Yep-- you want atoi().
int frstNumb = atoi(argv[1]);
int scndNumb = atoi(argv[2]);
I'd suggest googling atoi() to see how it works, it will probably help you in the future.
argv[1] and argv[2] are strings. When you do the sum, wrap them with atoi() calls so they are parsed and converted to integers.
Thanks to Mark Loeser and walkingTarget
Your declaration of main() should be:
int main(int argc, char **argv)
or
int main(int argc, char *argv[])
main() should always return an int, and argv should always be an array of strings.
As suggested in other answers, you first need to change declaration of function main - argc represents total number of arguments passed to it and argv is an array of arguments themselves (each argument is an element in array; first element - argv[0] - is path to this executing binary! argv[1] is actually first argument from a command line).
Both in C and C++ you can use atoi or sscanf functions but in C++ you should take advantage of STL stringstream's conversion capabilities:
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char* argv[])
{
if(argc != 3)
{
cerr << "Invalid number of operands" << endl;
return 1;
}
int n1 = 0;
int n2 = 0;
stringstream ss;
ss << argv[1];
ss >> n1;
ss.clear();
ss << argv[2];
ss >> n2;
int sum = n1 + n2;
return 0;
}