Solved

Input arguments

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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 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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

948 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

21 Experts available now in Live!

Get 1:1 Help Now