Reputation: 397
So I'm pretty new to C and am trying to learn how to make it take command line inputs, I typed it in what I'm hoping is the correct syntax and I'm trying to get it so you type in 2 numbers and it returns those two numbers added together. The program does compile but when I try to run it, it takes the parameters and they're printed completely wrong, almost as if they're printing the memory addresses.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int addition(int a, int b);
int main(int argc, char *argv[]) {
if(argc != 3) {
printf("You must enter 2 numbers!");
return TRUE;
}
else {
int answer = addition((int)*(argv[0]), (int)*(argv[1]));
printf("%d + %d = %d", argv[0], argv[1], answer);
return FALSE;
}
}
int addition(int a, int b) {
return a + b;
}
So what am I doing wrong? The output when I put in 2 3 is:
155 + 50 = 165
Upvotes: 0
Views: 102
Reputation: 123458
First of all, your input parameters start at argv[1]
; argv[0]
is always the string that you used to invoke the program.
Secondly (int)*(argv[1])
doesn't convert argv[1]
from a string to an integer; what it does is convert the character code of the first character in argv[1]
from char
to int
. Assuming ASCII, if argv[1] is "10"
, then that line would convert the character value '1'
(ASCII 49) from type char
to type int
.
You'll need to use a library function like atoi
or strtol
to convert your arguments from strings to integers, like so:
int a = (int) strtol( argv[1], NULL, 10 );
int b = (int) strtol( argv[2], NULL, 10 );
...
int answer = addition( a, b );
Read the strtol man page for an explanation of the parameters.
Upvotes: 1
Reputation: 16107
int answer = addition((int)*(argv[0]), (int)*(argv[1]));
printf("%d + %d = %d", argv[0], argv[1], answer);
I'm afraid this won't work as you expect. argv[x]
s are pointers to chars, rather than integers. For example, if you in put "2 3", argv[0]
points to a string representing the name of the running program(or a null pointer if the name isn't available), argv[1]
points to "2", argv[2]
points to "3", and argv[argc]
(namely argv[3]
) points to a null pointer.
To convert a string, which is essentially a const char array, simply casting doesn't work. You can call atoi()
function instead. Here is the code that works (without error handling):
#include <stdio.h>
#include <stdlib.h>
int addition(int a, int b);
int main(int argc, char *argv[]) {
if(argc != 3) {
printf("You must enter 2 numbers!");
return EXIT_FAILURE;
}
else {
int a = atoi(argv[1]), b = atoi(argv[2]);
int answer = addition(a, b);
printf("%d + %d = %d", a, b, answer);
return EXIT_SUCCESS;
}
}
int addition(int a, int b) {
return a + b;
}
When inputing "2 3", the output is "2 + 3 = 5".
Something irreverent: Why using TRUE
and FALSE
when returning? To avoid magic numbers, you can use EXIT_SUCCESS
and EXIT_FAILURE
.
Upvotes: 1
Reputation: 2478
Replace these lines:
int answer = addition((int)*(argv[0]), (int)*(argv[1]));
printf("%d + %d = %d", argv[0], argv[1], answer);
with:
int answer = addition(atoi(argv[1]), atoi(argv[2]));
printf("%d + %d = %d", atoi(argv[1]), atoi(argv[2]), answer);
As said in another answer by David Hoelzer, the arguments contains null terminated byte strings. Using atoi()
will convert the strings into integers.
Use argv[1]
and argv[2]
instead since they will actually be the ones that will contain the values you entered. As per the ISO C standard specification:
If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment.
argv[0]
would contain the name of the program.
More information regarding argv[0]
as the name of the program can be found in this StackOverflow answer.
With regards to the code, you could always store the arguments in a variable like this:
int a = atoi(argv[1]);
int b = atoi(argv[2]);
and then pass them to your addition()
function and use it in the printf()
part.
Upvotes: 2
Reputation: 16331
The values that are being passed to you via the **argv
are not integers, they are null terminated byte strings. In order to perform the addition, you would first need to use something like atoi()
to convert them to integers. Otherwise you are performing numeric addition on character arrays.
Upvotes: 2