Open In App

C++23 <print> Header

Last Updated : 10 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

C++ has long been a powerful language, known for its versatility and performance. With each new standard release, the language evolves, introducing features that enhance developer productivity and code readability. One of these additions of the C++ 23 standard is the introduction of <print> header to enhance the output capability of language.

<print> Header File in C++

The <print> header introduces a set of functions aimed at simplifying and enhancing the way developers handle output formatting in C++. These functions allow the use of the Unicode character set putting it on par with other programming languages which supported these features for long time.

The <print> header file defines the following two functions:

  1. print()
  2. println()

1. print() Function in C++

The print() function is used to print the formatted string to the output stream. This function is similar to the old C printf() function in the way it shows output but its internal implementation is based on the modern C++ components.

Syntax of print()

print (" {formatted_string}{} .... ", arguments...);

where,

  • formatted_string: The string to be printed.
  • arguments: Lists of arguments that are to be printed

How to print arguments in formatted_string?

Printing formatted string is different from the old printf() function. Here, arguments are denoted by curly brackets {} at the place where it we want the argument to be printed.

Example: Printing Hello World

C++




// C++ program to print hello world using print satement
#include <iostream>
#include <print>
using namespace std;
  
int main()
{
  
    // new function
    print("Hello World");
  
    return 0;
}


Output

Hello World

2. println() Function in C++

The println() function is also used to print the formatted string to the standard output stream but this function additionally prints the new line character (‘\n’) at the end of the output.

Syntax

println(" {formatted_string} ", arguments...);

where,

  • formatted_string: The string to be printed.
  • arguments: Lists of arguments that are to be printed

Example of println()

C++




// C++ program to illustrate the use of println()
#include <iostream>
#include <print>
using namespace std;
  
int main()
{
  
    int a = 10 println("The value of arguement: {}", a);
    // the above and below sentence would be seperated by a
    // new line
    println("The quick brown fox jumps over the lazy dog.");
  
    return 0;
}


Output

The value of argument: 10
The quick brown fox jumps over the lazy dog

Conclusion

The <print> header in C++23 brings a modern and convenient approach to output formatting. With its function templates and support for Unicode, developers can expect a more expressive and readable way to handle output, reducing the boilerplate code associated with traditional C++ output methods.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads