Display Your Name

Beginner-friendly C++ program that shows how to store your name in a string variable and print it on the screen.

BeginnerTopic: Basic Programs
Back

C++ Display Your Name Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
                  #include <string>
                  using namespace std;
                  
                  int main() {
                      string name = "John Doe";
                      cout << "My name is: " << name << endl;
                      return 0;
                  }
Output
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:

1.#include <iostream>

This header allows us to use cout for output.

2.#include <string>

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;

This line allows us to use names like cout and string without writing std: : before them.
Without it, we would have to write:
std: :cout, std::string
With it, we can simply write:

It makes the code easier and shorter for beginners.

---

                    cout, string

3. 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:

string is a data type that stores text (words, sentences, names, etc.).
name is the variable where the text will be stored.
"John Doe" is the actual text we are saving inside the variable.

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:

cout: Used to show output on the screen.
<< : This operator sends the text to cout.
"My name is: ": This is plain text inside quotes.
name: The variable that stores the name.
endl: Moves the cursor to the next line after printing.

So the full meaning is:

"Print 'My name is: ' and then print whatever is inside the variable name."

If name contains "John Doe", the output will be:

My name is: John Doe

---

                  cout << "My name is: " << name << endl;

6. return 0;

This tells the computer that the program ran successfully and is now ending.

---

Summary

The string data type is used for storing text.
Variables help you store and reuse information.
cout prints text and variables on the screen.
Header files add important tools to your program.
This program displays your name using a string variable.

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.

Table of Contents