Thread: arguments from a command line

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    23

    arguments from a command line

    Hey,

    I am trying to implement arguments from the command line ..

    I need to pass the argument into a function . .

    the problem i have is that i need the argument to be an integer ..

    i only know how to input it in as a char[]

    can anyone tell me how to make it read in as an int ..

    Matt

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Read As A String and Convert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void foo(int value)
    {
       printf("the value passed to foo is %d\n", value);
    }
    
    int main(int argc, char *argv[])
    {
       if(argc > 1)
       {
          foo(atoi(argv[1]));
       }
       return 0;
    }
    
    /*
    C:\Test>test 15
    the value passed to foo is 15
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    void func1(int);
    
     int main (int argc, char *argv[])
    {
    /* ... */
    func1 ( atoi(argv[3]) );
    /* ... */
    }
    Easist way but you might want to look into the problems with atoi().

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always read the FAQ. Beyond that, search the board for command line arguments, and try not to stagger under the weight of the load of hits you get.

    [edit]Curses, foiled again! (By two people. :P)[/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM