arguments from a command line

This is a discussion on arguments from a command line within the C Programming forums, part of the General Programming Boards category; Hey, I am trying to implement arguments from the command line .. I need to pass the argument into a ...

  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,006
    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, 02:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 04:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 07:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 01:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21