C 中 scanf() 的机制是什么

发布于 2024-09-05 01:06:43 字数 956 浏览 1 评论 0原文

所以我今天开始学习 C,作为练习,我被告知编写一个程序,要求用户输入数字,直到他们输入 0,然后将偶数和奇数相加。这是:

#include <stdio.h>;

int main() {
    int esum = 0, osum = 0;
    int n, mod;

    puts("Please enter some numbers, 0 to terminate:");
    scanf("%d", &n);

    while (n != 0) {
        mod = n % 2;
        switch(mod) {
        case 0:
            esum += n;
            break;
        case 1:
            osum += n;
        }
        scanf("%d", &n);
    }
    printf("The sum of evens:%d,\t The sum of odds:%d", esum, osum);
    return 0;
}

我的问题涉及 scanf() 函数的机制。似乎当您一次输入多个由空格分隔的数字时(例如 1 22 34 2 8),scanf() 函数会以某种方式记住该行中的每个不同数字,并分别为每个步骤执行 while 循环。为什么/如何发生这种情况?

命令提示符内的交互示例:

-> Please enter some numbers, 0 to terminate:  
42 8 77 23 11 (enter)  
0 (enter)  
-> The sum of evens:50,     The sum of odds:111

我通过命令提示符运行程序;它是使用 Visual Studio 为 win32 平台编译的。

So I started learning C today, and as an exercise I was told to write a program that asks the user for numbers until they type a 0, then adds the even ones and the odd ones together. Here it is:

#include <stdio.h>;

int main() {
    int esum = 0, osum = 0;
    int n, mod;

    puts("Please enter some numbers, 0 to terminate:");
    scanf("%d", &n);

    while (n != 0) {
        mod = n % 2;
        switch(mod) {
        case 0:
            esum += n;
            break;
        case 1:
            osum += n;
        }
        scanf("%d", &n);
    }
    printf("The sum of evens:%d,\t The sum of odds:%d", esum, osum);
    return 0;
}

My question concerns the mechanics of the scanf() function. It seems that when you enter several numbers at once separated by spaces (eg. 1 22 34 2 8), the scanf() function somehow remembers each distinct numbers in the line, and steps through the while loop for each one respectively. Why/how does this happen?

Example interaction within command prompt:

-> Please enter some numbers, 0 to terminate:  
42 8 77 23 11 (enter)  
0 (enter)  
-> The sum of evens:50,     The sum of odds:111

I'm running the program through the command prompt; it's compiled for win32 platforms with Visual Studio.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦慕雪 2024-09-12 01:06:43

请注意,每次执行循环时都会调用 scanf() ;每次使用参数“%d”和 &n 调用 scanf() 时,它都会将单个整数读入变量 n 并前进到输入流中紧邻该整数之后的位置。

您可以将输入流视为某种“字符串”。假设我输入“25 16 0”; scanf()读取第一个整数后,输入流变为“16 0”;如果再次调用 scanf(),您将读取整数 16 并且输入字符串变为“0”。

Notice that you're calling scanf() each time you go through the loop; each time you call scanf() with the arguments "%d" and &n, it reads a single integer into the variable n and advances to the position immediately after that integer in the input stream.

You can sort of think of the input stream as a "string" of sorts. Suppose I typed "25 16 0"; after scanf() reads the first integer, the input stream becomes "16 0"; if you call scanf() again, you'll read the integer 16 and the input string becomes "0".

香橙ぽ 2024-09-12 01:06:43

发生这种情况是因为 scanf 函数从标准输入获取输入。它被称为是有原因的:您输入的所有内容都会放入该流中,并且scanf读取该流。无论您放入输入流中的任何内容都将保留在那里,直到像 scanf 这样的东西将其从那里取出。

换句话说,scanf 并不真正“记住”任何东西。输入流负责所有“记忆”。

It happens because scanf function gets its input from the standard input stream. It is called stream for a reason: everything you enter is put into that stream, and scanf reads that stream. Whatever you put into the input stream will stay there until something like scanf gets it out of there.

In other words, scanf doesn't really "remember" anything. It is the input stream that does all the "remembering".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文