Definition | #include <stdio.h> int sprintf(char *s, const char *format, argumentlist);
|
Parameters | char *s Pointer to the result string. const char *format Format string as described under 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 argumentlist Variables or constants whose values are to be converted and formatted for output according to the information in the format statements. |
Return val. | Number of characters stored in s. The terminating null byte (\0) generated by |
Notes | You must see to it that the area to which s points is large enough for the result!
Maximum number of characters to be output: 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 #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 |