sprintf - C stdio.h

C examples for stdio.h:sprintf

Type

function

From


<cstdio>
<stdio.h>

Description

Write formatted data to string

Prototype

int sprintf ( char * str, const char * format, ... );

Parameters

ParameterDescription
str Pointer to a buffer
format a format string

A format specifier follows this prototype:

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

specifier character defines the type and the interpretation of its corresponding argument:

specifier Output
d or iSigned decimal integer
uUnsigned decimal integer
oUnsigned octal
xUnsigned hexadecimal integer
XUnsigned hexadecimal integer (uppercase)
fDecimal floating point, lowercase
FDecimal floating point, uppercase
eScientific notation (mantissa/exponent), lowercase
EScientific notation (mantissa/exponent), uppercase
gUse the shortest representation: %e or %f
GUse the shortest representation: %E or %F
aHexadecimal floating point, lowercase
AHexadecimal floating point, uppercase
cCharacter
sString of characters
pPointer address
nNothing printed. The argument must be a pointer to a signed int. The number of characters written so far is saved to the signed int.
%A % followed by another % character will write a single % to the stream.

The format specifier can contain the following sub-specifiers. They are optional.

  • flags,
  • width,
  • .precision and
  • modifiers,

The flags can be the following value.

flagsdescription
-Left-justify within the given field width; Right justification is the default.
+Add a plus or minus sign (+ or -) for numbers. By default, only negative numbers are preceded with a - sign.
(space) If no sign is written, a blank space is inserted before the value.
#Used with o, x or X specifiers, the value is preceeded with 0, 0x or 0X respectively for nonzero values.
0Left-pads the number with 0 instead of spaces when padding is specified.

The width can the following values;.

widthdescription
(number)Minimum number of characters to be printed. If the value is shorter, blank spaces is padding. If the result is wider, it is not truncated.
*An additional integer value preceding the argument that has to be formatted.

The .precision can have the following value.

.precision
description
.number







For specifiers (d, i, o, u, x, X): precision sets the minimum number of digits.
If the value is shorter, the result is padded with leading zeros. No truncating if the result is wider.
A precision of 0 means that no character is written for the value 0.
For a, A, e, E, f and F specifiers: it sets the number of digits after the decimal point. Default is 6.
For g and G: it is the maximum number of significant digits.
For s: it is the maximum number of characters to be printed.
By default all characters are printed until the ending null character is encountered.
If the period is specified without a value for precision, 0 is assumed.
.*
The precision is not set, but as an additional integer value argument preceding the argument that has to be formatted.

Return Value

On success, the total number of characters written is returned.

On failure, a negative number is returned.

Demo Code


#include <stdio.h>

int main ()//from w ww.j a  va  2s . c o  m
{
  char buffer [50];

  int n, a=5, b=3;

  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);

  printf ("[%s] is a string %d chars long\n",buffer,n);

  return 0;
}

Related Tutorials