簡體   English   中英

為什么 scanf 正在運行我的第二個輸入?

[英]Why is scanf running taking my second input?

為什么我的數組的 scanf 在 for 循環之前運行? Output [在此處輸入圖像描述][1]

#include <stdio.h>

void main()
{
    int num,i,arr[10];
    printf("The number you want to add with each number is:");
    
   scanf("%d \n",&num);
   // printf("%d \n",num);
    
    printf("the 10 numbers are:\n");
    for(i=0;i<=9;i++){
        scanf("%d \n",&arr[i]);
    }
    printf("The new array is:");
    for(i=0;i<=9;i++){
        arr[i] = arr[i]+ num;
        printf("%d \n",arr[i]);
    }
}

這是因為您在 scanf() 語法中使用了尾隨空格,而且您在 scanf 中使用了 \n 並且您的 scanf 不僅掃描數字 %d ,而且掃描引號(“”)之間的整個內容,這會導致意外錯誤。 所以你應該以另一種方式編寫你的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM