This article was co-authored by wikiHow staff writer, Travis Boylls. Travis Boylls is a Technology Writer and Editor for wikiHow. Travis has experience writing technology-related articles, providing software customer service, and in graphic design. He specializes in Windows, macOS, Android, iOS, and Linux platforms. He studied graphic design at Pikes Peak Community College.
This article has been viewed 4,111 times.
Learn more...
Do you want to learn how to print in C and C++? In coding, the term "print" means to display a stored string or variable on a computer's default output device (usually a monitor). A string is a stored sequence of characters that usually represent a word or a phrase. This wikiHow article teaches you how to print in C++.
Things You Should Know
- You can print using methods from C++ as well as C.
- The "cout" object is the preferred method of printing in C++.
- You can also use the "printf" object from C.
Steps
Using the "Cout" Object
-
1Open a C++ file in your integrated development environment. Most programming is done in an integrated development environment (IDE). There are many IDEs to chose from, including; Netbeans, Microsoft Visual Studio, Eclipse, and Xcode for developing Mac and iOS applications.create a new program Open your preferred IDE. Then open an existing C++ file or .
-
2Include the input/output stream class. The "cout" object used to output a string is part of the output class stream. Since your program will also likely be using inputs as well as outputs, go ahead and include the input/output stream. Add the following line at the top of your program to do so:[1]
- #include <iostream>
Advertisement -
3Use the standard (std) namespace directive (optional). In C++, there are a variety of different namespaces that contain different functions, classes, objects, and variables. The "cout" object is part of the standard (or "std") namespace. You will need to declare this namespace to use the "cout" object. If you want, you can use the standard (std) directive to declare this namespace for your entire program. This means that your compiler will use the std namespace by default when no other namespace is specified. To add the standard namespace directive, add the following line below the line that includes your input/output stream: [2]
- using namespace std;
- Alternatively, you can declare the standard namespace just for the "cout" object. To do so, type using std::cout; instead. This will declare the "std" namespace whenever the "cout" object is used by default.
-
4Initialize the function that will be used to print. To do so, type int <function name>(). Replace "<function name>" with the name of the function. Then add an opening bracket ("{") on the next line. All code for this function will be added after the opening bracket. If this is a new program, you will need to initialize the main function, which is the starting function for your entire program. Add the following line of code to initialize the main function:
- int main(). Then add the opening { on the next line.
-
5Type cout on the line you want to print. The "cout" object is the preferred way to print in C++. Enter this on the line you want to print. [3]
- If you did not declare the "std" namespace at the beginning of your program, you can declare it on each line you use the "cout" object. To do so, type std::cout each time you use the "cout" object.
-
6Type << followed what you want to print. Enter "<<" before each item you want to print on the same line as the "cout" object. You can print a string (i.e, cout << "Hello World!"), a variable, or both (i.e, cout << "X is equal to " << x).
- When printing a string, put the text of your string in quotation marks. When printing a variable, just enter the variable name without quotation marks after "<<"..
- If you want to add a line break after your printed text, add the modifier << endl after text and variables you want to print. The next printed object will be printed on a new line.
-
7Add a semi-colon (;) at the end of the line. This is how you end each line of code in a function.
-
8Add a closing bracket (}) at the end of your function. Your function begins with an opening bracket. When you have finished the last line of code in your function, enter the closing bracket of your function (}) below the last line.
- When you reach the last line of code in your main function, enter return 0; as the last line of code. This signifies the program has been completed successfully. Then enter the closing bracket after this line.
- Your program should look something like the following:
#include <iostream> using namespace std; int main() { int x = 25; //This specifies that the integer "x" is equal to 25. cout << "X is equal to " << x; //This should print "X is equal to 25." return 0; }
Using the "Printf" Object
-
1Open a C++ file in your integrated development environment. Most programming is done in an integrated development environment (IDE). There are many IDEs to chose from, including; Netbeans, Microsoft Visual Studio, Eclipse, and Xcode for developing Mac and iOS applications. Open your preferred IDE. Then open an existing C++ file or create a new program.
-
2Include the standard input/output header. The "printf" object is typically used in C to write to standard output. While it is typically used in C, it can also be used in C++. To do so, you need to include the standard input/output header. Enter the following line at the top of your program to do so:
- #include <stdio.h>
-
3Initialize the function that will be used to print. To do so, type int <function name>(). Replace "<function name>" with the name of the function. Then add an opening bracket ("{") on the next line. All code for this function will be added after the opening bracket. If this is a new program, you will need to initialize the main function, which is the starting function for your entire program. Use the following steps to initialize the main function:
- int main(). Then add the opening { on the next line.
- You can use the "printf" object as its own function. To do so, type int printf();
-
4Type printf(); on the line you want to print. This object stands for "print formatted." It should have a parenthesis at the end of it. The characters, strings, integers, and specifiers you want to print will be placed inside the parenthesis.
-
5Enter what you want to print in the proper format in the parenthesis. The proper format is "constant string with any format specifiers", variable name. Enter any words or phrases you want to print first in quotation marks. This method also requires that you include a specifier. These specify the format in which any variables you want to print will be printed. Then enter a comma (",") after the quotation marks and then enter the variable name. The format specifiers you can use are as follows:[4]
- %c — Character
- %s — String
- %p — Pointer address
- %i or %d — Signed integer
- %u — Unsigned integer
- %o — Unsigned octal
- %x or %X — Unsigned hexadecimal (lower/upper case)
- %e or %E — Floating point
- %g or %G — Shortest representation
- %a or %A — Hexidecimal Floating Point <li<\n — Creates a line break after the printed text
-
6Add a closing bracket (}) at the end of your function. Your function begins with an opening bracket. When you have finished the last line of code in your function, enter the closing bracket of your function (kbd|}) below the last line.
- When you reach the last line of code in your main function, enter return 0; as the last line of code. This signifies the program has been completed successfully. Then enter the closing bracket after this line.
- Your program should look something like the following:
#include <stdio.h> int main() { int x = 15; // This specifies that the integer "x" is equal to 15. printf("X is equal to %d\n", x); // This should print "X is equal to 15." The "%d" specifies the variable is a signed integer, and the "\n" adds a line break after the printed text. return 0; }