PDF download Download Article PDF download Download Article

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.
Method 1
Method 1 of 2:

Using the "Cout" Object

PDF download Download Article
  1. 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.
  2. 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
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. This is how you end each line of code in a function.
  8. 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 
       ; 
       } 
      
  9. Advertisement


Method 2
Method 2 of 2:

Using the "Printf" Object

PDF download Download Article
  1. 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.
  2. 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>
  3. 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();
  4. 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.
  5. 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
  6. 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 
       ; 
       } 
      
  7. Advertisement


Expert Q&A

Ask a Question
      Advertisement

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about programming languages, check out our in-depth interview with Tyrone Showers .

      About This Article

      Article Summary X

      1. Type std::cout on the line you want to print.
      2. Type

      Did this summary help you?
      Thanks to all authors for creating a page that has been read 24,416 times.

      Is this article up to date?

      Advertisement