Thread: argc

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    29

    argc

    Hi!

    I did a program that generates a random character and the user have to type the same character on the keyboard.
    What i want to do is to use ARGC to define the number of characters that the program needs to generate one at once wich is 3 at my program.
    The user must type the character after each one and not at the end of the tree characters.

    Ex:
    Output: t \\ Random character
    t \\ Introduced by user

    Output: g \\ Random character
    d \\ Introduced by user

    Output: s \\ Random character
    s \\ Introduced by user


    this is my code:

    Code:
    #include <stdio.h>
    
    /**************************************
    função generate_char()
    **************************************/
    
    static char first = 1;          // Global variables
    char x;
    
    
    char generateChar() {
    
    char caracter;
    
    if (first) {
    first = 0;
    srandom(time(NULL));
    }
    
    x = random() % 26 + 'a';
    printf("%c\n", x);
    
    scanf("%c", &caracter);
    
     if(caracter == x) {
       printf("Equal");
     }
     else 
       printf("Not Equal");
    }
    
    main(int argc, char *argv[]) {
      generateChar();
    }
    Thanks

    Calavera

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    #include <stdlib.h>

    int charCount = _atoi(argv[0]);

    .......maybe
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Umm, wouldn't you want argv[1]? Isn't argv[0] the name that invoked the program?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    hell, i dunno....i've never used argc and argv before
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Perhaps if you wish to be safe, you'd do:
    Code:
    if( argc > 0 )
    {
        cCount = atoi( argv[ argc - 1 ] );
    }
    else
    {
        cCount = some_default_value;
    }
    There's a section in the FAQ about using command line arguments if you wish to read it. It's also there even if you don't wish to read it.

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

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    Argv[1] will be the program
    Argv[2] will be the number of tests

    Ex: character(name of the program) 5(Number of times of the test)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Calavera
    Argv[1] will be the program
    Argv[2] will be the number of tests

    Ex: character(name of the program) 5(Number of times of the test)
    No it won't. If it happens to be there at all, the name of the program will reside in argv[0] and the first of the user supplied arguments will begin on argv[1]. If it doesn't, then the user supplied arguments will begin on argv[0].

    I suggest YOU read the FAQ entry.

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

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    You wrigth!
    My mistake.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by quzah
    No it won't. If it happens to be there at all, the name of the program will reside in argv[0] and the first of the user supplied arguments will begin on argv[1]. If it doesn't, then the user supplied arguments will begin on argv[0].

    I suggest YOU read the FAQ entry.

    Quzah.
    I have never heard or read (before this) that in some cases the user-supplied arguments can begin with argv[0]

    Yes, I have read the FAQ entry for command line arguments:

    If the value of argc is greater than 0, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which will be the command line arguments. argv[0] will contain the program name, argv[1] the first command line arg, argv[1] the second and so on.
    Is it (am I) wrong?

    Regards,

    Dave

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... I'm not sure about C, but for C++ the standard reads "argv[0] shall be the pointer to the initial character of a NTMBS (nul terminated multi-byte string) that represents the name used to invoke the program or "".".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C: A Reference Manual, 5th Edition
    Page 303:

    When arguments are declared, those arguments are set up by the execution enviorn-
    ment and are not directly under control of the C programmer. The parameter argc is the
    count of the number of "program arguments" or "options" supplied to the program when it
    was invoked by a user or another program. 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'. The string argv[i],
    for i=1, ..., argc-1, is the ith program argument. Standard C requires that argv[argc]]
    be a null pointer, but it is not so in some older implementations. The vector argv and the
    strings to which it points must be modifiable, and their values must not be changed by the
    implementation or host system during program execution. If the implementation does not
    support mixed-case strings, then the strings stored in argv must be in lower case.
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... doesnt that imply that argv[1] will always point to the first user supplied argument?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by quzah
    No it won't. If it happens to be there at all, the name of the program will reside in argv[0] and the first of the user supplied arguments will begin on argv[1]. If it doesn't, then the user supplied arguments will begin on argv[0].Quzah.
    (Emphasis added)


    You said that user supplied arguments can begin on argv[0]. The FAQ and the quote from the reference do not support this.

    Regards,

    Dave

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah. Looks like I misstated. I was thinking when I wrote it, that you could have user arguments without having the name of the program there, because I knew that you could have an instance where there was no program name in argc[0]. I'll blame it on the beer.

    Personally I've never seen an instance where there is no program name specified, which is why I was assuming that arguments would simply take the place of it. (Then I looked up the reference for my last post.)

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

  15. #15
    Registered User
    Join Date
    Nov 2004
    Posts
    17
    Code:
    #include <stdio.h> 
    
    int printcommandline(int argc, char* argv[])
    {
        for(int c = 0; c < argc; c++)
        printf("%d->%s\n", c+1, argv[c]);
        return 5150;
    }
    
    int main(int argc, char* argv[])
    {
        return printcommandline(argc, argv);
    }

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