Solved

Input arguments

Posted on 2006-11-23
2
270 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now