Thread: Why does argc need to be 2?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question Why does argc need to be 2?

    I was looking through the tutorials for c, and I was on the one about command line processing. I believe it was #14, but I'm not sure.

    The example was reading in a file, but it needed argc to be equal to 2 in order to work right. The example checked for that value and everything.

    This website is not the first place I've seen someone write their code like that before.

    My question is this: Why does it have to be exactly 2? Couldn't it be 1 instead? Possibly 3 or 4? But it would at least seem to me to be more reasonable to make sure the count is 1 instead of 2. Afterall file names don't always have white space in them.

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    The first argument is always the filename of the program, so if you wanted to act on one argument, say a number like 4, then argv would contain:-
    argv[0] = "myprogram.exe"
    argv[1] = "4"

    So, two arguments it is.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Basically, it needs to be 2 if you only have one arguement you want to pass in. If argc == 5 then there are 4 arguments that were passed in.

    [edit] Pwnt by SMurf... [/edit]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The first argument is always the filename of the program
    Not always. argv[0] can be an empty string.

    >Why does it have to be exactly 2?
    argc doesn't have to be exactly 2. It only has to be at least the number of items you expect in the command line. So if you access argv[1], argc must be at least 2. If you access argv[2], argc must be at least 3. argv[0] is reserved, as SMurf described.
    My best code is written with the delete key.

  5. #5
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    You can have more args just program it as needed. You can also have no args.

    int main(void)

    just remember to make your prog handle the args you get.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    Yes, argc and argv are only used as parameters of main and sent to the program via the command line, which is pretty cool when working in a DOS-based environment. So, when you type the following in the command line: "program.exe 1 2 3 4" it goes like this:
    argc = 5
    argv[0] = program.exe
    argv[1] = 1
    argv[2] = 2
    argv[3] = 3
    argv[4] = 4

    Take care because the 1, 2, 3 and 4 entered are defined as char so if you want the program to operate with them as integer or real you have to convert it to int or float or whatever crosses your mind, but I'm sure you understood it after the first reply, the rest is just us quasi programmers huffing and puffing

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Not sure if I remember right, but I don't think argv[0] is guarenteed to be the program name.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by chrismiceli
    Not sure if I remember right, but I don't think argv[0] is guarenteed to be the program name.
    It's amazing that someone already mentioned that in this thread:
    Quote Originally Posted by Prelude
    >The first argument is always the filename of the program
    Not always. argv[0] can be an empty string.
    And in case there's still any question, I quote the C99 standard (excerpt from sec. 5.1.2.2.1):
    If the value of argc is greater than zero, the string pointed to by argv[0]
    represents the program name; argv[0][0] shall be the null character if the
    program name is not available from the host environment.
    If the value of argc is
    greater than one, the strings pointed to by argv[1] through argv[argc-1]
    represent the program parameters
    So, like Prelude said, it's either the program name or an empty string.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by itsme86
    So, like Prelude said, it's either the program name or an empty string.
    Not quite. It either represents the program name or it is an empty string.

    There are all sorts of possibilities in that, depending on how the word "represents" is interpreted. For example argv[0] can be any of these;

    1) the unadorned name of the executable (eg "program")

    2) the name of the executable file (eg "program.exe" under windows)

    3) the fully qualified path of the executable file (eg "c:/directory/subdirectory/program.exe" or "C:\directory\subdirectory\program.exe" under windows, or drive:[directory.path]program.exe on an old VAX/VMS system).

    4) an alias for the executable (eg several unix shells support aliases, and could be passed as argv[0] by the shell, rather than the actual name of the executable)

    5) the name of something that is linked to the executable (eg symbolic links under unix: the name of the link can be passed as argv[0]).

  10. #10
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Sorry, I didn't read the whole thread, I just saw that stormtrooper said that argv[0] = program.exe
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    argv, by definition, contains information from a program's environment, thus it will be completely determined by the environment, and can be anything the host system wants it to be. In fact, it would be perfectly legal for a system to pass argv[x] as NULL-terminated arrays of '1's and '0's, however contrived, illogical and stupid the idea might be.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Only if those 1s and 0s represented the program name...
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. Replies: 18
    Last Post: 06-21-2003, 10:57 AM