c++ command line args
 

.. Command Line Args: argc, argv [ ]

Can provide arguments to the program on the command line: $ prog1 4 => 2 args (argc is 2) argv[0] => "prog1" is the name of the program argv[1] => "4" is an integer that the program can use
First line of code in program becomes: int main ( int argc, char *argv[] ) - argc is an abbreviation for "argument count" argc simply the count of args INCLUDES name of program - argv is an abbreviation for "argument vector" argv name for pointer to an array of string pointers passed to main function the second argument passed to main argv[0] always points to the name of the command itself.
EXAMPLE: ======== int main ( int argc, char *argv[] ) { int n; // number of reps // check # of args if ( argc != 2 ) { // ---- error msg... return error... } // get int argument n = atoi( argv[1] ); // first arg (program name is argv[0]) ...

Example: ======= $ mine -c 10 2.0 int main ( int argc, char *argv[] )

--------------------------------- argc is 4 (4 arguments) argv[] is an array of pointers there are 4 pointers in the array [0-3] and a null terminator [4] each pointer points to what?? and each argument is actually just an array of what? and each of those arrays is terminated by what?
EXAMPLE: error detection and report ======== forkProg 7 // check for correct # args if( argc != 2 || atoi( argv[1]) > 5 ) { cout << "Usage: " << argv[0] << " numChildren (5 or less)" << endl; return 1; } else numChild = atoi( argv[1] ); // args ok
EXAMPLE: error detection and report (better) ======== generateRandom 2000 100 --> argc = 3 --> argv[0] = "generateRandom" --> argv[1] = "2000" --> argv[2] = "100" //check # of command-line args which should be 3: program, num, range if( argc != 3 ) // ERROR { // in C fprintf( stderr, "Usage: %s number of nums, max size(-1)\n", argv[0] ); // in C++ cout << "\nUsage: " << argv[0] << " number of nums, max size(-1)" << endl; return 1; // <-- we're done... } // get the args limit = atoi( argv[1] ); // get the number of nums from user range = atoi( argv[2] ); // max size -1
EXAMPLE hello n ======= // prog1.cpp // repeats "Hello World!" n times - n specified on the command line #include <iostream> using namespace std; int main ( int argc, char *argv[] ) { int n; // check for valid number of command-line arguments if( argc != 2 ) // must be 2 args INCLUDING program name { // error message tells HOW to run this program cout << "Usage: " << argv[0] << " Number of reps (integer)" << endl; return 1; // error back to OS } // get the supplied argument n = atoi( argv[1] ); // loop and greet n times for( int i = 1; i <= n; i++ ) cout << i << ": Hello World!" << endl; // back to OS - ok return 0; }

.. Example Code: Repeat supplied message n times

EXAMPLE ======= // prog2.cpp // repeats supplied message n times // n specified on the command line // message supplied on command line #include <iostream> using namespace std; int main( int argc, char *argv[] ) { int n; string s; // check for valid number of command-line arguments if( argc != 3 ) // must be 3 args INCLUDING program name { // error message tells HOW to run this program cout << "Usage: " << argv[0] << " Repetitions" << " Message" << endl; return 1; // error back to OS } // get the supplied argument n = atoi( argv[1] ); s = argv[2]; // could just use argv[2] // loop and greet n times for( int i = 1; i <= n; i++ ) cout << i << ": " << s << endl; // back to OS - ok return 0; } EXAMPLE RUNS: ============= /home/wyatt/private/Code$ prog2 5 Usage: prog2 Repetitions Message /home/wyatt/private/Code$ prog2 5 yippeee! x Usage: prog2 Repetitions Message /home/wyatt/private/Code$ prog2 5 yippeee! 1: yippeee! 2: yippeee! 3: yippeee! 4: yippeee! 5: yippeee!

.. Architecture vs OS

If Windows and Linux are both running on the SAME Intel architecture, why can't programs run on each other's machine? 1. Will the machine code of the data be the same? 2. Will the machine code of the instructions be the same? 3. Will the calls to the OS be the same? Interrupts? (21h vs 80h) Arguments? 4. Supporting libraries the same? APIs? 5. Executable file structure the same? PE COFF / ELF?