CS 158A - Input, Output and Arrays in 'C'


Input

'scanf' is probably the best known function in 'C' to input data. It follows the same format as printf(used to output). It is defined in stdio.h

Say, you would like to input an integer and a float:


Output

We have already talked about output using printf. Below, we will discuss how to use printf to result in custom and formatted output.

Say we have integers 101000 and 10 and would like the following output:

101000
    10

We would do:

Say we have floats 1.2 and 1.23 and we would like the following output:

1.20

1.23

We would do:

You can type 'man printf ' at the unix prompt for more info.


Arrays

Say you would like to input 100 integers... certainly it wouldnt be clever to declare 100 variables!

In such cases we use arrays:

int my_array[100]; is a declaration for an array. This gives us the following variables:

my_array[0], my_array[1], my_array[2] ................. my_array[99]. Keep in mind 'C' counts starting from zero, and therefore the last array, in this case my_array[100] is not usable.

Below is an example which inputs 10 integers using arrays:

#include <stdio.h>

int main(void)

{

}

-END-