Discussion :: Command Line Arguments - Find Output of Program (Q.No.3)
|Ranjit Singh said: (Aug 9, 2010)|
|Can any describe it how the error will generate?|
|Brindha said: (Sep 7, 2010)|
|Instead of arg atoi(arg) is to be put.
argv will contain "1" (as string), we have to convert into integer with atoi() function.
|Bhagat Singh said: (Nov 26, 2010)|
|argv is not declare automatically
so we never used argv
|The Prodigy said: (Dec 29, 2010)|
|The command line arguments are taken as strings (char *) and not integers.|
|Sweety said: (Jan 19, 2011)|
|@Brindha : perfect|
|Navneet said: (Feb 9, 2011)|
|argv is argument vector and it is string. here it is tried to concatenate string. C does not provide the string concatenation it this way. So that error!|
|Kavtihha said: (Jul 17, 2011)|
|I'm getting the error even if I put atoi() function. What to do?|
|Mukesh said: (Jul 18, 2011)|
|All the confusion accounts to the generosity of our loveable C compiler. The C standard says "standard conversions" but never defines an upper boundary for the conversions. Hence the compiler is let loose - it tries and performs all sorts of conversions and when successful(which it always is!!) says "Fine fine the program has no errors - yeah but this is a warning... "
For example, int a = "1";
"1" is a char*, so the RHS is an address to the string. The compiler converts this address to an integer and assigns it to int.
But the problem is the binary operator : +
The + operator has been defined to work only with a predefined set of types, and not to work on a predefined set of types. For instance, C standard says Ok + operator, you can work on integers, floats, doubles, blah.. but you shouldnt work on two pointers(since I forbid arithmetic operations with two pointers).
So if you write :
int j = argv + 2; // WARNING, NO ERROR
int k = argv + "2"; // 2 pointers?? error x-(
So just make sure you do the appropriate type-casts(like the one answered above)
|Rupinderjit said: (Dec 7, 2011)|
|argv[i] is pointer to strings in an argument.So argv,argv or whatever are pointer to respective strings pointed to by argv[index].
Since if we look at pointer arithmetic, then two pointer's addition is not allowable.
So there is an error.
But if we change type using typecast,and after on changing type to integer, these are then merely an constant int value.
So do the answer.
|Manish.Sargaiyan said: (Sep 14, 2013)|
|I agreed with atoi we have to use to convert into integer.
What about argv ?
Please anyone clear this doubt...!!!
|Siri said: (Dec 10, 2014)|
|Cmd line arguments passed is sample 1 2 3 (which is your filename followed by args).
argv=> Contains your executable file name/ program name (in this case sample is the program name).
argv=> Will have 1.
argv=> Will have 2.
argv=> Will have 3.
And all these will be stored in the form of strings. So to add convert the args to integer using atoi function and then add.
|Abhishek Sakariya said: (Feb 3, 2015)|
|I am focusing on Linux environment. Is it possible to write.
Char*argv instead of char**argv?
|Yogesh said: (Feb 12, 2015)|
|But for me it prints the output as 1 use online C compiler from here.|
|Aadesh said: (Nov 5, 2015)|
You can use it if you want to pass a single argument in command line arguments. But generally we pass more than 1 so we need to take array of pointer to char.
Post your comments here:
Email : (optional)
» Your comments will be displayed only after manual approval.