What is command line arguments? how should i declare it? what is its use?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aimthiaz
    New Member
    • Jul 2010
    • 5

    What is command line arguments? how should i declare it? what is its use?

    command line arguments
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Command line arguments are used when you run your program from the OS command prompt:

    c:\>myprog.exe filename.txt

    Here a program named myprog has an argument that is a file name. Maybe the program will read or write the file. Without this, the program would need you to enter the file name as part of the program.

    Basically, any data the program needs in order to run can be passed as a command line argument. These arguments are passed to main:

    Code:
    int main(int argc, char* argv[])
    {
    
    
    }
    argc is the number of arguments. In our example, there are 2 arguments:mypro g.exe and filename.txt. You use argc to tell you which arguments you are using:

    argv[0] is myprog.exe
    argv[1] is filename.txt

    In the program if you fopen argv[1] you will be opening filename.txt.

    Comment

    • aimthiaz
      New Member
      • Jul 2010
      • 5

      #3
      Originally posted by weaknessforcats
      Command line arguments are used when you run your program from the OS command prompt:

      c:\>myprog.exe filename.txt

      Here a program named myprog has an argument that is a file name. Maybe the program will read or write the file. Without this, the program would need you to enter the file name as part of the program.

      Basically, any data the program needs in order to run can be passed as a command line argument. These arguments are passed to main:

      Code:
      int main(int argc, char* argv[])
      {
      
      
      }
      argc is the number of arguments. In our example, there are 2 arguments:mypro g.exe and filename.txt. You use argc to tell you which arguments you are using:

      argv[0] is myprog.exe
      argv[1] is filename.txt

      In the program if you fopen argv[1] you will be opening filename.txt.
      only the integer and a character pointer can be passed through the main function?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Yes, all data supplied by the user from the command line or console is always in text form. You can pass digits but you will need to convert them to binary from text in order to use them.

        Comment

        Working...