Solved

Input arguments

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

758 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

20 Experts available now in Live!

Get 1:1 Help Now