Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Your First C++ Program: Hello World!
Write your first C++ program that displays "Hello, World!" to the console.
Prerequisites
Why Hello World?
Everything starts somewhere and in programming we start with declaring to the world we are here, hungry to write the next latest and greatest piece of software.
"Hello World" is a tradition in programming that started over 50 years ago. Programmers all around the world start with this same program - now you're part of that awesome tradition!
Meet Your First C++ Program
Here's the code that makes the computer say "Hello World!":
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Don't worry if it looks confusing right now - we'll break it down into simple pieces!
Breaking Down the Magic
Let's understand each part of our program, like taking apart a LEGO creation to see how it works:
The Library Helper
#include <iostream>
This line is like asking for help from a library. The iostream
library knows how to display text on the screen. It's like borrowing a megaphone so everyone can hear you!
The Main Function - Where the Magic Happens!
int main() {
// Your code goes here!
return 0;
}
Every C++ program needs a main()
function. This is where your program starts running - like the "START" button on a video game!
The Speaking Part (output to screen)
std::cout << "Hello World!" << std::endl;
This is the line that actually makes the computer speak! (Here speak refers to output to screen)
std::cout stands for (c)haracter (out) and means "computer output" - it's how we tell the computer to display something
<< are like arrows pointing where the text should go, we are going to take the next part and push it into std::cout
"Hello World!" is the message we want to show (always put text in quotes!)
std::endl means "end line" - it's like pressing Enter to go to the next line
The End
return 0;
This tells the computer "We're done! Everything worked perfectly!" It's like saying "The End" at the end of a story.
Its a good practice to end the program with a 0
. Later we will learn about EXIT_SUCCESS
.
Important Rules to Remember
Semicolons are Super Important ;
- Most lines in C++ end with a semicolon
- It's like putting a period at the end of a sentence
Curly Braces Keep Things Together { }
- They group code together like a box
- Everything inside the
main()
function goes between{
and}
Quotes for Text " "
- When you want to display words, put them in quotes
- The computer knows the difference between code and text this way
Fun Variations to Try!
Once you master "Hello World!", try these fun variations:
Say Your Name
std::cout << "Hello, I'm [Your Name]!" << std::endl;
Multiple Lines
std::cout << "Hello World!" << std::endl;
std::cout << "This is my first program!" << std::endl;
std::cout << "Programming is awesome!" << std::endl;
What Happens When You Run "Hello World"?
When you run your program, you'll see this appear on your screen:
Hello World!
You're Already a Programmer!
Congratulations! By understanding this program, you've taken your first step into the world of programming. You've learned:
- How to include libraries (
#include
) - How to create the main function (
int main()
) - How to display text (
std::cout
) - How to end lines (
std::endl
) - How to finish your program (
return 0
)
Ready for Your Challenge?
Now it's time to write your very own "Hello World" program. Remember:
- Start with the library:
#include <iostream>
- Create your main function:
int main() { }
- Add your message with
std::cout
- Don't forget the semicolons!
- End with
return 0;
Fun Facts
- The first "Hello World" program was written in 1972
- C++ was created in 1985 by Bjarne Stroustrup
- There are over 4 million C++ programmers worldwide
- Many video games, operating systems, and apps are built with C++!
Now go ahead and create your masterpiece! Remember, every expert was once a beginner, and you're doing great!
Your First C++ Program: Hello World! - Quiz
Test your understanding of the lesson.
Practice Exercises
Hello World Program
Write a simple C++ program that outputs "Hello, World!" to the console.
Lesson Discussion
Share your thoughts and questions