Comment about the FAQ

This is a discussion on Comment about the FAQ within the C Programming forums, part of the General Programming Boards category; I thought that the first program here could be written like this: Code: #include <stdio.h> int main(int argc, char *argv[] ...

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048

    Comment about the FAQ

    I thought that the first program here could be written like this:

    Code:
    #include <stdio.h> 
    
    int main(int argc, char *argv[] ) {
        printf("I am the child\n");
        while(--argc)
            printf ("Arg %d %s\n", argc-1, *++argv);
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, both methods have a potential flaw. They never check for argc being zero. There is nothing which dictates that the program's name ever must be stored in argv[ 0 ]. That is to say, nothing says is will be there for sure. To quote a reference:
    C: A Reference Manual, 5th Edition
    Page 303, 9.9 THE MAIN PROGRAM, pharagraph 3.

    ...
    The parameter argv is a vector of pointers to
    strings representing the program arguments. The first string, argv[0], is the name of the program; if the name is not available, argv[0][0] must be '\0'.
    There's no reason you have to have the program name available. As such, argc would be zero. Decrementing it would underflow the variable, and would crash both programs.


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

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You have the wrong quote. argv[argc] must be NULL. Your quote is stating that if the name is not available then argv[0] must be an empty string, which is not NULL. What you want to quote is:

    The value of argc shall be nonnegative.
    argv[argc] shall be a null pointer.
    The standard then goes to describe situations where argc > 0, which is what the quote you gave is part of.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, I have the quote right. If argc is zero, and you decrement it, you'll end up trying to access '++argv' some thousands, or millions, or billions of incrementations past where you're supposed to be accessing it due to variable underflow.
    Your quote is stating that if the name is not available then argv[0] must be an empty string, which is not NULL.
    That's the quote from the book. I don't have a copy of the standard to compare it to. But the point wasn't if it's null or not. The point was you'll increment your variable millions of times past where you should be, simply because argc is zero before you try and use it.


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

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    If argv[0] can never be NULL (it must contain the program name or an empty string), doesn't that mean that argc must always be at least 1?
    If you understand what you're doing, you're not learning anything.

  6. #6
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    how could you not know the program's name (i.e. argv[0] being NULL)?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  8. #8
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    and how do you find such implementation? wouldn't it be logical to use a "normal" implem.?

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    From that link Dave posted:
    - argv[argc] shall be a null pointer.
    Posted by Quzah:
    The first string, argv[0], is the name of the program; if the name is not available, argv[0][0] must be '\0'.
    Hence, argc must always be at least 1. Either that or someone is wrong (Quzah or the information from that link.) Or maybe something changed between standards?
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    If argv[0] can never be NULL (it must contain the program name or an empty string)
    But the standard says that argv[0] will be a pointer to a string only if argc is greater than 0. So if argc is 0 then argv[0] would be NULL, and the other requirements that are dependant on argc being positive do not apply.


    C99
    If they [argc and argv] are declared, the parameters to the main function shall obey the following
    constraints:
    The value of argc shall be nonnegative.
    argv[argc] shall be a null pointer.
    If the value of argc is greater than zero, the array members argv[0] through
    argv[argc-1] inclusive shall contain pointers to strings, which are given
    implementation-defined values by the host environment prior to program startup. The
    intent is to supply to the program information determined prior to program startup
    from elsewhere in the hosted environment. If the host environment is not capable of
    supplying strings with letters in both uppercase and lowercase, the implementation
    shall ensure that the strings are received in lowercase.
    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.
    The parameters argc and argv and the strings pointed to by the argv array shall
    be modifiable by the program, and retain their last-stored values between program
    startup and program termination.
    Last edited by the pooper; 08-08-2005 at 12:10 PM.

  11. #11
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    right, but still, how can the program name not be available from the environment. you call a program by it's name afterall, don't you?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    I can't provide a practical example either. But the point is that it is allowed to happen. Programming would be so much easier if we allowed ourselves to ignore errors that were uncommon. But robust programming means that we must consider all the possibilities.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Thanks pooper. That's what I was wondering. I don't have a copy of the standard.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    C89 (Technically only a draft, but identical to the final release as far as I know. If anyone knows where C89 is freely available, spread the word.)

    and C99 (This one's the real thing.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 02:17 PM
  2. Comment using // or /* &&& */
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 03-16-2005, 02:45 PM
  3. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 12:21 PM

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