Like Tree1Likes
  • 1 Post By Cynic

Command line arguments and passing them

This is a discussion on Command line arguments and passing them within the C Programming forums, part of the General Programming Boards category; Hey everyone, this is my first post here. I am currently learning C and have been writing a program that ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    Command line arguments and passing them

    Hey everyone, this is my first post here. I am currently learning C and have been writing a program that will take a command line argument and save it some where in the program.

    I have googled tutorials and such but I guess I am just too dense to get to where I need.

    Anyway, here is my code. What did I do wrong? What did I not learn?
    Code:
    #include <stdio.h>
    
    int main (int a, char *file[])
    {
    
    
        if (a>0)
        {
            char gfile[256] = file[1];
            printf("%s", gfile[]);
        }
    
        return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Firstly, giving the arguments to main your own special names is not a great idea. They are conventionally called argc and argv, and it's best to use those names since everybody recognizes them.

    Secondly, a will always be > 0 since the first element of the argv (the "argument vector") will be the program name (or possibly an empty string).

    Thirdly, you're not dealing with the strings properly.

    Fixing all that, your code should look like this:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        if (a > 1) {
            char gfile[256];
            strcpy(gfile, argv[1]); // technically this should be strncpy as strcpy can overflow
            printf("%s\n", gfile);
        }
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You can not assign a string like that, file[1] is a pointer and gfile is an array of 256 chars. You can assign it to a pointer, but you don't need to you can just print file[1] directly. Keep in mind that a will always be 1 for the name of the program but arrays are indexed from 0 in C so even if a > 0 it's not guaranteed that file[1] will be valid.

    Also the convention is to call the arguments to main argc and argv, (presumably for argument count and argument vector).

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Subsonics View Post
    Also the convention is to call the arguments to main argc and argv, (presumably for argument count and argument vector).
    I always think of it as count and values.

    Using your own names for argc/argv is not that bad a thing IMO, if the names are equally (or more) meaningful. Which "a" is not, so that is bad.

    Anyway, Fourthly,

    I am currently learning C and have been writing a program that will take a command line argument and save it some where in the program.
    Umm, they already are saved somewhere in the program. They are not going to disappear. Is there a reason you want to make a second copy?
    Last edited by MK27; 04-07-2012 at 08:58 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Thank you all for the tips and help!

    Quote Originally Posted by MK27 View Post
    Umm, they already are saved somewhere in the program. They are not going to disappear. Is there a reason you want to make a second copy?
    I am learning C, I would like to make a copy of them because it would hopefully help me get a grasp on a few things. It was just something to help me come to terms with the language.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by somebody View Post
    I am learning C, I would like to make a copy of them because it would hopefully help me get a grasp on a few things. It was just something to help me come to terms with the language.
    Fair enough. As long as you are not confused about the fact that argv is just an array of regular strings, and not anything unusual requiring you copy them.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by MK27 View Post
    I always think of it as count and values.
    Well, both will work.

    Quote Originally Posted by MK27 View Post
    Using your own names for argc/argv is not that bad a thing IMO, if the names are equally (or more) meaningful.
    It's not bad in a different way that breaking conventions in other cases, no. It looks odd and might raise an eyebrow somewhere and maybe lead to a mistake, dunno.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you call them argc and argv then everyone knows what they are and nothing new has to be noticed and remembered to read the code. That seems optimal to me.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by oogabooga View Post
    Secondly, a will always be > 0 since the first element of the argv (the "argument vector") will be the program name (or possibly an empty string).
    Under what condition would it be an empty string? From my experience, argv[0] has always been the path + program name + full command line options.

    @OP, you may need to distinguish your options if there is a difference between them, such as an input file vs. a program behavior modifier. The convention appears to be '/' for MS style command line options and '-' for unix style with input files being left as is (though you could technically use "-ifilename" to indicate an input file.

    If there is only one option, say an input file name, then there is no issue. I often use this in the initial stages of development with semi-hard coded options and work on the command line later.
    Last edited by Cynic; 04-07-2012 at 09:28 AM.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Under what condition would it be an empty string? From my experience, argv[0] has always been the path + program name + full command line options.
    I'm just going by this Question 19.31.

    And it certainly never contains the "full command line options".

    From my experience
    Not good enough. You've used every system? Every compiler?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by oogabooga View Post
    And it certainly never contains the "full command line options".
    You're right, it does not. I always thought it did, but then I've never had an occasion to make use of argv[0], so I never explored it.

    Not good enough. You've used every system? Every compiler?
    No, which is why I said "from my experience" and not something on the order of "it is without question across all possibilities."
    Last edited by Cynic; 04-07-2012 at 10:02 AM.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Cynic View Post
    I always thought it did, but then I've never had an occasion to make use of argv[0], so I never explored it.
    So your "experience" is zero. Perhaps you should have said that.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by oogabooga View Post
    So your "experience" is zero. Perhaps you should have said that.
    If you're going to start being demeaning, I will escalate and return it back and I can get extremely nasty and obscene.
    Last edited by Cynic; 04-07-2012 at 11:07 AM.
    MK27 likes this.

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You said "in my experience" which, believe it or not, implies that you actually have some. But then you just pulled something out of your butt. How is that helping anyone? People are pretending to know more than they do all the time here, and it's so easy to see through that it's pathetic. If you're going to try to help people then you need to admit your limitations. Don't be one of the poseurs.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguments
    By Jasper in forum C Programming
    Replies: 8
    Last Post: 08-07-2009, 11:24 AM
  2. Command line arguments...
    By AeonMoth in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2007, 08:41 AM
  3. command line arguments
    By St0rM-MaN in forum C Programming
    Replies: 10
    Last Post: 05-07-2007, 05:40 AM
  4. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 04:46 PM
  5. Command line arguments
    By Kelvin in forum C Programming
    Replies: 3
    Last Post: 07-12-2002, 09:22 AM

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