Printing your own name means displaying a text string containing your name on the output screen.
- Uses the cout object to display text on the screen.
- Demonstrates basic output operations in C++.
Examples
Input: name = "Anmol"
Output: Anmol
Explanation: Given name is printed on the output screen.Input: name = "Alex"
Output: Alex
Explanation: Given name is printed on the output screen.
Print Your Own Name Using cout
The simplest way to print something is to use the cout. It is the standard method to output any data in C++.
Syntax
cout <<"your_name";
#include <bits/stdc++.h>
using namespace std;
int main() {
// Printing the name using cout object
cout << "Anmol";
return 0;
}
Output
Anmol
Explanation: The string containing the name is passed to cout using the insertion operator (<<), which displays it on the output screen.
Other Ways to Print Your Name
Apart from cout object there are also the various methods by which we can print your own name.
Using printf() Function
C++ supports the printf() function from the C language which can be used to print name on the output screen. It is defined inside the <cstdio> header file.
Syntax
printf("your_name");
#include <bits/stdc++.h>
using namespace std;
int main() {
// Printing the name using printf() method
printf("Anmol");
return 0;
}
Output
Anmol
Explanation: The string passed to printf() is displayed directly on the output screen.
Using puts()
The puts() function prints a string followed by a newline character. It is also defined in the <cstdio> header file.
Syntax
puts("your_name")
#include <bits/stdc++.h>
using namespace std;
int main() {
// Printing the name using puts function
puts("Anmol");
return 0;
}
Output
Anmol
Explanation: The string is printed on the screen, and the cursor automatically moves to the next line.
Using wcout
The wcout object is used to display wide characters and wide strings. It is useful when working with Unicode or multilingual text.
Syntax
wcout << L"your_name";
#include <bits/stdc++.h>
using namespace std;
int main() {
// Printing the string using wcout object
wcout << L"Anmol";
return 0;
}
Output
Anmol
Explanation: The L prefix creates a wide string literal, which is displayed using wcout.
By Taking Name as Input
Instead of hardcoding the name, we can read it from the user and then display it on the screen.
#include <bits/stdc++.h>
using namespace std;
int main() {
// Variable to store the name
string str;
// Taking the name string as input using
// cin object
cin >> str;
// Print the name string using cout object
cout << str;
return 0;
}
Input
AnmolOutput
AnmolExplanation: The name is stored in a string variable using cin and then printed using cout. This allows the program to display any name entered by the user.