Je sais que scanf attend l'entrée. Mais dans ce programme, j'ai écrit qu'il est en train d'imprimer bonjour dans une boucle infinie. Il n'attend pas que j'entre.Scanf n'attend pas l'entrée
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include<unistd.h>
void timer_handler (int signum)
{
static int count = 0;
printf ("timer expired %d times\n", ++count);
}
int main()
{
struct sigaction sa;
struct itimerval timer;
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sigaction (SIGALRM, &sa, NULL);
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 250000;
/* ... and every 250 msec after that. */
timer.it_interval.tv_sec = 1;
timer.it_interval.tv_usec = 250000;
/* Start a virtual timer. It counts down whenever this process is
executing. */
setitimer (ITIMER_REAL, &timer, NULL);
/* Do busy work.
*/
int i=0;
while(1){
scanf("%d",&i); //****Not waiting for input****
printf("hello");
}
}
Sortie:
timer expired 1 times hello timer expired 2 times hello timer expired 3 times hello timer expired 4 times hello timer expired 5 times hello timer expired 6 times hello timer expired 7 times
Pourquoi?
?
Utiliser 'scanf ("% d", &i);' au lieu de 'scanf ("% d", &i);' – rsp
@rsp Même résultat. –
@rsp Tous 'scanf()' 'spécificateurs, à l'exception c, n, [ 'L'espace suggéré dans' scanf ("% d", &i); 'sert peu de but – chux