Formatted I/O: printf() and scanf()
These functions use Format Specifiers to tell the computer what kind of data is being moved.
printf(): Sends data to the screen.
#include <stdio.h>
int main() {
int age;
float weight;
char grade;
printf("Enter your grade (A/B/C): ");
scanf(" %c", &grade); // Note the space before %c to skip any leftover newline characters
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your weight: ");
scanf("%f", &weight);
printf("\n--- Summary ---\n");
printf("Grade: %c | Age: %d | Weight: %.1f kg\n", grade, age, weight);
return 0;
}Expected Output:
Enter your grade (A/B/C): A
Enter your age: 25
Enter your weight: 70.5
--- Summary ---
Grade: A | Age: 25 | Weight: 70.5 kg3. Character & String I/O
Sometimes you don't need formatted data—just raw characters or lines of text.
getchar()/putchar(): For a single character.
#include <stdio.h>
int main() {
char ch;
printf("Type a character: ");
ch = getchar(); // Read one char
printf("You typed: ");
putchar(ch); // Print one char
printf("\n");
return 0;
}Expected Output:
Type a character: Z
You typed: ZSpecifier
DataType
%dInteger
%fFloat
%lfDouble
%cCharacter
%sString (Array of characters).