printf() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The printf() method is an inbuilt method of the cstdio library that is used to print output in the format of string type to the console using various types of format specifiers.

Syntax of printf() in C++

When we have to print something on the console window of the c++ program, we will use the printf() method to perform this operation. Let's understand the syntax of the printf() method. Syntax:

Parameters of printf() in C++

The printf() method takes two parameters.

  • format specifiers: The format specifiers specify the type of data to be printed on the standard output.
  • Multiple arguments: The data values that need to be printed on the standard output.

Return Value of printf() in C++

The printf() method returns the total number of characters that are printed. If any kind of an error occurs the printf() method returns a negative value i.e -1 in this case.

Exceptions for printf in C++

The printf() method doesn't throw any kind of exception.

Example of printf() Function

Let's understand the prinf() method using an example

In the below example, we are printing the int value to the standard output using the printf() method.

Output:

What is Printf in C++?

Let's suppose you want to print some values to the console of the c++ program, How you can do this? We will use the printf() method to print the various types of values using the different kinds of format specifiers.

Difference Between the Cout and the printf in c++

The printf() method sends the formatted string according to the format specifiers to the standard output, whereas the cout doesn't format the arguments to the String.

printf() Format Specifier Prototype

The format specifier is used to customize as well as define the output values. Format specifiers are present in the c++ language for all the datatypes. Let's understand the format of the format specifier.

Syntax:

  • %: Every format specifier starts with this symbol.
  • flags: The flags are optional the are used to modify the conversion behavior.
  • width: This is an optional parameter that is used to specify the width field.
  • precision: This is used to specify the precision of the decimal values.
  • specifier: This is a conversion format specifier.

Commonly Used Format Specifiers

Let's understand the commonly used format specifiers in the printf() method.

Format SpecifierDescription
aThis is used to write a single character.
sThis is used to write a character string.
d or iThis is used to convert a signed integer to decimal value
uThis modifier converts the unsigned integer to decimal values.
E or eThis modifier is used to convert the floating numbers to the exponential values.

Example:

  1. printf() in c++ Let's understand the printf() in c++ using an example.

In the below example, we are printing the value of various datatypes using the printf() method.

Output:

Conclusion

  • printf() method is used to write a output.
  • printf() returns negative value, if any kind of error occurs.
  • There are multiple types of format specifers are used in the printf() method.