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?

Community
  • 1
  • 1
Kaity
  • 337
  • 1
  • 5
  • 12
  • Doesn't your textbook provide examples on using command arguments? – mmcdole Feb 07 '09 at 06: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 07 '09 at 07:24
  • Why did you open 2 topic on the same subject? Plz close one. – qrdl Feb 07 '09 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 07 '09 at 16:37

7 Answers7

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!]

Community
  • 1
  • 1
Paul Beckingham
  • 14,495
  • 5
  • 33
  • 67
  • 45
    I used to know how to fish, but have forgotten. Would rather be reminded than patronized. – jwg Jul 09 '18 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.

Mike
  • 3,219
  • 23
  • 29
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!

PTBNL
  • 6,042
  • 4
  • 28
  • 34
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

theBuzzyCoder
  • 2,652
  • 2
  • 31
  • 26
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 
timrau
  • 22,578
  • 4
  • 51
  • 64
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'.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Denise Ignatova
  • 465
  • 4
  • 7
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)

mattsap
  • 3,790
  • 1
  • 15
  • 36