Thread: Reading arguments from command-line

  1. #1
    Registered User
    Join Date
    Oct 2016

    Reading arguments from command-line

    I'm new to programming and to C and I'm trying to learn some new things. I want to make program, which will find minimum and/or maximum number. As far as I was making researches it would be the best to sort number with bubble sort, and than print out the first or the last number of that sorted array.
    What I don't know is how to pass all those numbers to array via therminal on Linux.
    For example:
    $ ./findnumber -n 4-m -t int 7 4 3 1 5 2 8 
    Minimum number is:4
    -n means find the "n" max/min number of sorted array -m means find the smallest number -t means type of numbers (integer or float)
    I find out also, that you have to save those arguments into argc, argv, but I don't know how to go further.

    Thanks for help.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Sorting the whole array with one of the slowest algorithms in existence to get the min and max values is pointless. Looping once through the array, saving the smallest and largest number you'd find thus far is easier, more intuitive and in fact faster.

    About your question, you can pass arguments to a C program by declaring main like this:
    int main(int argc, char* argv[])
    argc is the number of arguments passed to your program.
    argv is an array of pointers/strings, each being another argument.

    argc is always one greater that what you gave, because argv[0] is always the name/path of your program.

    You could build a rudimentary parser, but since you're asking us about linux, you can use getopt().
    Last edited by GReaper; 10-27-2016 at 01:52 PM.
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    New York, NY
    Quote Originally Posted by GReaper View Post
    You could build a rudimentary parser, but since you're asking us about linux, you can use getopt().
    XGetopt is available for Windows, and also read the comments as a version may be shipped with Microsoft compilers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguments
    By avgprogamerjoe in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2008, 03:21 AM
  2. Command line arguments
    By malooch in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2006, 12:36 AM
  3. reading in command line arguments from a file?
    By g1i7ch in forum C Programming
    Replies: 30
    Last Post: 06-22-2006, 01:35 PM
  4. reading command line arguments into buffer
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 08:08 PM

Tags for this Thread