sprintf()

Formats a string.

Synopsis

int sprintf(string &s, string format [,<type1> var1 [,<type2> var2 ...]]);

Parameters

Parameter Description
s String. The characters are written to this string.
format

Format. How are the characters displayed, for example, as a string %s. See the "Format" table below.

%[*][width][length]specifier, e.g. %+6.2f - see the format table below.

var1, var2, ... Values that are written to the string "s".

Format

Specifier Description
c A character (char).
d

A decimal number. Any number of decimal digits.

+/- can be optionally preceded.

o

An octal number without leading zeros. Any number of octal digits 0-7.

+/- can be optionally preceded.

u An unsigned decimal number. Any number of decimal digits.
x

A hexadecimal number (without leading 0x). Any number of hexadecimal digits (0-0, a-f).

+/- can be optionally preceded.

X Like x. Any number of hexadecimal digits (0-0, A-F).
l Prefix e.g. "%ld" or "%lu" for long and ulong values.
f, e, E, g,G

A series of decimal digits.

+/- can be optionally preceded and can contain a decimal point.

e,E: The exponent always contains at least two places. If the value is zero, the exponent is zero.

g: Dependent on the value. The conversion type e is only used when the exponent is less than -4 or greater than the accuracy.

0Number: The minimum number of characters that should be output. If the minimum is not reached then leading zeros are added, e.g. the number 45.765998765 formatted with %07.2f' = 0045.77 means that 7 numbers are output with leading zeros and two decimal places (.2). However, the result can contain more then 7 characters.

.Number: Specifies the number of decimal numbers (decimal places) that should be output.

s A string. Arbitrary characters except for whitespace characters.
Flag Meaning
* An asterisk is used for scanning characters but the characters are not saved.
blank A blank between the percent sign and the width (Number) allows to allocate space for the sign. e.g. % 6.2f. See examples at the end of this page.

Return value

see Description

Errors

missing/incorrect arguments

Description

Writes outputs under the specified format to the string s and returns the number of characters written, in the event of errors a negative number. Otherwise, sprintf() functions like fprintf().

Example

The example shows how to use the sprintf function. The example includes formatting of different variable types (float, int and string), signed variables, alignment of the formatted string, a specific length of variables etc. (see example below).

main()
{
  float rawValueF1 = 45.765998765,
  rawValueF2 = 1637451.6543,
  rawValueF3 = 2.1,
  rawValueF4 = -3.112,
  rawValueF5;
  int rawValueD1 = 1234567890,
  rawValueD2 = 12;
  string rawValueS1 = "ABC",
  rawValueS2 = "ABCDEFGHIJKL",
  valueString ="yxxxxx";

  //Formatting of float variables
  sprintf(valueString, "%6.2f", rawValueF1);
  DebugN(rawValueF1 + "  formatted '%6.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  sprintf(valueString, "%6.2f", rawValueF2);
  DebugN(rawValueF2 + "  formatted '%6.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  sprintf(valueString, "%6.2f", rawValueF3);
  DebugN(rawValueF3 + "  formatted '%6.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  sprintf(valueString, "%6.2f", rawValueF4);
  DebugN(rawValueF4 + "  formatted '%6.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  //* A blank between the percent sign and field width allows you to reserve space for the sign.
  sprintf(valueString, "% 6.2f", -rawValueF4);
  DebugN(rawValueF4 + "  formatted '% 6.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  //Also a plus sign allows you to reserve space for a sign
  sprintf(valueString, "%+6.2f", -rawValueF4);
  DebugN(-rawValueF4 + "  formatted '%+6.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  sprintf(valueString, "%+6.2f", rawValueF4);
  DebugN(rawValueF4 + "  formatted '%+6.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  //Leading zeros
  sprintf(valueString, "%07.2f", rawValueF1);
  DebugN(rawValueF1 + "  formatted '%07.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  /* Alignment of the valueString with a leading sign, alignment left (default alignment right)*/
  sprintf(valueString, "%8.2f", rawValueF3);
  DebugN(rawValueF3 + "  formatted '%8.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  sprintf(valueString, "%-8.2f", rawValueF3);
  DebugN(rawValueF3 + "  formatted '%-8.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  //Maximum field width for integers(rawValueD1)
  sprintf(valueString, "%6d", rawValueD1);
  DebugN(rawValueD1 + "  formatted '%6d' -> '"+valueString+"'    length: " + strlen(valueString));

  sprintf(valueString, "%6d", rawValueD2);
  DebugN(rawValueD2 + "  formatted '%6d' -> '"+valueString+"'    length: " + strlen(valueString));

  //Formatting of strings (specific length)
  sprintf(valueString, "%-6s", rawValueS1);
  DebugN(rawValueS1 + "  formatted '%-6s' -> '"+valueString+"'    length: " + strlen(valueString));

  /* precision in the string defines the number of used characters for the variable */
  sprintf(valueString, "%8.2s", rawValueS2);
  DebugN(rawValueS2 + "  formatted '%8.2' -> '"+valueString+"'    length: " + strlen(valueString));

  //precision in string can be combined with the alignment
  sprintf(valueString, "%-8.2s", rawValueS2);
  DebugN(rawValueS2 + "  formatted '%8.2' -> '"+valueString+"'    length: " + strlen(valueString));

  /* A format string can contain fixed characters. These are added to the formatted value */
  sprintf(valueString, "PREFIX %6.2f", rawValueF1);
  DebugN(rawValueF1 + "  formatted 'PREFIX  %6.2f' -> '"+valueString+"'    length: " + strlen(valueString));

  /* A format string can receive its additional characters from data point attributes */
  dpGet("ExampleDP_Arg1.:_original.._value",rawValueF5);
  sprintf(valueString, "%6.2f "+dpGetUnit("ExampleDP_Arg1.:_original.._value"), rawValueF5);
  DebugN("Original value: ",rawValueF5 + " Mit %6.2f formatted value-> '"+valueString+"'length: " + strlen(valueString));
}

Assignment

Strings

Availability

CTRL