Display Your Name
Beginner-friendly C++ program that shows how to store your name in a string variable and print it on the screen.
C++ Display Your Name Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "John Doe";
cout << "My name is: " << name << endl;
return 0;
}My name is: John Doe
Understanding Display Your Name
This program teaches you how to store text in a variable and display it on the screen. It is simple and perfect for beginners who are learning about strings, variables, and cout.
---
1. Header Files
There are two header files in this program:
This header allows us to use cout for output.
This header allows us to use the string data type in C++.
Without this, the program would not understand what a string is.
Header files bring ready-made tools into our program so that we can use them.
---
2. using namespace std;
It makes the code easier and shorter for beginners.
---
cout, string3. main() Function
Every C++ program starts running from the main() function.
The line:
marks the beginning of the program.
Everything inside the curly braces { } is what the computer will run.
---
int main() {4. Declaring a String Variable
Inside main, we have:
string name = "John Doe";
Here's what this means:
You can think of a variable like a small box that holds information.
So this line is simply saying:
"Create a box named name and put the text 'John Doe' inside it."
You can change "John Doe" to your own name.
---
5. Displaying the Name Using cout
The next line is:
This line prints the message on the screen.
Let's break it down:
So the full meaning is:
If name contains "John Doe", the output will be:
---
cout << "My name is: " << name << endl;6. return 0;
This tells the computer that the program ran successfully and is now ending.
---
Summary
This example helps you understand how text works in C++ and prepares you for inputs, strings, and more complex programs later on.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Display Your Name
This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.