Lesson 9: Arrays
This lesson will show you how to:
- Use string literals
- Assign strings to variables
- One dimensional arrays
Q. What's the difference between a computer and a blonde?
A. You only have to punch information once into a computer.
Arrays are a data structure that is used to store a group
of objects of the same data type in memory. All the elements
of an array must be the same data type, for example float,
char, int, a structure or function. Structures provide a
way to organize related data and will be studied in a later
lesson. Functions provide a way to define a new operation.
They are used to calculate a result or update parameters.
Functions will be covered in a later lesson. T
An array is defined with this syntax.
data type arrayName[size];
Examples:
int ID[30];
/*
Could be used to store the ID numbers of students in a
class */
float temperatures[31];
/*
Could be used to store the daily temperatures in a month
*/
char name[20];
/*
Could be used to store a character string. Character strings
in C are terminated be the null character, '\0'. This
will be discussed later in the this lesson. */
An example program using arrays:
#include <stdio.h>
int main(void)
{
char work[20];
word[0] = ‘H’;
word[1] = ‘e’;
word[2] = ‘l’;
word[3] = ‘l’;
word[4] = ‘o’;
word[5] = 0;
printf(“The contents of word[] is -? %s\n”,
word);
return 0;
}
This program prints the word "Hello" on the
screen.
back to top go
to next lesson