Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:58:13+00:00 2026-06-14T22:58:13+00:00

I would like to writing a small C program that runs an infinite loop

  • 0

I would like to writing a small C program that runs an infinite loop until the user presses a key on the keyboard (ie: there is a char in the stdin buffer). I am running into trouble breaking the loop on user input. I have tried using fgetc but that does not behave as expected. The code below waits for user input rather then running until user input.

Sample C Code:

while((c=fgetc(stdin) == EOF) {
  /* Does stuff for infinite loop here */
  printf("Example work in the loop\n");
}
printf("Out of the loop!\n");

How do I write a loop that executes until user intervention? Pressing any key or a specific key could be the intervention trigger.

Note 1: I am writing this for a Unix console in case of platform specific solutions

Note 2: Do not suggest Ctrl + C/X/Z as the user intervention trigger

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:58:15+00:00Added an answer on June 14, 2026 at 10:58 pm

    This seems to work for me:

    #include <fcntl.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/uio.h>
    #include <unistd.h>
    
    static void set_non_blocking(int fd)
    {
        int flags  = fcntl(fd, F_GETFL, 0 );
        flags |= O_NONBLOCK;
        flags = fcntl(fd, F_SETFL, flags);
    }
    
    
    int main(int argc, char ** argv)
    {
        int fd = fileno(stdin);
        char buf[10];
    
        set_non_blocking(fd);
    
        while (read(fd, buf, sizeof buf) < 0) {
            perror("read");
            sleep(1);
        }
        return 0;
    }
    

    or you could use select:

    int main(int argc, char ** argv)
    {
        int fd = fileno(stdin);
        struct timeval tv = {0,0};
        fd_set fdset;
        int s;
    
        do {
            sleep(1);
            FD_ZERO(&fdset);
            FD_SET(fd, &fdset);
    
        } while ((s = select(fd+1, &fdset, NULL, NULL, &tv)) == 0);
    
        if (s < 0) {
            perror("select");
        }
        return 0;
    }
    

    Poll works too 🙂

    int main(int argc, char ** argv)
    {
        struct pollfd pfd;
        int s;
    
        pfd.fd = fileno(stdin);
        pfd.events = POLLRDNORM;
    
        while ((s = poll(&pfd, 1, 0)) == 0) {
            perror("polling");
            sleep(1);
        }
        if (s < 0) {
            perror("poll");
        }
        return 0;
    }
    

    One last way is to set the terminal in ‘raw’ mode. Note that this upsets output to the terminal (at least on mine on OS-X) in that \r becomes necessary after \n. Note also that it needs to be undone at the end (the terminating tcsetattr call). This is the only one that does not need a \n (ie any keypress will do)

    #include <poll.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <termios.h>
    
    
    static void set_non_blocking(int fd)
    {
        int flags = fcntl(fd, F_GETFL, 0) | O_NONBLOCK;
    
        if (fcntl(fd, F_SETFL, flags) < 0) {
            perror("fcntl");
            exit(EXIT_FAILURE);
        }
    }
    
    
    int main(int argc, char ** argv)
    {
        struct termios params;
        struct termios params_orig;
        char buf[10];
        int fd = fileno(stdin);
    
        if (tcgetattr(fd, &params) < 0) {
            perror("tcgetattr");
            exit(EXIT_FAILURE);
        }
        params_orig = params;
    
        cfmakeraw(&params);
    
        if (tcsetattr(fd, TCSANOW, &params) < 0) {
            perror("tcsetattr");
            exit(EXIT_FAILURE);
        }
        set_non_blocking(fd);
    
        while (read(fd, buf, sizeof buf) < 0) {
            perror("\rread");
            sleep(1);
        }
    
        (void) tcsetattr(fd, TCSANOW, &params_orig);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small program with an interpreter, I would like to pipe any
I am writing a small wxPython utility. I would like to use some event
I would like seek some guidance in writing a process profiler which runs in
I'm writing a blackjack program and would like to be able to write the
I'm writing a small program in C that will read input from the console.
I'm just a beginner in C++. I'm writing small and simple program that prints
In a program I am writing, I would like to develop a hashing algorithm
I'm writing a small C++ program that tests C dlls, containing some functions. Those
I am writing boot code where I would like to use only relative addressing
I am writing a C++ application and would like to request several data files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.