- Want to learn C++ as a beginner
- Want to code a brief program with C++
Table of contents
Coding as a helloworld.cpp
#include <iostream> int main() { std::cout << "Hello World!"; return 0; }
Let's see more detail one by one.
#include <iostream>
"#include" operator means that importing something libraries.
This time, I import "iostream" library. this library is a standard library in C++.
int main()
This is a main function of profram, also called "Entry Point". This function would be called at first in C++.
Thus you must define the main function.
std::cout << "Hello World!";
"std::cout" is an object for standard output stream.
You can use as it is because C++ has already provided this object.
Once specifying "Hello World" with the shift operator ("<<") to this stream object, it will be output to tha standard output.
return 0;
This is a return value of the main function.
"0" means no error occurred. Anything other than "0" means that something error occurred.
コンパイルして実行
When you develop with IDE such as Visual Studio or XCode, you might just click the "Run" button. IDE would execute compile, link and run a program instead automatically.
When you use Command-Line interface, you need compile your program yourself.
For example, when using "g++" as a C++ compiler, you would compile and run as below:
> g++ helloworld.cpp -o helloworld > ./helloworld Hello World!