Thread: Command line argument

  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
    39,660
    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
    28,413
    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).
    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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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, 08:09 AM
  2. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  3. Peculiar malloc / command argument question
    By Beowolf in forum C Programming
    Replies: 4
    Last Post: 09-10-2007, 11:54 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM