C++ printf()

The printf() function in C++ is used to write a formatted string to the standard output (stdout). It is defined in the cstdio header file.

Example

#include <cstdio>

int main() {
  int age = 23;

// print a string literal printf("My age is ");
// print an int variable printf("%d", age);
return 0; } // Output: My age is 23

printf() Syntax

The syntax of printf() is:

printf(const char* format, ...);

Here,

  • format is the string that is to be written to the standard output
  • ... in the above code signifies you can pass more than one argument to printf().

printf() Parameters

The printf() function takes the following parameters:

  • format - pointer to a null-terminated string (C-string) that is written to stdout. It consists of characters along with optional format specifiers starting with %.
  • ... - other additional arguments specifying the data to be printed. They occur in a sequence according to the format specifier.

printf() Return Value

The printf() function returns:

  • On Success - the number of characters written
  • On failure - a negative value

printf() Prototype

The prototype of the printf() function as defined in the cstdio header file is:

int printf(const char* format, ... );

Example 1: C++ printf()

#include <cstdio>

int main() {
  int num = 5;
  char my_name[] = "Lincoln";

// print string and int variable printf("num = %d \n", num);
// print string and char array printf("My name is %s", my_name);
return 0; }

Output

num = 5 
My name is Lincoln

In this program, we have used the printf() function to print the integer num and the C-string my_name.

printf("num = %d \n", num);
printf("My name is %s", my_name);

Here,

  • %d is replaced by the num variable in the output
  • \n is an escape sequence character that prints a new line
  • %s is replaced by the my_name C-string.

Format Specifier

The format parameter of printf() can contain format specifiers that begin with %. These specifiers are replaced by the values of respective variables that follow the format string.

A format specifier has the following parts:

  • A leading % sign
  • flags - one or more flags that modifies the conversion behavior (optional)
    • - : Left justify the result within the field. By default it is right justified.
    • + : The sign of the result is attached to the beginning of the value, even for positive results.
    • space: If there is no sign, a space is attached to the beginning of the result.
    • # : An alternative form of the conversion is performed.
    • 0 : It is used for integer and floating point numbers. Leading zeros are used to pad the numbers instead of space.
  • width - an optional * or integer value used to specify minimum width field.
  • precision - an optional field consisting of a . followed by * or integer or nothing to specify the precision.
  • length - an optional length modifier that specifies the size of the argument.
  • specifier - a conversion format specifier.

printf() Format Specifier Prototype

The general prototype of format specifier for printf() is:

%[flags][width][.precision][length]specifier

Commonly Used Format Specifiers

The table below lists some commonly used format specifiers:

Format Specifier Description
% a % followed by another % character prints % to the screen
c writes a single character
s writes a character string
d or i converts a signed integer to decimal representation
o converts an unsigned integer to octal representation
X or x converts an unsigned integer to hexadecimal representation
u converts an unsigned integer to decimal representation
F or f converts floating-point number to the decimal representation
E or e converts floating-point number to the decimal exponent notation
A or a converts floating-point number to the hexadecimal exponent
G or g converts floating-point number to either decimal or decimal exponent notation
n - returns the number of characters written so far
- the result is written to the value pointed to by the argument
- the argument must be a pointer to signed int
p writes an implementation-defined character sequence defining a pointer

Example 2: C++ More examples on printf()

#include <cstdio>

int main() {
  char ch = 'a';
  float a = 5.0, b = 3.0;
  int num = 10;

// set precision to 3 decimal places printf("%.3f / %.3f = %.3f \n", a, b, a / b);
// set width to 5 digits with * printf("Setting width %*c \n", 5, ch);
// get octal value of an integer printf("Octal equivalent of %d is %o", num, num);
return 0; }

Output

5.000 / 3.000 = 1.667
Setting width     a 
Octal equivalent of 10 is 12 

In this program, we have used the printf() function three times.

1. In the 1st printf() function:

  • %.3f - sets the precision of float variables to 3 decimal places.
  • The first %.3f is replaced by the value of the 2nd parameter a.
  • The second %.3f is replaced by the value of the 3rd parameter b.
  • The last %.3f is replaced by the value of the 4th parameter a / b.

2. In the 2nd printf() function:

  • %*c - prints the char variable ch (3rd parameter) with an unspecified width.
  • The width is later specified as five digits by the 2nd argument 5.

3. In the 3rd printf() function:

  • %d - prints the value of the int variable num in decimal number system
  • %o - prints the value of the int variable num in octal number system

Also, the escape character \n prints a new line.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges