#include <stdio.h> int sprintf(char *dest, const char *format, var1, var2, ...);
sprintf
writes formatted output to the area addressed by dest
under the control of the string addressed by format
. In the argument
list following format
, there may be one or more additional arguments
whose values are to be formatted and transmitted.
The string pointed to by format
is in the same form as that used by
fprintf
. Refer to the fprintf
description for detailed
information concerning the formatting conversions.
sprintf
returns the number of characters written to the area addressed
by dest
.
sprintf
. Thus, you must ensure that the destination area is
large enough.
sprintf
is just like fprintf
, with two exceptions:
dest
.
#include <stdio.h> #include <string.h> char *names[] = { "John M. Brown", "Daniel Lopez", "H. Margaret Simmons", "Ralph Jones", "Harry L. Michaels" }; main() { char lfm[94]; char first[31], last[31], middle[31]; int i, n; puts("The names in f-m-l format are:"); for (i = 0; i < sizeof(names)/sizeof(names[0]); ++i) puts(names[i]); puts("nThe names in l-f-m format are:"); for (i = 0; i < sizeof(names)/sizeof(names[0]); ++i) { n = sscanf(names[i], "%s %s %s", first, middle, last); if (n != 3){ /* There was no middle name. */ strcpy(last, middle); middle[0] = '0'; } sprintf(lfm, "%s, %s %s", last, first, middle); puts(lfm); } }
format
, fprintf
, sprintf
, vsprintf
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.