在提供输入后,什么不明显的条件可以决定getchar访问换行符的时间?

在提供输入后,什么不明显的条件可以决定getchar访问换行符的时间?,c,scope,stdin,getchar,C,Scope,Stdin,Getchar,我有两个函数,它们都从使用getchar获取输入 Example1()用于收集输入并返回值。但是,当在一行中调用两次时,此函数注册上一次调用getchar时提交的换行符。这对我来说没有意义,因为我认为getchar将被限制在调用它的实际范围内,在函数本身结束时结束 int Example1() { printf("Gather input from example1\n"); int user_input; while ((user_input =

我有两个函数,它们都从使用getchar获取输入

Example1()
用于收集输入并返回值。但是,当在一行中调用两次时,此函数注册上一次调用
getchar
时提交的换行符。这对我来说没有意义,因为我认为
getchar
将被限制在调用它的实际范围内,在函数本身结束时结束

int Example1()
{
    printf("Gather input from example1\n");

    int user_input;
    while ((user_input = getchar()) != '\n')
    {
        return user_input;
    }

    return 0;
}
Example2()
收集输入并将其存储在新创建的结构成员中。当调用两次时,此函数不会产生与第一个函数相同的问题

struct foo *Example2()
{

    struct foo *user= (struct foo*) malloc(sizeof(struct foo));
    
    printf("Gather input from example2\n");

    int user_input
    while ((user_input = getchar()) != '\n') 
    { 
        user->user_input= user_input;
    }
    
    return user;
}
我认为当函数返回
getchar
的实际缓冲区时,该函数调用的范围将消失。是什么原因导致换行符保留在
Example1()
函数的
stdin
缓冲区中,而不是保留在
Example2()
函数中

完整代码:

#include<stdlib.h>
#include<stdio.h>

struct foo {
    char user_input;
};

struct foo *Example2()
{

    struct foo *user= (struct foo*) malloc(sizeof(struct foo));
    
    printf("Gather input from example2\n");

    int user_input;
    while ((user_input = getchar()) != '\n') 
    { 
        user->user_input= user_input;
    }
    
    return user;
}

int Example1()
{
    printf("Gather input from example1\n");

    int user_input;
    while ((user_input = getchar()) != '\n')
    {
        return user_input;
    }

    return 0;
}

int main()
{
    struct foo *user = Example2();
    printf("Example 2 function user_input 1/2 : %d\n", user->user_input);
    
    struct foo *user2 = Example2();
    printf("Example 2 function user_input 2/2 : %d\n", user2->user_input);

    printf("Example 1 function user_input 1/2 : %d\n", Example1());
    printf("Example 1 function user_input 2/2 : %d\n", Example1());
}
#包括
#包括
结构foo{
字符用户输入;
};
结构foo*示例2()
{
struct foo*user=(struct foo*)malloc(sizeof(struct foo));
printf(“从示例2中收集输入”\n);
int用户输入;
而((用户输入=getchar())!='\n')
{ 
用户->用户输入=用户输入;
}
返回用户;
}
int Example1()
{
printf(“从示例1收集输入”\n);
int用户输入;
而((用户输入=getchar())!='\n')
{
返回用户输入;
}
返回0;
}
int main()
{
struct foo*user=Example2();
printf(“示例2函数用户输入1/2:%d\n”,用户->用户输入);
struct foo*user2=Example2();
printf(“示例2函数用户输入2/2:%d\n”,用户2->用户输入);
printf(“示例1函数用户输入1/2:%d\n”,示例1());
printf(“示例1函数用户输入2/2:%d\n”,示例1());
}
getchar()
从缓冲区读取一个字符。缓冲区取决于函数作用域

Example1
函数中,
return
语句在调用
getchar()
一次后执行(无论读取的字符是否为换行符)。 因此,一次调用
Example1
只能读取一个字符,它可能只是一行的一部分。如果
Example1
仅读取行的一部分,则后续调用
getchar()
(或其他stdin读取函数)将读取行的左侧部分

另一方面,在
Example2
函数中,循环中没有
return
语句,在读取换行符之前执行
getchar()
。(即使达到EOF!)因此,
Example2
将通过一次调用一直读取到一行的末尾。

getchar()
从缓冲区读取一个字符。缓冲区取决于函数作用域

Example1
函数中,
return
语句在调用
getchar()
一次后执行(无论读取的字符是否为换行符)。 因此,一次调用
Example1
只能读取一个字符,它可能只是一行的一部分。如果
Example1
仅读取行的一部分,则后续调用
getchar()
(或其他stdin读取函数)将读取行的左侧部分


另一方面,在
Example2
函数中,循环中没有
return
语句,在读取换行符之前执行
getchar()
。(即使达到EOF!)因此,
Example2
将通过一次调用一直读取到一行的末尾。

在第一个函数中,您没有循环,因为您在它的主体中返回,所以最多只能读取一个字符。如果,它将减少为一个


这些函数在任何方面都不是等价的,它们之间的差异与作用域或使用
struct foo
无关。此外,标准输入流的状态(由
getchar()
)对程序来说是全局的。

在第一个函数中,您没有循环,因为您在它的主体中返回
,因此最多只能读取一个字符。如果
,它将减少为一个


这些函数在任何方面都不是等价的,它们之间的差异与作用域或使用
struct foo
无关。此外,标准输入流的状态(由
getchar()
)对程序来说是全局的。

顺便问一下,为什么要打印
user->user\u input
两次而不是使用
user2
?顺便问一下,为什么要打印
user->user\u input
两次而不是使用
user2
?好的,这很有道理。如果像这样使用while循环,它是否真的会在编译级别优化为If?好的,这很有意义。如果像这样使用while循环,它真的会在编译级别优化为If吗?