Solved

Input arguments

Posted on 2006-11-23
2
280 Views
Last Modified: 2010-04-15
Hi,

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,
DJ.
0
Comment
Question by:dj_humpyg
2 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 275 total points
ID: 18003742
argv is a vector of "strings"

So you have to use the proper modifier.
Try

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.

Regards
Friedrich

0
 

Author Comment

by:dj_humpyg
ID: 18003808
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.

Thanks,
DJ.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question