HP C/iX Library Reference Manual (30026-90004)
Chapter 5 321
HP C/iX Library Function Descriptions
sprintf
{
char name[31], prof[31], hdate[7], curve[3], string[81];
char *format = "%30s%2d%30s%6ld%6s%2d%2s";
int age, rank;
long salary;
/* start asking questions */
printf("\nName (30 chars max): ");
gets(name);
while(name[0] != ']') {
printf("Age: ");
printf("Job title (30 chars max): ");
gets(prof);
printf("Salary (6 digits max, no comma): ");
printf("Hire date (numerical MMDDYY): ");
gets(hdate);
printf("Pay curve: ");
gets(curve);
/* format string */
sprintf(string,format,name,age,prof,salary,hdate,rank,curve);
printf("\n%s\n", string);
/* start next round */
printf("\nName (30 chars max): ");
gets(name);
}
}
The program above asks questions about name, age, job title, salary, hire date, ranking,
and pay curve. This data is then packed into a 78-character string using the sprintf()
function. This program writes the string on your screen, but in an actual working
environment, the string would probably be passed directly to the database program. Note
that sprintf() function format is speciļ¬ed as an explicit character pointer. When lengthy,
unchanging formats are used, this is more convenient than typing the entire format string,
especially if the item list is long.
See Also
setlocale(), putc(), scanf(), ANSI C 4.9.6.5, POSIX.1 8.1