Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

sprintf - Formatted output to a string

&pagelevel(4)&pagelevel

Definition

#include <stdio.h>

int sprintf(char *s, const char *format, argumentlist);

sprintf edits data (characters, strings, numerical values) according to specifications in the string format and writes this data to the area pointed to by s.

sprintf works like printf, except that the edited data is written to a string and not to the standard output.

Parameters

char *s

Pointer to the result string. sprintf terminates the string with the null byte (\0).

const char *format

Format string as described under printf with KR or ANSI functionality (cf. printf).

The only difference is with regard to the way in which the control characters for white space (\n, \t, etc.) are handled. As opposed to printf, sprintf enters the EBCDIC value of the control character in the result string. It is only during output to text files that the control characters are converted to their appropriate effect depending on the type of text file (see section “White space” (Basic terms)).

argumentlist

Variables or constants whose values are to be converted and formatted for output according to the information in the format statements.
If the number of format statements does not match the number of arguments, the following applies:
If there are more arguments, the surplus arguments are ignored.
If there are fewer arguments, the results are undefined.

Return val.

Number of characters stored in s.

The terminating null byte (\0) generated by sprintf is not included in this total.

Notes

You must see to it that the area to which s points is large enough for the result!

sprintf rounds to the specified precision when converting floating-point numbers.

sprintf does not convert one data type to another. A value must be explicitly converted (e.g. with the cast operator) if it is not to be output in conformity with its type.

Maximum number of characters to be output:
With KR functionality (applies to C/C++ versions prior to V3.0 only) a maximum of 1400 characters can be output per sprintf call, with ANSI functionality a maximum of 1400 characters per conversion element (e.g. %s).

The behavior is undefined if memory areas overlap.

Attempts to output non-initialized variables or to output variables in a manner inconsistent with their data type can lead to undefined results.

The behavior is undefined if the percent sign (%) in a format statement is followed by an undefined formatting or conversion character.

Example

You can use sprintf to copy a string, for example. It is thus possible to implement the strncpy function. The example under strncpy would then appear as follows:

#include <stdio.h>
int main(void)
{
   int n;
   char *s2 = "Peter is going swimming !";
   char s1[BUFSIZ];
   printf("The sentence is : %s \nCopy how many characters ?\n", s2);
   scanf("%d",&n);
               /* Alternatively, the following call
                  could appear at this point:
                  strncpy(s1,s2,n);        */
  sprintf(s1,"%.*s",n,s2);
  printf("%s \n",s1);
  return 0;
}

See also

printf, fprintf, snprintf, putc, putchar, puts, sscanf