Selected Reading

C++ print println() Function



The std::println() is used to print various data types to the standard output. This function automatically appends a newline character at the end, making the output more readable and helps in debugging.

It can accept multiple arguments of different types such as integers, floating-point numbers, and strings.

Syntax

Following is the syntax for std::println() Function.

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

Parameters

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

Return Value

This function does not return a value. It simply prints the given arguments to the standard output, followed by a newline.

Time Complexity

The time complexity of this function is constant, i.e.,O(1), but the total complexity depends on the number of arguments passed.

Example 1

In this example, std::println() prints the string "Hello, World!" followed by a newline. It is a simple demonstration of how to use the function for basic text output.

#include <print>
int main() {
   std::println("Hello, World!");
   return 0;
}

Output

Output of the above code is as follows

Hello, World!

Example 2

In this example, multiple arguments of various types are passed to std::println(). It effectively prints the text and variables in sequence.

#include <print>
int main() {
   int num = 42;
   double pi = 3.14159;
   std::println("Number:", num, "Pi:", pi);
   return 0;
}

Output

If we run the above code it will generate the following output

Number: 42 Pi: 3.14159

Example 3

This example demonstrates how to directly print results of expressions and calculations using the std::println() function.

#include <print>
int main() {
   int a = 5;
   int b = 10;
   std::println("Sum:", a + b, "Difference:", b - a, "Product:", a * b);
   return 0;
}

Output

Following is the output of the above code

Sum: 15 Difference: 5 Product: 50
cpp_print.htm
Advertisements