Command line argument

This is a discussion on Command line argument within the C++ Programming forums, part of the General Programming Boards category; In most of c++ books I have found arg as Code: int main(int argc,char * argv[]) Is it necessary that ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Command line argument

    In most of c++ books I have found arg as
    Code:
    int main(int argc,char * argv[])
    Is it necessary that int variable name must be argc only and char* name must be argv only. Though on Turbo c++(3) I am able to use any name but I suspect that it should only be argc and argv.

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    I don't think so.

    :: Edit
    I did a test, and you can name them whatever you like. I guess it's just convention.
    Last edited by SirCrono6; 12-27-2005 at 11:17 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Call them whatever you want, but you're sure to attract comments if you defy this particular convention.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    one more ques.
    What will be output and why?
    Code:
    //myprog.exe
    int main(int argc,char* argv[],char* env[])
    {
      for(int i=1;i<argc;++i)
        cout<<env[i];
    }
    return(0);
    }
    if command line arg is
    [code]
    myprog.exe one two three

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    It wont compile in the first place due to a syntax error. Other than that it probably depends on your system, since the C++ standard doesnt specify what the additional arguments to main() should be (besides argc and argv).
    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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Assuming your run-time passes a third argument to main() called envp, you should note that argc doesn't count how many environment variables you have.

    for ( i = 0 ; envp[i] != NULL ; i++ )
    Is how you step through it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 09:09 AM
  2. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 12:53 PM
  3. Peculiar malloc / command argument question
    By Beowolf in forum C Programming
    Replies: 4
    Last Post: 09-11-2007, 12:54 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM

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