在c中运行多个命令

在c中运行多个命令,c,C,我想创建一个程序。有人,必须找到隐藏的数字,但我也想放一个计时器,但我不知道如何使这些同时运行。这是我的密码 enter code here #include <stdio.h> #include <time.h> #include <stdlib.h> #include <unistd.h> void timer(); int main(void) { timer(); return 0; }void timer(){

我想创建一个程序。有人,必须找到隐藏的数字,但我也想放一个计时器,但我不知道如何使这些同时运行。这是我的密码

enter code here







#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
void timer();
int main(void) {

  timer();
  return 0;
}void timer(){
  int i=0;
  int b= 61;
  for( i=0;i<b;i++){
    sleep(1);

  printf("\n%d",i);
  }
   int hidden,x;
  srand(time(NULL));
  hidden=1+rand()%100;
  do{
    printf("\nGuess the number\n");
    scanf("%d",&x);
    if(x==hidden)
    printf("\nYou found the hidden number");
    else if(x>hidden)
    printf("\nPut a smaller number");
    else
    printf("\nPut a larger number");
  }while(x!=hidden);

}
在此处输入代码
#包括
#包括
#包括
#包括
无效计时器();
内部主(空){
定时器();
返回0;
}无效计时器(){
int i=0;
int b=61;
对于(i=0;i隐藏)
printf(“\n输入一个较小的数字”);
其他的
printf(“\n输入一个较大的数字”);
}while(x!=隐藏);
}

您可以使用C信号功能C报警功能。如本文所述

您还应该找到scanf的替代方案,因为它可能导致缓冲区溢出。您可以使用FGET

代码:

#包括
#包括
#包括
#包括
#包括
无效终止(int信号)
{
出口(0);
}
int main()
{
int snum=0,num=0;
srand(时间(空));
snum=rand()%10+1;
puts(“尝试在10秒内猜出数字!”);
信号(信号,终止);
警报(10);
scanf(“%d”和&num);
while(num!=snum)
{
放(“你猜错了!”);
scanf(“%d”和&num);
}
“恭喜你!”;
返回0;
}

这是否回答了您的问题?这是另一个:。如果搜索“C中的超时”,则以此类推。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

void terminate(int signal)
{
  exit(0);  
}

int main()
{
  int snum=0,num=0;
  srand(time(NULL));
  snum=rand()%10+1;
  puts("Try to guess the number within 10 seconds!");
  signal(SIGALRM,terminate);
  alarm(10);
  scanf("%d",&num);
  while (num!=snum)
  {
    puts("Your guess is wrong!");
    scanf("%d",&num); 
  }
  puts("Congratulations!");

  return 0;
}