初学C语言疑问:如何用getchar或scanf_s实现两个变量的输入
getchar() and scanf_s() to Input Two Variables in C Hey there! I totally get that "the answer's right in front of me but I can't reach it" feeling when you're starting out with C—we've all been stuck on these small, tricky steps. Let's walk through exactly how you can use both getchar() and scanf_s() to get your X and Y variables from the user.
First Up: scanf_s() (The Straightforward Pick for Numeric Input)
If X and Y are numeric types (like int, float, etc.), scanf_s() is your easiest bet since it's designed to read formatted input directly into variables. Here's how to do it:
Example for Integer Variables
#include <stdio.h> int main() { int X, Y; printf("Enter two integers separated by space or enter: "); // scanf_s requires passing the address of variables scanf_s("%d %d", &X, &Y); printf("You entered X: %d, Y: %d\n", X, Y); return 0; }
A quick note: scanf_s is Microsoft's secure alternative to standard scanf—it adds extra safety checks, but for basic integer input like this, the syntax is almost identical to scanf. Just make sure your format specifiers match your variable types (e.g., %f for float, %lf for double).
Using getchar() (More Manual, But Flexible)
getchar() reads single characters from input, so it takes a bit more work if you're dealing with numeric variables (since you'll have to convert characters to numbers). But it's totally doable—let's cover two scenarios:
Scenario 1: X and Y are Single Characters
If you just want to read two single characters (like letters or digits), you can use getchar() twice. Just watch out for newline characters left in the input buffer!
#include <stdio.h> int main() { char X, Y; printf("Enter two characters (one after another): "); X = getchar(); // Skip any whitespace (like the newline if user pressed enter) while (getchar() != '\n'); Y = getchar(); printf("You entered X: %c, Y: %c\n", X, Y); return 0; }
Scenario 2: X and Y are Integers (Using getchar())
For integers, you'll need to read each character, convert it to its numeric value (by subtracting the ASCII value of '0'), and build up the number. Here's a simple example for single-digit integers:
#include <stdio.h> int main() { int X, Y; printf("Enter two single-digit integers: "); // Convert character to integer: '0' is ASCII 48, so '5' - '0' = 5 X = getchar() - '0'; // Skip any spaces or newlines between inputs while (getchar() != '\n'); Y = getchar() - '0'; printf("You entered X: %d, Y: %d\n", X, Y); return 0; }
If you need to handle multi-digit numbers later, you'd loop through each character until you hit a non-digit, accumulating the value (e.g., X = X * 10 + (getchar() - '0')). That's a bit more advanced, but totally manageable as you learn more!
Wrap-Up
Both methods work perfectly—scanf_s() is great for quick, formatted numeric input, while getchar() gives you more control over individual characters (which is useful for things like input validation later on). Start with scanf_s() for your current need, then play around with getchar() once you're comfortable with the basics.
内容的提问来源于stack exchange,提问作者Robert

