关于continue在C语言程序中的问题

zaixiankaifa zaixiankaifa 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>


int main()
{
int keyboard;
int ret,i;
char c;
fd_set readfd;
struct timeval timeout;
keyboard=open("/dev/tty",O_RDONLY | O_NONBLOCK);
assert(keyboard>0);
while(1)
{
timeout.tv_sec=1;
timeout.tv_usec=0;
FD_ZERO(keyboard,&readfd);
FD_SET(keyboard,&readfd);
ret=select(keyboard+1,&readfd,NULL,NULL,&timeout);
if(FD_ISSET(keyboard,&readfd)
{
i=read(keyboard,&c,1);
if('\n'==c)continue;
printf("the input is %c\n",c);
if('q==c')break;
}
}
return 0;
}

运行结果:
e
the input is c
w
the input is w
t
the input is t

hello
the input is h
the input is e
the input is l
the input is l
the input is o



我是在CentOS 7 执行该程序,使用select判断终端输入的时候,如果输入字母则会读取,然后通过printf输出,但是为什么当执行回车键时,这个语句块 if('\n'==c)comtinue;不是应该就中断该循环吗,为什么还会有printf输出? 不应该有输出呀, continue直接中断该循环,不会执行后续语句,难道我理解错了? 求解.......
...全文
评论 19
收藏 已收藏
分享
返回顶部
zaixiankaifa
C语言 70,040 成员 243,237 内容