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

  • Home
  • SEARCH
  • 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 6877183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:34:10+00:00 2026-05-27T04:34:10+00:00

Just learned about sigacation, also implemented another timer to make it more interesting. #include

  • 0

Just learned about sigacation, also implemented another timer to make it more interesting.

#include <stdio.h>
#include <signal.h> 
#include <sys/time.h>

volatile sig_atomic_t gotsignal;


void handler(){

    gotsignal = 1;

}

int main(){

struct sigaction sig;
struct itimerval timer;

timer.it_value.tv_sec = 5;
timer.it_value.tv_usec = 0;
timer.it_interval = timer.it_value;

sig.sa_handler = handler; 
sig.sa_flags = 0; 
sigemptyset(&sig.sa_mask); 

setitimer(ITIMER_REAL, &timer, 0);
sigaction(SIGALRM, &sig, NULL);


int value, y = 100, x=0;

while(!gotsignal && x < y){
    printf("Insert a value: \n");
    scanf("%d", &value);
    x+=value;
    printf("Current x value: %d\n", x);
}
}

What i don’t understand is, when it is waiting for user input and i write 5, but not press enter. He still reads it? Shouldn’t he clear it off?
The output it gave me:

Insert a value: 
1
Current x value: 1
Insert a value: 
2
Current x value: 3
Insert a value: 
5Current x value: 5

What I would want would be like:

Insert a value: 
1
Current x value: 1
Insert a value: 
2
Current x value: 3
Insert a value: 
5(Wthout pressing enter!)Current x value: 3 (He would forget about 5 no?)
  • 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-05-27T04:34:11+00:00Added an answer on May 27, 2026 at 4:34 am

    A (pedantically) correct signal handler can do very few things: notably setting a volatile sig_atomic_t variable (this is some integer type), and perhaps calling siglongjmp [I’m not even sure for siglongjmp].

    So declare first

    volatile sig_atomic_t gotsignal;
    

    then your signal handler is simply

    void handler (void)
    {
      gotsignal = 1;
    }
    

    and your loop is

    while(!gotsignal && x < y){
        printf("Insert a value: \n");
        scanf("%d", &value);
        x+=value;
    }
    

    Don’t forget that asynchronous signals happen at any time (any machine instruction!!!), including inside malloc or printf. Never call these functions from inside a handler.

    Bugs related to bad signal handling are hard to debug: they are not reproducible!

    Consider perhaps using sigaction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just learned about how to include FxCop on a build. But it's slow
I've recently started learning C#. I just learned about properties and decided to make
I just learned about arrays but can't conceptualize arrays with more than three dimensions.
I just learned about the i/o part of the STL, more specifically fstream. Although
I just learned about how the Java Collections Framework implements data structures in linked
I just learned about ngrep , a cool program that lets you easily sniff
I just posted this question and learned about <see cref=> , however when i
I've got an application that just shipped. Since I wrote it, I've learned about
I just learned something interesting. The add method for the javascript select object in
I just learned about list comprehension, which is a great fast way to get

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.