Thread: how to use argc and argv

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    how to use argc and argv

    what is the different in use between
    Code:
    main(void)
    and
    Code:
    int main(int argc, char* argv[])
    i knew that
    argc :an integer containing a count of the number of words the user typed
    argv :an array of pointers to strings, these strings are the actual words the user typed or
    an exact copy of them.

    but i don't understand what is the importance of argc and argv and how to use them in my program . ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When you run a program, you are allowed to type other things as well. For instance, when you type something like
    Code:
    C:\> ren foo.c foo.bak
    you are running the command ren (or rename) but with two command-line arguments, "foo.c" and "foo.bak".

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Here's a simple and easy tutorial on the usage of argc and argv.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salahuddin View Post
    but i don't understand what is the importance of argc and argv and how to use them in my program . ?
    Pretty much any way you like.... main() is a function like any other. argc and argv (they could actually be named anything you like) are parameters passed into the function, just like any other... The only thing special about them is their origin. They come from the command line you typed to start the program.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    C:\> ren foo.c foo.bak
    i don't know any thing about this code and how to write it
    i saw it alot but i don't understand it
    i mean , does the user type this code or the programmer
    would you please explain or give me a link explaining this code

    The only thing special about them is their origin. They come from the command line you typed to start the program.
    i don't get what command line i type to start the program
    i just type the source code then run the program
    would you please make it clearer


    argc :an integer containing a count of the number of words the user typed
    argv :an array of pointers to strings, these strings are the actual words the user typed or
    an exact copy of them.
    how can i use the count of the number of words the user typed?? and the words themselves ??
    i'm confused ..

    thanks

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... before you're ever going to be a programmer, you need to know how to operate the machines you're using...

    At your present level you are writing comand line programs. ... The Windows Command Line Interpreter, Batch Files, and Scripting- Using the Command Shell (DOS Prompt) (Click the cute red part!) ... That is programs that run in a text mode window on your desktop. To open a command shell in windows type WIN+R in the run dialog type CMD and click Ok... this is the environment you need to learn how to work with.

    A command line is a string of characters that does two things... it starts the program and it feeds preliminary data (options, paths, filenames, etc) into the probram which are then accessible as argc and argv ...

    When you compile your programs, you are creating an executable... this is a real program that can be run from the command shell, without your programming IDE even loaded into the system... Needless to say, you need to understand how this works.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    i'm working on ubuntu 11.04 so the command shell is the terminal i guess right?
    the link you suggest is for windows is there any for linux , or are they the same ?

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Salahuddin View Post
    i'm working on ubuntu 11.04 so the command shell is the terminal i guess right?
    the link you suggest is for windows is there any for linux , or are they the same ?
    The "Command Shell" is a terminal emulator which runs a shell program, in your case(by default), bash.

    Yes, it works similarly here.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27
    The argc and argv (or whatever name we can them) are very useful when we want to use the command line parameters.
    I will give an example to explain this. (Daily life example)

    You must have used the copy command in windows. Here the syntax is copy <source-file> <dest>
    Suppose you want to make your own C program for copying one file to another, then either you can take these inputs using conventional scanf() function
    or you can command line arguments to take your inputs from the user.

    Whatever the user will type during the program run in command line, they will availbale to you in an array (which you can access with the help
    of argv pointer: argv is a pointer to an array of pointers, where each element of the array points to the string typed by the user).

    I hope you got basic idea about using argc and argv.
    Hope this helps

    ///Regards
    Sumit

    Quote Originally Posted by Salahuddin View Post
    what is the different in use between
    Code:
    main(void)
    and
    Code:
    int main(int argc, char* argv[])
    i knew that
    argc :an integer containing a count of the number of words the user typed
    argv :an array of pointers to strings, these strings are the actual words the user typed or
    an exact copy of them.

    but i don't understand what is the importance of argc and argv and how to use them in my program . ?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Perhaps an example will help...


    Compile the following code as... myprog
    Code:
    int main(int argc, char *argv[]);
      { int i;
         printf("arg c = %d\n",argc);
         for i = 0; i < argc; i++)
            printf(" argv[%d] = %s\n",i, argv[i]);
    
        return 0;
    }
    Run it from the command line... not your ide... Type.... myprog oldfile.txt newfile.txt -c <Enter>
    You should see...
    argc = 4
    argv[0] = myprog
    argv[1] = oldfile.txt
    argv[2] = newfile.txt
    argv[3] = -c

    One of the best ways to understand things is to try them out!

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    when i open terminal in linux ->>> Salahuddin@ubuntu:~$ <<<<<<<<<<-
    is written .. so i have to write --->>>>>>myprog oldfile.txt newfile.txt -c <<<<<<<- after it?
    i have tried so but the next line appears saying --->>>>myprog:command not found<<<<-

    by the way ,when i finish typing the code on eclipse i just press Ctrl+F11 and the program is run below the same eclipse window in a tab called console
    showing me .. ->>>>>>>>>
    arg c = 1
    argv[0] = /home/salahuddin/SAMP/Getchar() and Putchar()/Debug/Getchar() and Putchar()
    <<<<<<
    and i can't write anything else in this sub window
    so i'm confused .. where to write the code?
    please help ..

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to learn at least the basics of navigating around your shell. To execute something in the current directory, you specify that, like so:

    ./myprog foo bar baz


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

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    The problem is that i don't deal with the shell-of ubuntu 11.04- at all , the eclipse environment-which i'm working on- has a Console tab already , so i don't type any code to run the program i just press Ctrl+F11.
    if you have a tutorial for dealing with shell ,i'll be grateful.
    thanks

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salahuddin View Post
    The problem is that i don't deal with the shell-of ubuntu 11.04- at all , the eclipse environment-which i'm working on- has a Console tab already , so i don't type any code to run the program i just press Ctrl+F11.
    if you have a tutorial for dealing with shell ,i'll be grateful.
    thanks
    Something to think about... no need to answer...

    How do you expect to program a computer you don't even know how to operate?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    How do you expect to program a computer you don't even know how to operate?
    And why do you care about command line arguments when you are never at a command prompt?


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argc, **argv in C++.
    By apacz in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2007, 12:09 AM
  2. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  3. argc--; argv++;
    By C of Green in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2003, 06:16 PM
  4. Argv And Argc
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-11-2001, 03:43 PM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM