learn/doc/cpp_introduction at main · gitLibs/learn · GitHub
Skip to content

Latest commit

 

History

History
 
 

Folders and files

README.md

back to contents

C++: introduction

↑ top




Reference

↑ top




Install

Please visit here.

↑ top




Hello World!

#include <stdio.h>
#include <iostream>
using namespace std;

int main(int argc, const char *argv[])
{
	printf("%s %d\n", "Hello World", 10);
    cout << "Hello World!";
    return 0;
}
// Hello World 10
// Hello World!

You can either:

  • cd code/ and g++ -std=c++11 hello.cpp and ./a.out
  • cd code/ and g++ -std=c++11 hello.cpp -o hello and ./hello

int argc, const char *argv[] is used to get arguments from command line. If you do not need to process it, just use int main() or int main(void).

↑ top




argument

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    // Check the number of parameters
    if (argc < 2) {
        cerr << "Usage: " << argv[0] << " NAME" << endl;
        return 1;
    }
    // Print the user's name:
    cout << argv[0] << "says hello, " << argv[1] << "!" << endl;
}

/*
$ ./a.out  Gyu-Ho

Then

./a.outsays hello, Gyu-Ho!
*/

↑ top