Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Hello, C++! | Getting Started with C++
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ FOundations: V1

bookHello, C++!

main.cpp

main.cpp

copy
1234567
#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }

To understand how this simple C++ program works, start by looking at the first line: #include <iostream>. This line tells the compiler to include the standard input-output stream library, which provides the functionality for displaying output on the screen using std::cout.

Next, the line int main() { begins the definition of the main function. Every C++ program must have a main function, which is where the program starts executing. The int before main means that the function returns an integer value to the operating system when the program finishes.

Inside the curly braces { }, you find the statement std::cout << "Hello, World!" << std::endl;. Here, std::cout is the standard character output stream in C++. The << operator is used to send the string "Hello, World!" to the output stream. Adding << std::endl inserts a newline character so that any following output appears on the next line. This line is what actually prints Hello, World! to the console.

Finally, return 0; signals that the program finished successfully. The closing brace } marks the end of the main function. With these elements, you have a complete, functional C++ program that prints a message to the screen.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain what would happen if I removed `#include <iostream>`?

What does `return 0;` actually do in the program?

Can you show me how to modify this program to print a different message?

bookHello, C++!

Svep för att visa menyn

main.cpp

main.cpp

copy
1234567
#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }

To understand how this simple C++ program works, start by looking at the first line: #include <iostream>. This line tells the compiler to include the standard input-output stream library, which provides the functionality for displaying output on the screen using std::cout.

Next, the line int main() { begins the definition of the main function. Every C++ program must have a main function, which is where the program starts executing. The int before main means that the function returns an integer value to the operating system when the program finishes.

Inside the curly braces { }, you find the statement std::cout << "Hello, World!" << std::endl;. Here, std::cout is the standard character output stream in C++. The << operator is used to send the string "Hello, World!" to the output stream. Adding << std::endl inserts a newline character so that any following output appears on the next line. This line is what actually prints Hello, World! to the console.

Finally, return 0; signals that the program finished successfully. The closing brace } marks the end of the main function. With these elements, you have a complete, functional C++ program that prints a message to the screen.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1
some-alt