In C, formatted input/output functions are used to control the way data is displayed or read. The primary functions for formatted I/O are part of the stdio.h (standard input-output) library. Here are some commonly used formatted input/output functions in C:
Formatted Output Functions (printf):
Example 1: Print Integer and String
#include <stdio.h>
int main() {
int age = 25;
char name[] = “John Doe”;printf(“Age: %dn”, age);
printf(“Name: %sn”, name);return 0;
}
Output:
Age: 25
Name: John Doe
Example 2: Print Floating-Point Numbers with Precision
#include <stdio.h>
int main() {
double pi = 3.141592653589793;printf(“Pi (with precision): %.2fn”, pi);
return 0;
}
Output:
Pi (with precision): 3.14
Formatted Input Functions (scanf):
Example 1: Read Integer and String
#include <stdio.h>
int main() {
int age;
char name[50];printf(“Enter your age: “);
scanf(“%d”, &age);printf(“Enter your name: “);
scanf(“%s”, name);printf(“Age: %dn”, age);
printf(“Name: %sn”, name);return 0;
}
Output:
Enter your age: 25
Enter your name: John
Age: 25
Name: John
Example 2: Read Floating-Point Numbers
#include <stdio.h>
int main() {
double height;printf(“Enter your height in meters: “);
scanf(“%lf”, &height);printf(“Your height is: %.2f metersn”, height);
return 0;
}
Output:
Enter your height in meters: 1.75
Your height is: 1.75 meters