关于continue在C语言程序中的问题
#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直接中断该循环,不会执行后续语句,难道我理解错了? 求解.......
#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直接中断该循环,不会执行后续语句,难道我理解错了? 求解.......
...全文
相关推荐
c语言-使用continue的程序.cpp
c语言-使用continue的程序.cpp
c循环结构break与实用PPT课件.pptx
c循环结构break与实用PPT课件.pptx
工学c语言程序设计PPT学习教案.pptx
工学c语言程序设计PPT学习教案.pptx
break与continue详解[项目源码]
本文详细介绍了C语言中break和continue关键字在循环中的作用及区别。break用于永久终止当前循环,而continue则跳过本次循环的剩余代码,进入下一次循环。文章通过while、for和do while循环的具体示例,展示了break和continue的不同效果,特别是在while和do while循环中,continue可能导致死循环的情况。此外,还提供了练习题和答案,帮助读者更好地理解这两个关键字的使用场景和注意事项。
C程序设计语言.pdf
C程序设计语言.pdf


