Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C getchar函数获取的打印字符串_C - Fatal编程技术网

C getchar函数获取的打印字符串

C getchar函数获取的打印字符串,c,C,我在GNU编译器上测试一个简单的getchar()函数,但为什么不得到预期的输出呢 mto@ubuntu:~/c$ ./a.out agc mto@ubuntu:~/c$ 代码如下: #include <string.h> #include "stdio.h" #define MAX 80 int main() { char ch, buffer[MAX+1]; int x=0 ; while ((ch =getchar()

我在GNU编译器上测试一个简单的getchar()函数,但为什么不得到预期的输出呢

mto@ubuntu:~/c$ ./a.out 
agc

mto@ubuntu:~/c$
代码如下:

#include <string.h>
#include "stdio.h"
#define MAX 80
int main()
  {
        char  ch, buffer[MAX+1];
        int x=0 ;
        while ((ch =getchar() != '\n') && x<MAX)
                buffer[x++]= ch;
         buffer[x]= '\0';

 int len = strlen(buffer);

for (int c=0; c< len; c++)
       printf("%c" , buffer[c]);
       return 0;

}
#包括
#包括“stdio.h”
#定义最大值80
int main()
{
字符通道,缓冲区[MAX+1];
int x=0;

while((ch=getchar()!='\n')&&x
while((ch=getchar()!='\n')&&x
while((ch=getchar()!='\n')&&x请在要求人们查看代码之前对代码进行一致的格式化。此
(ch=getchar()!='\n')
应该是
(ch=getchar())!='\n'
。您将比较结果分配给
ch
,但您希望分配字符。请在要求他人查看代码之前对代码进行一致的格式设置。此
(ch=getchar()!='\n')
应为
(ch=getchar())!='\n'
。您可以将比较结果分配给
ch
,但要分配字符。
while ((ch =getchar() != '\n') && x<MAX)
while (((ch = getchar()) != '\n') && x<MAX)