它只是創建隨機值
我嘗試為陣列的變數使用單獨的值,但我也不知道為什么它從元素 6 開始計數。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int array[3][5];
for (i = 0; i<5; i );
{
printf("Input a whole number for row 1 element %d:\n", i 1);
scanf("%d", &array[0][i]);
}
printf("Row 1 elements:\n");
for(i = 0; i<5; i )
{
printf("%d\n", array[0][i]);
}
return 0;
}
輸出:
> Input a whole number for row 1 element 6: 4 Row 1 elements: 0 0 0 0
> 1897665488
>
> Process returned 0 (0x0) execution time : 1.969 s Press any key to
> continue.
uj5u.com熱心網友回復:
它從 開始計數,6因為該行for (i = 0; i < 5; i );正在迭代(遞增)i5 次,因此i變為5,然后您列印i 1到stdout。
所以,基本上你對printf()和scanf()函式的呼叫從來都不是任何回圈的一部分。
;注意:在任何回圈之后添加分號意味著回圈沒有主體。基本上它是一個空回圈。它對于查找字串的長度等很有用。
一些技巧:
- 也可以代替使用裸機
return 0;,使用return EXIT_SUCCESS;頭檔案中定義的stdlib.h。 - 使用
int main(void) { }, 而不是int main() { } - 總是檢查
scanf()輸入是否成功
正確的代碼
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
int array[3][5];
for (i = 0; i < 5; i )
{
printf("Input a whole number for row 1 element %d:\n", i 1);
if (scanf("%d", &array[0][i]) != 1)
{
perror("bad input: only numbers are acceptable\n");
return EXIT_FAILURE;
}
}
printf("Row 1 elements:\n");
for (i = 0; i < 5; i )
{
printf("%d\n", array[0][i]);
}
return EXIT_SUCCESS;
}
輸出:
Input a whole number for row 1 element 1:
1
Input a whole number for row 1 element 2:
2
Input a whole number for row 1 element 3:
3
Input a whole number for row 1 element 4:
5
Input a whole number for row 1 element 5:
7
Row 1 elements:
1
2
3
5
7
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451794.html
