Add a Timeout for getchar()

前端 未结 2 1288
梦如初夏
梦如初夏 2021-01-12 07:57

I need to add a timeout function for getchar() in my program.

What do I do so that when my program reaches the instruction getchar(), it will only wait for a certai

2条回答
  •  暖寄归人
    2021-01-12 08:35

    This is usually achieved by using select() on stdin. Another solution would be using alarm() and a dummy SIGALRM handler to interrupt the getchar() call (only working on POSIX systems though).

提交回复
热议问题