argc

This is a discussion on argc within the C Programming forums, part of the General Programming Boards category; Hi! I did a program that generates a random character and the user have to type the same character on ...

  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
    21,781
    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 "".".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,781
    hmm... doesnt that imply that argv[1] will always point to the first user supplied argument?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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);
    }

Page 1 of 2 12 LastLast
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, 05: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

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