Input arguments


I'm pretty new to C, but having a slight problem with bringing in variables from the command line. To run the program you type in:   test 1 2 3

and the start of my code looks like this:

int main(int argc, char **argv)

So now I really want to be able to store the value as an integer, but at the moment I'm just trying to print out to the screen, but I can't get this right. How come the following won't work:

printf("number 1 is %d \n", argv[1]);
printf("number 2 is %d \n", argv[2]);
printf("number 3 is %d \n", argv[3]);

Can anyone help?

Thanks in advance,
Who is Participating?
fridomConnect With a Mentor Commented:
argv is a vector of "strings"

So you have to use the proper modifier.

printf("argc[1] = %s\n", argv[1]); ...

If you really want to use the %d you have to make an interger from a string.
Lookup atol or maybe sscanf.


dj_humpygAuthor Commented:
Many thanks for the quick reply.

I understand now, thanks for the clarification. It's now all sorted and working, it printed to the screen fine and I used the atoi function.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.