【问题标题】:printf scanf Execution Order is getting exchangedprintf scanf 执行顺序正在交换
【发布时间】:2014-06-12 04:31:16
【问题描述】:
printf("Enter the value\n");
scanf("%d ",&data);

但是scanf 在printf 之前被执行。可能是什么原因?

这是我的整个程序

#include<stdio.h>
#include<stdlib.h>
struct Queue{
    int front,rear;
    int capacity;
    int *array;
};

void enqueue(struct Queue *q){

    int data;
    if(q->front == (q->rear+1)%q->capacity){
        printf("Queue is full\n");
        return;
    }else{
        printf("Enter the value\n");
        scanf("%d ",&data);
        q->rear = (q->rear+1) % q->capacity;
        q->array[q->rear] =data;

        if(q->front == -1)
            q->front = q->rear;
    }

}
void dequeue(struct Queue *q){

    if( q->front == -1){
        printf("Queue is Empty\n");
        return;
    }else{
        printf("value dequeued %d \n",q->array[q->front]);
        if(q->front == q->rear)
            q->front = q->rear = -1;
        else
            q->front = (q->front+1)% q->capacity;
    }
}
void printQueue(struct Queue *q){

    if( q->front == -1){
        printf("Queue is Empty\n");
        return;
    }else{
        int f = q->front, r =q->rear;

        while((f% (q->capacity)) != r)
        {
            printf ("\t %d", q->array[f] );
            f++;
        }
    printf("\t %d",q->array[q->rear]);
    printf("\n");
    }
}


main(){

    struct Queue *q = malloc(sizeof(struct Queue));
    if(!q){
        printf("Memory Error\n");
        exit;
    }
    q->capacity = 3;
    q->front= q->rear = -1;
    q->array = malloc(q->capacity * sizeof(int));   
    if(!q->array){
        printf("Memory Error\n");
        exit;
    }

    int option;
    char ch ='a';
    while(1) {
        printf("Enter the choice 1.Enqueue or 2.Dequeue 3.print\n");
        fflush(stdout);
        scanf("%d",&option);
        switch(option){
        case 1:
            enqueue(q);                 
            break;
        case 2:
            dequeue(q);
            break;
        case 3:
            printQueue(q);
            break;
        default:
            printf("Wrong choice\n");
        }


    printf("Do you want to continue? press y to continue\n");
    scanf("%s", &ch);
    if(ch != 'y')break;
    }
}

当我将一个元素排入队列时,它会等待输入,然后执行该 printf 语句。 e 您要继续吗? 理想情况下,当我输入要排队的元素并按回车键时,它应该问我是否要继续,然后它应该等待输入,即“y”。但是,一旦我输入要入队的元素,它就会等待输入'y',然后在输入'y'后,“你想继续吗”被执行

【问题讨论】:

  • 不,不是。‍
  • 我使用的是ubuntu默认的gcc编译器
  • 为什么让你相信它正在被重新排序?
  • scanf("%s", &amp;ch); 行不对。你的意思是scanf("%c", &amp;ch);?
  • stdout 默认被缓冲。在 printf 之后(在 scanf 之前)刷新标准输出,你会看到你所期望的。这回答了你的问题。但是您的代码中还有其他问题...已在下面进行了说明...(如果您愿意,请参阅setvbuf (man setvbuf) 以关闭标准输出缓冲)

标签: c printf scanf


【解决方案1】:

重新编写了问题代码。最大的问题是使用 scanf(),它会在标准输入中留下未读值(例如回车)。未读字符会干扰下一次使用 scanf()。通过将 %*c 添加到格式字符串中,将 scanf() 更改为从标准输入中吞噬多余的字符:

#include<stdio.h>
#include<stdlib.h>
struct Queue{
    int front,rear;
    int capacity;
    int *array;
};

void enqueue(struct Queue *q){

    int data;
    if(q->front == (q->rear+1)%q->capacity){
        printf("Queue is full\n");
        return;
    }else{
        printf("Enter the value\n");
        scanf(" %d%*c",&data);
        q->rear = (q->rear+1) % q->capacity;
        q->array[q->rear] =data;

        if(q->front == -1)
            q->front = q->rear;
    }

}
void dequeue(struct Queue *q){

    if( q->front == -1){
        printf("Queue is Empty\n");
        return;
    }else{
        printf("value dequeued %d \n",q->array[q->front]);
        if(q->front == q->rear)
            q->front = q->rear = -1;
        else
            q->front = (q->front+1)% q->capacity;
    }
}
void printQueue(struct Queue *q){

    if( q->front == -1){
        printf("Queue is Empty\n");
        return;
    }else{
        int f = q->front, r =q->rear;

        while((f% (q->capacity)) != r)
        {
            printf ("\t %d", q->array[f] );
            f++;
        }
    printf("\t %d",q->array[q->rear]);
    printf("\n");
    }
}


int main()
   {
   int  option;
   char ch ='a';

   struct Queue *q = malloc(sizeof(struct Queue));
   if(!q)
      {
      printf("Memory Error\n");
      goto CLEANUP;
      }

   q->capacity = 3;
   q->front= q->rear = -1;
   q->array = malloc(q->capacity * sizeof(int));
   if(!q->array)
      {
      printf("Memory Error\n");
      goto CLEANUP;
      }

   while(1)
      {
      printf("Enter the choice 1.Enqueue or 2.Dequeue 3.print\n");
      scanf(" %d%*c",&option);
      switch(option)
         {
         case 1:
            enqueue(q);
            break;

         case 2:
            dequeue(q);
            break;

         case 3:
            printQueue(q);
            break;

         default:
            printf("Wrong choice\n");
            break;
         }


   printf("Do you want to continue? press y to continue\n");
   scanf(" %c%*c", &ch);
   if(ch != 'y')
      break;
   }

CLEANUP:

   return(0);
   }

【讨论】:

  • 非常感谢。实际上,当我在 %d 之后删除空格时,代码运行良好
  • 我把 scanf("%d ",&data) 改成了("%d",&data)
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 2020-08-13
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多