Thread: What does this code segment do?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    15

    What does this code segment do?

    "You need to provide a makefile so that I can compile and run your program using the following commands:

    make
    ./crossReference.x theNameOfAnInputFile"


    So what does this code fragment actually do? It check what arguments?

    Code:
    int main (int argc, char * argv[])
    {
    if (argc !=2)
    {cout << "NOT SUFFICENT ARGUMENTS \n";
      exit(1);
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    When you call a program from the command-line, you can often specify options after writing the program's name, and the program can then use this information to act accordingly. argc is the number of arguments supplied, and argv is an array of char pointers to the arguments themselves. The code above checks the value of argc, and if it isn't equal to two, tells you there weren't sufficient arguments. Even better than this, is if it would check if there were less than two, or more than two.

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    That program checks the argument counts. The arguments its looking at are from when you either 1) run the file from command prompt and give it arguments, ie. if you've ever read, "go to command prompt and type c:\blah\blah\filename.exe -i -s -m", or in the case of Diablo II you would make a shortcut and go to properties and change the path to "C:\Program Files\Diablo II\" -fullscreen -32bit. What its doing is looking for commands you might have given the .exe file itself in either command prompt or file properties, and those commands are processed in your program if you want.. using argv, you can say if argv[1] == "fullscreen" .. do this, etc. Another explanation by Major_small because its recent, or you could search google, http://cboard.cprogramming.com/showthread.php?t=69588

    First argument is the filename and sometimes path, that ones looking for 2, so it must be expecting argv[1] to contain a command, so you'd have to run it in command prompt and give it a command, Command Prompt-> C:\filename.exe commandhere.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #define vs const
    By @nthony in forum C Programming
    Replies: 12
    Last Post: 11-29-2006, 08:08 PM
  2. Trying to make this code faster & Cramer
    By just2peachy in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2004, 10:54 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM