?
Solved

Integer Command line arguments in C?

Posted on 2003-03-15
5
Medium Priority
?
413 Views
Last Modified: 2010-04-17
Hi,

  This question is about C Programming. How can i convert the arguments accepted from main and convert it to integer?
For example, consider this simple program :

#include<stdio.h>

void main(int argc, int **argv)
{
  int a;
  a=argv[1];
  printf("argv[%d] %s",1,argv[1]);
  printf("\n%d",a);
 
}
(The program compiles and runs fine.)

I have assigned the integer argv[1] to "a" which is the first argument at the command line.

when i look at the results i get a junk character as the output for the second " printf " statement. When i use %s to print the argv[1] it prints the correct number, but if i use %d it prints a junk character. In this case how can i assign an argument accepted as an integer in the command line to an integer variable?
I have tried typecasting it : a=(int)argv[1] does not work, neither does a=(char*)argv[1]; work. where am i doing a mistake?

Please explain how this works.
0
Comment
Question by:snmurthy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:Hermetic
ID: 8144190
been a while since I did command line arguments.
I think argv[1] is a pointer to a string.
try a=strtol(argv[1]);
0
 
LVL 11

Accepted Solution

by:
bcladd earned 140 total points
ID: 8144240
atoi (ascii to integer) or atol (ascii to long) will work; you want to be careful that there are actually as many command-line parameters as you expect.

-bcl
0
 
LVL 1

Expert Comment

by:MatthewL
ID: 8145424
Hi Snmurthy,

Take a look at sscanf as well.  Everything that you can do with scanf from stdin can also be done from within a string.

You can find an example at http://www.cplusplus.com/ref/cstdio/sscanf.html

Matt
0
 

Expert Comment

by:CleanupPing
ID: 9447122
snmurthy:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Make the most of your online learning experience.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress

752 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