Hello, C++!
main.cpp
123456#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
The example above is a classic starting point for every C++ programmer. Each line in this simple program has a specific role:
- The first line,
#include <iostream>, tells the compiler to include the input/output stream library. This library gives you access to objects likestd::cout, which is used for printing text to the screen; - The next part,
int main() { ... }, defines the main function. Every C++ program must have amainfunction, as this is where the program begins running; - Inside the main function, the line
std::cout << "Hello, World!" << std::endl;sends the text"Hello, World!"to the output stream, which displays it on your screen. The<<operator is used to pass the string tostd::cout, andstd::endladds a new line after the text; - The final line,
return 0;, ends the main function and returns the value0to the operating system. This is a way of signaling that the program finished successfully.
Even though this program is short, it introduces you to the basic building blocks of every C++ application: including libraries, defining the main function, displaying output, and signaling successful completion.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Can you explain what happens if I remove the `return 0;` line?
What does `std::endl` do compared to using `\n`?
Are there other ways to print output in C++?
Fantastiskt!
Completion betyg förbättrat till 6.67
Hello, C++!
Svep för att visa menyn
main.cpp
123456#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
The example above is a classic starting point for every C++ programmer. Each line in this simple program has a specific role:
- The first line,
#include <iostream>, tells the compiler to include the input/output stream library. This library gives you access to objects likestd::cout, which is used for printing text to the screen; - The next part,
int main() { ... }, defines the main function. Every C++ program must have amainfunction, as this is where the program begins running; - Inside the main function, the line
std::cout << "Hello, World!" << std::endl;sends the text"Hello, World!"to the output stream, which displays it on your screen. The<<operator is used to pass the string tostd::cout, andstd::endladds a new line after the text; - The final line,
return 0;, ends the main function and returns the value0to the operating system. This is a way of signaling that the program finished successfully.
Even though this program is short, it introduces you to the basic building blocks of every C++ application: including libraries, defining the main function, displaying output, and signaling successful completion.
Tack för dina kommentarer!