How to multiply two numbers supplied as command line arguments in C

1.3k Views Asked by At

I am trying to find the multiple of two numbers supplied as arguments in the command line using the following code:

#include <stdio.h>

/**
 * main - Entry point for my program
 * @argc: This is the number of arguments
 * @argv: This is the array of arguments
 *
 * Return: Zero upon program success
 */

int main(int argc, char *argv[])
{
        int i;

        int result = 1;

        for (i = 1; i < argc; i++)
        {
                result = result * argv[i];
        }
        printf("%d\n", result);
        return (0);
}

But as I try to compile my code, I get the following error:

3-mul.c: In function ‘main’:
3-mul.c:19:19: error: invalid operands to binary * (have ‘int’ and ‘char *’)
   19 |   result = result * argv[i];
      |                   ^ ~~~~~~~
      |                         |
      |                         char *

How do I get around this error?

1

There are 1 best solutions below

0
Andreas Wenzel On

The line

result = result * argv[i];

will not work, because argv[i] is not an integer, but it is of type char *, i.e. a pointer to a string. It does not make sense to multiply an integer with a pointer.

What you want to do is to convert the string pointed to by argv[1] to an integer, and then multiply that integer with result. A simple way to do this would be to use the standard library function strtol, by changing the line

result = result * argv[i];

to

result = result * strtol( argv[i], NULL, 10 );

and by adding #include <stdlib.h> to the start of the file.

If the conversion fails, then the function strtol will simply return 0, causing your final result to also be 0. Also, if the user enters a number that is so large that it is not representable as a long, then the function strtol will simply return the largest representable number. If that is not what you want, then you probably want to print an error message instead:

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

int main( int argc, char *argv[] )
{
    long result = 1;

    for ( int i = 1; i < argc; i++ )
    {
        long num;
        char *p;

        //attempt to convert argv[i] to integer
        errno = 0;
        num = strtol( argv[i], &p, 10 );

        //verify that conversion was successful
        if ( p == argv[i] )
        {
            fprintf( stderr, "Conversion of argv[%d] failed!\n", i );
            exit( EXIT_FAILURE );
        }

        //check for range error
        if ( errno == ERANGE )
        {
            fprintf( stderr, "Argument of argv[%d] out of range!\n", i );
            exit( EXIT_FAILURE );
        }

        //conversion was successful and the range was ok, so we
        //can now use the converted number
        result = result * num;
    }

    //print the sum
    printf( "%ld\n", result );

    return EXIT_SUCCESS;
}

For the command-line arguments

3 abc

this program has the following output:

Conversion of argv[2] failed!

For the command-line arguments

3 500000000000000000000

this program has the following output:

Argument of argv[2] out of range!

For the command-line arguments

3 5

this program has the following output:

15

For the command-line arguments

3 5abc

this program has the following output:

15

This program is still not perfect, though, because, as shown above, it is accepting the command-line argument 5abc as a valid representation for the number 5. If you want that argument to be rejected too, then the program would have to perform further validation tests.