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:
- print()
- 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.
Similar Reads
< iomanip > Header in C++ In C++, the <iomanip> header file defines the manipulator functions that are used to manipulate the format of the input and output of our program. It is a part of the input/output library in C++. In this article, we will discuss the functions and facilities provided in the <iomanip> head
9 min read
C++17 - <charconv> Header The C++ <charconv> header provides many functions for converting the character sequences to numerical values and vice-versa. It is considered better than the <cstdlib> header file functions for the same purpose. The functions provided by the <charconv> header file are generally fas
5 min read
Header Files in C In C programming, a header file is a file that ends with the .h extension and contains features like functions, data types, macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor.C language uses header files to provide the standard
5 min read
C++ 11 - <cinttypes> Header The header offers type aliases and functions for manipulating integer types with particular sizes and signedness, as a part of the C++11 standard. Its aim is to present a standard group of integer types across diverse platforms and architectures. <cinttypes> header The <cinttypes> header
2 min read
Header Files in C++ C++ offers its users a variety of functions, one of which is included in header files. In C++, all the header files may or may not end with the ".h" extension unlike in C, Where all the header files must necessarily end with the ".h" extension. Header files in C++ are basically used to declare an in
6 min read