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.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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.

Sharpen Your Fundamentals with Free Learning

Exceptions for printf in C++

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

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.