08/30

An Introduction to C

Part VIII, 20120830

So far, we have already covered the use of the character data type, but what happens when you want to print more than one character to screen? This was strings are for.

A string is a series of characters terminated as with a null character.

That is, a string is an array of characters.

An array is a series of pieces of data with an identical data type. You can have arrays of all types of numerals, characters, and even arrays.

#include <stdio.h>
int main(void) {
  char name[5];
  name[0] = 'F';
  name[1] = 'o';
  name[2] = 'r';
  name[3] = 'd';
  name[4] = '\0'; // this is the null character defining the end of the string
  printf("Hello, my name is %s\n", name);
}

One very important thing to note about this is that while our declaration declared five different elements, the counting of those elements starts at zero. We count zero through four, which is five total numbers.