The sprintf()
function in C++ is used to write a formatted string to character string buffer. It is defined in the cstdio header file.
Example
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
char buffer[100];
int age = 23;
// print "My age is " and age variable to buffer variable
sprintf(buffer, "My age is %d", age);
// print buffer variable
cout << buffer;
return 0;
}
// Output: My age is 23
sprintf() Syntax
The syntax of sprintf()
is:
sprintf(char* buffer, const char* format, ...);
Here,
- buffer is the string buffer to which we need to write
- format is the string that is to be written to the string buffer
...
in the above code signifies you can pass more than one argument tosprintf()
.
sprintf() Parameters
The sprintf()
function takes the following parameters:
- buffer - pointer to the string buffer to write the result.
- format - pointer to a null-terminated string (C-string) that is written to the string buffer. 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.
sprintf() Return Value
The sprintf()
function returns:
- On Success - the number of characters written for sufficiently large buffer (excluding the terminating null character
'\0'
) - On failure - a negative value
sprintf() Prototype
The prototype of the sprintf()
function as defined in the cstdio header file is:
int sprintf(char* buffer, const char* format, ...);
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.
sprintf() Format Specifier Prototype
The general prototype of format specifier for sprintf()
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: C++ sprintf()
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
char buffer[100];
int count;
char name[] = "Max";
int age = 23;
// write combination of strings and variables to buffer variable
// store the number of characters written in count
count = sprintf(buffer, "Hi, I am %s and I am %d years old", name, age);
// print the string buffer
cout << buffer << endl;
// print the number of characters written
cout << "Number of characters written = " << count;
return 0;
}
Output
Hi, I am Max and I am 23 years old Number of characters written = 34