snprintf - C stdio.h

C examples for stdio.h:snprintf

Type

function

From


<cstdio>
<stdio.h>

Description

Write formatted output to sized buffer,

Prototype


int snprintf ( char * s, size_t n, const char * format, ... );

Parameters

ParameterDescription
s Pointer to a buffer.
n Maximum number of bytes to be used in the buffer.
format format string as format in printf.

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, number of characters written.

On occurs, a negative number is returned.

Demo Code


#include <stdio.h>

int main ()//from  ww w . java2  s . c om
{
  char buffer [100];
  int cx;

  cx = snprintf ( buffer, 100, "test %d is %d %f", 60, 60/2,1.1234 );

  if (cx>=0 && cx<100)
      snprintf ( buffer+cx, 100-cx, ", this is a test and the half of that is %d.", 60/2/2 );

  puts (buffer);

  return 0;
}

Related Tutorials