Thread: command line arguments

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    command line arguments

    Are command line arguments part of the language or are they compiler specific? Or from a slightly different angle, for someone to be "competent" in C++ should they be comfortable with command line syntax?
    You're only born perfect.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    do you mean the two parameters to main(), or what you type on the command line after the name of the program you want to run. The parameters to main() are part of the c and c++ language standards -- you can call them anything you want, but the parameter types are always the same.
    Code:
    int main(int argc, char* argv[])
    or
    int main(int argc, char** argv)
    
    or 
    int main()
    what you type on the command line following the name of the program you want to execute is program-dependent.

    >for someone to be "competent" in C++ should they be comfortable with command line syntax

    Again, depends on what you mean by "command-line syntax". Certainly he will know how to process the arguments to main(). Only the author of the program can tell you what to type on the command-line after the name of the program.
    Code:
    // MS-DOS or MS-Windows example
    c:>MyProgram.exe param1 param2 param3 ... <Enter>
    *nix example
    : ./a.out param1 param2 param3 ... <Enter>
    Last edited by Ancient Dragon; 09-09-2005 at 08:59 AM.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The arguments of main() are used to enumerate and hold the information available in the command line but aren't command line arguments themselves and switches used in the command line are not stored in argv, that I know of anyway.

    I was thinking more along the lines of using something like this:

    C:\>g++ -Wall -pedantic -o prog.exe prog.cpp

    or whatever. As I understand it, this command line has a series of switches and parameters to tell the compiler/linker/whatever which files to load, what protocols to use, etc. I suspect that IDEs have default mechanisms for obtaining the same information that are kept out of my direct view. But, since I've been using whatever IDE I have available, I haven't made it a priority to become comfortable with command line/compiler/linker switches, etc. (To be honest I've never even run a program directly from the command line.)


    // MS-DOS or MS-Windows examplec:>MyProgram.exe param1 param2 param3 ... <Enter>*nix example: ./a.out param1 param2 param3 ... <Enter>To me, these examples would indicate that at least command line syntax for switches/flags may be compiler dependent rather than part of the C++ language itself, though that may or may not be correct. Don't get me wrong, I know you need precompilers and compilers and linkers to change the code I type into a word processor into code the computer can actually use. My question is, would you consider someone "competent" in C++ if they didn't feel comfortable using command line switches etc., but let the IDE do it for them. And yes I know, "competent" is about as definable as beauty or intelligence or big, but I think you can get the gist of the inquiry even if I can't define "competence" in twenty words or less.
    You're only born perfect.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Everything you type on the command line before pressing <Enter> key is passed to the application program in argc and argv -- that includes all switches. white-space (normally space and tabs) separate one argument from another, so argc is the number of "words" you type and argv is an array of those strings. In your example of g++, the g++ program main will get these:
    g++ -Wall -pedantic -o prog.exe prog.cpp
    argc = 6
    argv[0] = "g++"
    argv[1] = "-Wall"
    argv[2] = "-pedantic"
    argv[3] = "-o"
    argv[4] = "prog.exe"
    argv[5] = "prog.cpp"

    It is the program's respoinsibility to intrepret the command-line arguments. A "competent c++ programmer" will know how to do that. Anyone who doesn't is not yet at that level of knowledge about the c++ language.

    [edit]After re-reading your last post I think I misunderstood your question about competent c++ programmer. You mean "if you don't know how to run the compiler from the command-line instead of an IDE" ? Depends on your level of experience. You may be good solid knowledge about c++ language without knowing much about the compiler details. someone who has been working on-the-job for serveral years should be familiar with command-line switches and know where to find out more information about them. Nobody expects anyone to memorize all the hundreds of available switches -- that's one reason why compiler vendors write compiler documentation. A competent c++ programmer will know where to find information about the compiler (s)he is using and probably know at least the basics of writing makefiles.[/edit]
    Last edited by Ancient Dragon; 09-09-2005 at 12:10 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Fair enough. I appreciate your reply.

    To answer my other question--Do you know if command line switches are standard across compilers/linkers/whatever or are they compiler/linker/whatever specific?
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Yes I do know the answer -- and you won't like it. Each compiler has its own set of options and switches. That means when you switch compilers, even if they are for the same platform, you have to forget everything you knew and start all over again.

    This becomes a huge task for people who write cross compiler and cross platform programs. Have to write a different makefile for every compiler that you want to support, and there are a lot of them.
    Last edited by Ancient Dragon; 09-09-2005 at 01:15 PM.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Like it, no, but I can live with.

    And makefiles are something else to learn about someday if I want to know more about what goes on behind the scenes before/after I hit the compile button on the IDE I use.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM