📜  hello world C++, C plus plus hello world - C++ (1)

📅  最后修改于: 2023-12-03 15:31:07.148000             🧑  作者: Mango

C++ Hello World

C++ is a programming language that was developed in the late 1970s by Bjarne Stroustrup. It is an extension of the C programming language, and provides object-oriented features to the language.

What is Hello World?

"Hello World" is a simple program that is commonly used as an introduction to programming. It is an easy program to write, and serves as a way for programmers to get familiar with the syntax of the language they are working with.

Writing "Hello World" in C++

To write a "Hello World" program in C++, you will need a text editor and a C++ compiler. Here is an example of a "Hello World" program in C++:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}
Code Explanation
  • The first line #include <iostream> is a preprocessor directive that tells the compiler to include the standard input-output library in the program.
  • using namespace std tells the compiler to use the standard namespace for input and output operations.
  • int main() is the entry-point function of every C++ program. It returns an integer value (in this case 0) to the operating system to indicate if the program ran successfully or not.
  • cout is an output stream object that allows us to print output to the console.
  • "Hello World!" is the message we want to print to the console.
  • << endl is an endline character that tells the console to move to a new line.
Compiling and Running the Program

To compile and run the program on your computer:

  1. Save the above code in a file called helloworld.cpp.
  2. Open a terminal or command prompt and navigate to the directory containing the file using the cd command.
  3. Type the command g++ helloworld.cpp -o helloworld to compile the program. This will generate an executable file called helloworld.
  4. Type the command ./helloworld to run the program.

You should see the message "Hello World!" printed to the console.

Conclusion

Congratulations! You have written and executed your first C++ program. This is just the beginning of your journey learning C++. There are many more concepts and features of the language to explore.