24

How can make it so the program reads any two integers input before the program is run?

I want the output to look like this, with x and y being any variables typed in (I am using Cygwin):

$ ./a x y

(product of x and y)

(sum of x and y)

I used int main(int argc, char *argv[]). I tried to assign argv[2] to x and argv[3] to y, but when I compile the program it says assignment makes integer from pointer without cast. What does this mean and how do I fix it?

4
  • Doesn't your textbook provide examples on using command arguments?
    – mmcdole
    Feb 7, 2009 at 6:05
  • I am using c language. We have only covered the first three chapters of the book, and the chapter for this is halfway through and is total gibberish to me. The teacher covered this when I was absent so I am clueless!
    – Kaity
    Feb 7, 2009 at 7:24
  • Why did you open 2 topic on the same subject? Plz close one.
    – qrdl
    Feb 7, 2009 at 13:05
  • on the update: Could you please post your code. If you view my example below, that compiles for me in gcc without errors or warnings. Also, please let us know what compiler you are using. Thanks.
    – Mike
    Feb 7, 2009 at 16:37

7 Answers 7

56

Assuming the C language:

  • Command line arguments are found in the argv array - argv[1], argv[2] etc.
  • Converting a string argument to an integer can be done with the atoi function.
  • Output can be done with the printf function.

[Trying to teach you to fish, rather than providing a fish. Good luck!]

1
  • 45
    I used to know how to fish, but have forgotten. Would rather be reminded than patronized.
    – jwg
    Jul 9, 2018 at 10:59
36

Assuming that you are using bash, you can use $1, $2, etc for those arguments. If, however, you are useing C, you're code should looks something more like this:

#include <stdio.h>
#include <stdlib.h>

main(int argc, char *argv[]) {
    if(argc<=1) {
        printf("You did not feed me arguments, I will die now :( ...");
        exit(1);
     }  //otherwise continue on our merry way....
     int arg1 = atoi(argv[1]);  //argv[0] is the program name
                                //atoi = ascii to int
     //Lets get a-crackin!
 }

Hope this helps.

0
5

Firstly, if you run your C program as

./a x y

then a is argv[0], x is argv[1], and y is argv[2], since C arrays are 0 based (i.e. the first item in the array is indexed with 0.

Realize that argv is an array (or I've always thought of it as an ARGument Vector, though you might think of it as an array of ARGument Values) of character string pointers. So, you need to convert the strings to integers. Fortunately, C has library functions to convert ASCII to integer. Look at the stdlib.h documentation.

Good luck!

4

My code would look like this.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    // argc is number of arguments given including a.out in command line
    // argv is a list of string containing command line arguments
    int total = 0;
    int i;
    char *value;
    for(i = 1; i < argc; i++)
    {
        // The integers given is read as (char *)
        value = argv[i];
        printf("Command line index: %d value: %s in ascii: %d\n", i, value, *value);

        // Convert ascii to integer.
        // atoi function is defined in stdlib.h
        total += atoi(value);
    }
    // .2f limits the decimals to two digits after '.'
    printf("Total of given integers is %d\n", total);
}

enter image description here

2
int arg1 = argv[1];

Will not work because it is an array of pointers which holds all the addresses of argv[0]....argv[n] to get the value of argv[..] suppose argv[1] you have to write:

int n=*argv[1]-'0'; // Direct atoi 
1

Simply using atoi() will convert the char type console input into int

   int main(argc, char* argv[])
   {
      int first = atoi(argv[1]);
      printf("%i", first);
   }

If you ask why argv[1] instead argv[0], the answer is the first ever argument is the name of your executable file ./some 2 int this case argv[0] will point to 'some'.

0

In command line arguments, char*argv[] is string type. We need to convert it into integers. We do this by type casting but in oop we do this by the atoi function(method), it works like typecasting(means method of convert one data type to other)

2
  • 2
    char* argv[] is char pointer pointer--here, it is a char * array rather than a string type.
    – mattsap
    May 11, 2016 at 20:38
  • there are no string types in C ,basically strings are char arrays -arrays of characters char[] Aug 5, 2021 at 6:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.