Download ArticleDownload 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 How.com.vn 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

Download Article
  1. How.com.vn English: Step 1 Open 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.
  2. How.com.vn English: Step 2 Include 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
  3. How.com.vn English: Step 3 Use 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.
  4. How.com.vn English: Step 4 Initialize 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.
  5. How.com.vn English: Step 5 Type 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.
  6. How.com.vn English: Step 6 Type << 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.
  7. How.com.vn English: Step 7 Add a semi-colon (;) at the end of the line.
    This is how you end each line of code in a function.
  8. How.com.vn English: Step 8 Add 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;}
  9. Advertisement


Method 2
Method 2 of 2:

Using the "Printf" Object

Download Article
  1. How.com.vn English: Step 1 Open 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.
  2. How.com.vn English: Step 2 Include 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>
  3. How.com.vn English: Step 3 Initialize 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();
  4. How.com.vn English: Step 4 Type 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.
  5. How.com.vn English: Step 5 Enter 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
  6. How.com.vn English: Step 6 Add 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;}
  7. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      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

      How.com.vn English: Tyrone Showers
      Written by:
      Technologist
      This article was written by Tyrone Showers and by How.com.vn staff writer, Travis Boylls. Tyrone Showers is a Technologist and the Co-owner of Taliferro Group, an IT consulting company based in Seattle, Washington. With over 35 years of professional experience, he specializes in API Design, e-Commerce, Operational Efficiency, and website development. He has a B.S. in Computer Science from DeVry Institute of Technology. This article has been viewed 18,920 times.
      How helpful is this?
      Co-authors: 7
      Updated: January 17, 2024
      Views: 18,920
      Article SummaryX

      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 18,920 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement