﻿ C homework – 编译代码时发现的木马 - 代码日志

#### C homework – 编译代码时发现的木马

``````#include <stdio.h>

int main ( void ) {
int numbers [10];
int i, temp;

for (i = 1; i <= 10; ++i)
numbers[i] = 0;

printf("Enter up to 10 integers. input '-1' to finish \n");

for (i = 0; i < 10; i++) {
scanf("%d", &temp);
if (temp == -1) {
break;
} else {
numbers [i] = temp - 1;
}
}

for (i = 1; i <= 10; ++i)
printf("the numbers are: %d\n", numbers[i]);

return 0;
}
``````

``````    for (i = 1; i <= 10; ++i)
numbers[i] = 0;
``````

> for(i = 1; i< = 10; i)是不正确的,因为C中的数组从0开始,第二个初始化变量你不需要为循环执行,你可以像任何其他变量一样为它们赋值.
> numbers [i] = temp – 1在数组中存储值的方式不太好,因为在执行-1时,您正在改变输入的值.

``````/*For the array initialization.*/
int numbers[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
``````
``````/*For inputing the values.*/

for ( i = 0; i < 10; i++ ){
scanf( "%d", &temp );
if( temp == -1 ){
numbers[ i ] = -1;
break;
else{
numbers[ i ] = temp;
}
}
``````
``````/*For the printing. */

for( i = 0; i < 10 ; i++ ){
if( numbers[ i ] == -1 ){
break;
}
printf( "numbers[%d] is %d", i, numbers[ i ] );
}
``````