My C experience

C programming lesson 1:
Insert data into array and display, using for loop

int main(void)
{     
    int i;
    int str[20];  //initial array of size 20

    printf("insert data。\n");

// enter the value to str[20]
    for (i = 0; i < 20; i++) {
        scanf_s("%d", &str[i]);
        if (str[i] == -1)
            break;
    }  
//print the array, break when -1 entered
    for (i = 0; i < 20; i++) {
        if (str[i] == -1)
            break;
        printf("%d。\n", str[i]);
    }
}

Leave a Reply