[C] Array Input/Output

  • int array
#include <stdio.h>

int main(void)
{
    int bus[5];
    int i, n;
    n = sizeof(bus) / sizeof(int);

    puts("-----------------------");
    puts(" ** Int Array i/o ** ");
    puts("-----------------------");

    for(i = 0; i < n; i++)
    {
        printf(" index [%d] bus No. : ", i);
        scanf("%d", &bus[i]);
    }

    printf(" \n * There are [%d] bus * \n", n);
    puts("-----------------------");
    for (i = 0; i < n; i++)
    {
        printf(" bus[%d] = No. %d \n", i, bus[i]);
    }
    puts("-----------------------");

    return 0;

    /*
    * There are [5] bus *
    -----------------------
    bus[0] = No. 111
    bus[1] = No. 22
    bus[2] = No. 333
    bus[3] = No. 444
    bus[4] = No. 555
    -----------------------
    */
}
  • String array
#include <stdio.h>

int main(void)
{
    char city[5][20];
    int i, n;
    n = sizeof(city) / sizeof(char[20]);

    puts("-----------------------------");
    puts(" ** String Array ** ");
    puts("-----------------------------");

    for (i = 0; i < n; i++)
    {
        printf(" [%d] city name : ", i);
        scanf("%s", city[i]);
    }

    printf(" \n * [%d] cities name * \n", n);
    puts("-----------------------------");
    for (i = 0; i < n; i++)
        printf(" city[%d] = %s \n", i, city[i]);

    puts("-----------------------------");

    return 0;

    /*
    -----------------------------
    ** String Array **
    -----------------------------
    [0] city name : Toronto
    [1] city name : Montreal
    [2] city name : Vancouver
    [3] city name : Ottawa
    [4] city name : Edmonton

    * [5] cities name *
    -----------------------------
    city[0] = Toronto
    city[1] = Montreal
    city[2] = Vancouver
    city[3] = Ottawa
    city[4] = Edmonton
    -----------------------------
    */
}

Reference

Tags:

Categories:

Updated: