Header file:
stdio.h
Synopsis:
int sprintf(char *str, const char *format, ...);
Description:
Performs formatted output conversion and writes the output to str. It gives us the number of characters written into the output string (excludes '\0') as return value.
sprintf function C example:
#include<stdio.h>
#include<string.h>
int main() {
#include<string.h>
int main() {
char name[100], company[100], str[256];
int age, salary;
printf("Enter your name:\n");
fgets(name, 90, stdin);
name[strlen(name) - 1] = '\0';
printf("Enter your designation:\n");
fgets(company, 90, stdin);
company[strlen(company) - 1] = '\0';
printf("Enter your age and salary:\n");
scanf("%d%d", &age, &salary);
sprintf(str, "%s works as %s, earns %d per month "
"at the age of %d\n", name, company, salary, age);
printf("Output:\n%s", str);
return 0;
}
Output:
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
Enter your name:
Micheal
Enter your designation:
Engineer
Enter your age and salary:
22 10000
Output:
Micheal works as Engineer, earns 10000 per month at the age of 22
No comments:
Post a Comment