C++ Program to Print Your Own Name
Last Updated :
11 Oct, 2024
Printing your own name means displaying your name on the computer screen. In this article, we will learn how to print your own name using a C++ program.
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++. You can provide your name to cout object using << operator and it will print it on the output screen.
Syntax
cout <<"your_name";
Example
C++
// C++ program to demonstrate how to print your
// own name using cout object
#include <bits/stdc++.h>
using namespace std;
int main() {
// Printing the name using cout object
cout << "Anmol";
return 0;
}
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");
Example
C++
// C++ program to demonstrate how to print your
// name using printf() method
#include <bits/stdc++.h>
using namespace std;
int main() {
// Printing the name using printf() method
printf("Anmol");
return 0;
}
Using puts() Function
The puts() function is another function that is used to print the given string to the output screen.
Syntax
puts("your_name")
Example
C++
// C++ Program to demonstrate how to print your
// name using puts() method
#include <bits/stdc++.h>
using namespace std;
int main() {
// Printing the name using puts function
puts("Anmol");
return 0;
}
Using wcout
wcout is used for printing the wide characters and wide strings object as output on the screen. We can also use this function to print our name.
Syntax
wcout << L"your_name";
Example
C++
// C++ program for printing the wide characters and
// string using wcout object
#include <bits/stdc++.h>
using namespace std;
int main() {
// Printing the string using wcout object
wcout << L"Anmol";
return 0;
}
We can also provide the name as input instead of hardcoding it in the program. cin can be used to take the name as input from the user and store it in a string variable. We can then use cout or any other function to print the name on the screen.
Example
C++
// C++ program demonstrate how to print your
// name by taking it as input
#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
Anmol
Output
Anmol