Dear “Reboot Heroes”, after the training in the first three lessons, we can now: ✅ Make the computer speak (printf) ✅ Order takeout for the computer (#include) ✅ Make the computer remember things (variables) But I wonder if you feel a bit constrained—why do we always have to hard-code content? Why can’t the program listen to what we want to say? Today, this frustrating era ends! Because we have invited the golden partner of printf, the “ear” of the programming world— scanf function!
1. scanf is the “ear” of the program. If printf is the mouth of the program, responsible for output; then scanf is the ear of the program, responsible for input. Its job is to “listen” to what you type on the keyboard and then store it in a variable.
2. The usage of the “ear” is similar to printf but in the opposite direction: scanf(“format control string”, &variable_name); Important! Important! Important! The & symbol must not be omitted! (Don’t ask why yet, just remember this is the “delivery address”, and it will be explained later.)
3. Practical application! Let’s have a conversation with the program!
Scene 1: Let the program remember your name#include <stdio.h>int main(){ char name[20]; // Create a “big box” that can store 20 characters printf(“Please enter your name:”); scanf(“%s”, name); // Note: there is no & before name! printf(“Hello, %s! Welcome to the world of Reboot Heroes!\n”, name); return 0;}
Run result: Please enter your name: AliceHello, Alice! Welcome to the world of Reboot Heroes!
Interpretation: · char name[20]: Create a “big box” (array) that can store strings. · scanf(“%s”, name): “Please store the user-input string into the name box!” · Strings are special, so no & is needed.
Scene 2: Let the program do math for you#include <stdio.h>int main(){ int a, b; int sum; printf(“I will perform addition magic for you!\n”); printf(“Please enter the first number:”); scanf(“%d”, &a); // Store the first input number into box a printf(“Please enter the second number:”); scanf(“%d”, &b); // Store the second input number into box b sum = a + b; printf(“Look at my magic: %d + %d = %d\n”, a, b, sum); return 0;}
Run result: I will perform addition magic for you!Please enter the first number: 5Please enter the second number: 3Look at my magic: 5 + 3 = 8
Wow! The program really understands what we are saying!
4. What is that mysterious & symbol? It’s time to unveil this mystery! Imagine: there are 100 identical small boxes in your community, all called score. The courier has a package labeled “For score”; which box should he put it in? &score is the specific address of the score box! · score: the content inside the box · &score: the specific address of the box. scanf needs to know the specific address to accurately place the item in the correct box!
Let’s do an experiment: what happens if you forget to write &? #include <stdio.h>int main(){ int age; printf(“Please enter your age:”); scanf(“%d”, age); // Error! Forgot to write & printf(“Your age is: %d\n”, age); return 0;}
Running this program and entering a number… the program may crash or output garbled text! This is the consequence of writing the address incorrectly—the courier delivered the package to the wrong place!
5. Different data types have different “listening formats”. The program’s “ear” is picky; different data types require different “listening methods”: · int (integer): use %d · float (decimal): use %f · char (character): use %c · char[] (string): use %s#include <stdio.h>int main(){ int age; float height; char grade; printf(“Please enter your age, height, and grade level in order:”); scanf(“%d %f %c”, &age, &height, &grade); printf(“Age: %d years, Height: %.2f meters, Grade: %c\n”, age, height, grade); return 0;}
6. Ultimate fun: Create a personalized greeting program. Now, let’s combine all skills to create a truly interactive program!#include <stdio.h>int main(){ char name[20]; int age; float score; printf(“===== Personalized Greeting Generator =====\n”); printf(“Please enter your name:”); scanf(“%s”, name); printf(“Please enter your age:”); scanf(“%d”, &age); printf(“Please enter your C language exam score:”); scanf(“%f”, &score); printf(“\n”); // Empty line printf(“🎉 Generating your exclusive greeting! 🎉\n”); printf(“Dear %s, you are %d years old,\n”, name, age); printf(“You scored %.1f in the C language exam!\n”, score); if(score >= 60) { printf(“Great! You have surpassed the realm of just rebooting!\n”); } else { printf(“Don’t be discouraged! Come learn more at ‘Reboot Heroes’!\n”); } return 0;}
Interactive session: “Write a program using scanf to let the user input two numbers and then output their product! Send the code and running screenshot to the backend to show off your ‘dialogue magic’!”