<Input and Output of Data>From beginner to expert, from Hello World to ACMAll practical content, no textbooks required!
1. Formatted Output with printf Function
Basic Usage of printf Function
<span>printf</span> is responsible for outputting information to the screen.
Basic Syntax:
printf("format control string", output item list);
Detailed Explanation of Format Specifiers
|
Format Specifier |
Meaning |
Example |
Output Result |
|---|---|---|---|
|
|
Decimal Integer |
|
100 |
|
|
Floating Point Number |
|
3.140000 |
|
|
Character |
|
A |
|
|
String |
|
hello |
|
|
Keep 2 decimal places |
|
3.14 |
Common Mistakes Reminder
Format Specifier Must Match Data Type
int age = 18;printf("Age: %d years\n", age); // Correctprintf("Age: %f years\n", age); // Error! Outputs garbage
2. Formatted Input with scanf Function
Basic Usage of scanf Function
<span>scanf</span> is responsible for receiving input from the keyboard.
Basic Syntax:
scanf("format control string", variable address list);
Input of Various Data Types
#include<stdio.h>int main() { int age; float score; char grade; printf("Please enter age, score, grade: "); scanf("%d %f %c", &age, &score, &grade); printf("Age:%d, Score:%.1f, Grade:%c\n", age, score, grade); return 0;}
Common Questions and Answers
Q: Why do we need to add<span>&</span> before the variable in scanf?
A:<span>&</span> is the address-of operator, which tells scanf where to store the data in memory. Forgetting to add<span>&</span> is a common mistake for beginners.
Q: What is the role of spaces in the format control string during input?
A: Spaces can match any number of whitespace characters (spaces, tabs, newlines), making input more flexible.
3. Character Input and Output: getchar, putchar
Input and Output of Single Characters
#include <stdio.h>int main() { char ch; printf("Please enter a character: "); ch = getchar(); // Get a character printf("The character you entered is: "); putchar(ch); // Output a character putchar('\n'); // Output newline return 0;}
Clearing Input Buffer
// When mixing scanf and getchar, you need to clear the bufferint num;char ch;printf("Please enter a number: ");scanf("%d", &num);getchar(); // Absorb the newline characterprintf("Please enter a character: ");ch = getchar();
4. Common Input and Output Errors and Debugging
Common Error Types
-
Format Mismatch Error
float f;scanf("%d", &f); // Error: should use %f
-
Forgot to Use Address Operator
int x;scanf("%d", x); // Error: should be &x
-
Buffer Residue Issues
int a;char c;scanf("%d", &a);scanf("%c", &c); // Will read the previous input's newline
Debugging Tips
Use Debug Output:
int a, b;scanf("%d%d", &a, &b);printf("Debug: a=%d, b=%d\n", a, b); // Check if input is correct
In-Class Test
Practical Exercise Guidance
Task 1: Personal Information Entry System
#include <stdio.h>int main() { char name[20]; int age; float height; char gender; printf("=== Personal Information Entry ===\n"); printf("Please enter your name: "); scanf("%s", name); printf("Please enter your age: "); scanf("%d", &age); printf("Please enter your height (meters): "); scanf("%f", &height); printf("Please enter your gender (M/F): "); scanf(" %c", &gender); // Note the space before %c to absorb the newline printf("\n=== Entry Complete ===\n"); printf("Name: %s\n", name); printf("Age: %d years\n", age); printf("Height: %.2f meters\n", height); printf("Gender: %c\n", gender); return 0;}
Task 2: Simple Calculator
#include <stdio.h>int main() { double num1, num2; char operator; printf("Simple Calculator (format: number1 operator number2): "); scanf("%lf %c %lf", &num1, &operator, &num2); switch(operator) { case '+': printf("Result: %.2f\n", num1 + num2); break; case '-': printf("Result: %.2f\n", num1 - num2); break; case '*': printf("Result: %.2f\n", num1 * num2); break; case '/': if(num2 != 0) printf("Result: %.2f\n", num1 / num2); else printf("Error: Divisor cannot be 0!\n"); break; default: printf("Error: Unsupported operator!\n"); } return 0;}
Common Mistake Detection
Identify the errors in the following code:
#include <stdio.h>int main() { int a, b; float c; char d; printf("Enter two integers: "); scanf("%d %d", a, b); // Error 1 printf("Enter a floating point number: "); scanf("%f", c); // Error 2 printf("Enter a character: "); d = getchar(); scanf("%c", &d); // Error 3 return 0;}
Homework
Basic Questions (Required) – Luogu Problem Numbers
-
P1001: A+B Problem – Input two integers and output their sum
-
P1421: Xiaoyu Buys Stationery – Input amount and product price, calculate how many can be bought
-
P1085: Unhappy Jinjing – Input study time for 7 days, determine which day was the least happy
Advanced Questions (Optional) – Luogu Problem Numbers
-
P1046: Taotao Picks Apples – Input apple height and Taotao’s reach height, calculate how many apples can be picked
-
P1055: ISBN Number – Input ISBN number, verify and output the correct result
Thought Questions
-
Write a program to input a three-digit number and output its units, tens, and hundreds digits separately
Homework Answers
P1001: A+B Problem Answer
#include <stdio.h>int main() { int a, b; scanf("%d %d", &a, &b); printf("%d\n", a + b); return 0;}
P1421: Xiaoyu Buys Stationery Answer
#include <stdio.h>int main() { int a, b; scanf("%d %d", &a, &b); int total = a * 10 + b; // Convert to jiao printf("%d\n", total / 19); // Each stationery costs 19 jiao return 0;}
P1085: Unhappy Jinjing Answer
#include <stdio.h>int main() { int school, home, total; int max_day = 0, max_hours = 8; for(int i = 1; i <= 7; i++) { scanf("%d %d", &school, &home); total = school + home; if(total > max_hours) { max_hours = total; max_day = i; } } printf("%d\n", max_day); return 0;}
Thought Question Answer
#include <stdio.h>int main() { int num; printf("Please enter a three-digit number: "); scanf("%d", &num); int hundred = num / 100; int ten = (num % 100) / 10; int one = num % 10; printf("Hundreds: %d\n", hundred); printf("Tens: %d\n", ten); printf("Units: %d\n", one); return 0;}
In the next class, we will learn:
Relational Operations and Logical Operations
Remember to practice more on Luogu, these are great practical training!
If you have any questions, feel free to comment!
Follow YunJie Algorithm, no textbooks needed, just a few clicks to systematically learn programming!