How to Start

beginner c++11

Every C++ program starts with the main function. main returns int as a signal to other programs if your program was successful or not.

int main() {
  return 0; // success!
}


A Different Main

The main function in C++ can take different forms. To accept command line arguments you define a main with two parameters: how many arguments there are (“arg count”) and the arguments themselves (“arg vector”). If there are arguments, then the first one represents the program’s name.

#include <iostream>

int main(int argc, char** argv) {
  if (argc > 0) {
    std::cout << "Hello from " << argv[0] << "\n";
  }
  return 0;
}
Hello from ./build/how_to_start2

For more C++ By Example, click here.