c++ int argc code example

Example 1: c++ argv

// command line arguments in c++ are stored in an array of c-strings
// # of arguments: argc
// actual arguments: argv

#include <iostream>
using namespace std;

int main(int argc, char** argv){
  for(int i = 0; i < argc; i++){
    cout << "argv" << i << ": " << argv[i] << endl;
  }
  return 0;
}

/*

./main.exe hello world

argv0: main.exe
argv1: hello
argv2: world

*/

Example 2: int main(int argc char *argv ) in C

int  // Specifies that type of variable the function returns.
     // main() must return an integer
main ( int argc, char **argv ) {
     // code
     return 0; // Indicates that everything went well.
}

Tags:

Cpp Example