Tuesday, May 20, 2008

Character Arrays

Lesson 4.4

Characters and Character Arrays

Storing characters is slightly different from storing numbers. You may have notice through out this lesson; I have hardly shown any character arrays. But you have already used it in the first lesson of LCD output. Characters in C are individual letters, numbers and special symbols. These characters are stored using 1 byte or 8 bits using the ASCII character value. So when I store the letter ‘A’ in a variable, C stores the ASCII code value of ‘A’, which is integer 65. So, it would be perfectly valid for me to do this in C:-

result = ‘A’ + 32;

Result will now be 97 decimal, which is ‘a’ ASCII. In C individual characters are stored like this:-

char MenuOption[4] = {‘A’, ‘E’, ‘D’, ‘Q’};

Notice a single quotation mark ‘ is used.

When a collection of characters are stored in C, it is called a string and math operation like the example above cannot be preformed.

char callsign[6] = “9w2gu”;

This means, declare variable callsign of 6 elements long and initialize the variable with 9w2gu.

9w2gu is only 5 characters why did I specify 6 elements? The compiler need to know when the string terminates, it does this by adding a ‘\0’, a NUL character, at the end of the string. Also a string declaration begins and ends with double quotation mark “, and string do not require the { } for single dimensional array. So take care when declaring strings.

To declare and initialize string variables 1D character array.

char callsign[] = “9W2DTR”; // let compiler find the size

char handle[7] = ”Dexter”; // max char handle is 6 1 for ’\0’ NUL

To display the content of the string, just use the variable name.


Using a for loop will display each character at a time. Ex.




This is where the character array is different from numerical arrays.

if char is declared as below,

char callsign[6];

callsign = “9w2dtr”

Will only print 9w2dt or undesired results, remember you need to specify space for NUL character too.

Below a visual sample of 1D character array.

Now for some explanation on 2 and 3 dimensional character array.

They work similar to their numerical partners except the last element defines the width of the string field. Ex.

char dow[7][4] = {“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”);

This means, create an array of 7 rows, 4 characters each.

Below the visual display of the above declared 2D array.

To display Friday I would need to specify the row element only. Example:


Will display Fri on the lcd.

Similarly, 3D character arrays work the same way.

3D character array are declared as shown below.

char students[3][2][7] =

{“9w2bpy”, “Yau”,

“9w2dtr”, “Dexter”,

“9m2cf”, “Chow”};

Which will look like this.

This means declare a character array called students 3 rows 2 columns which can hold 7 characters each. Always remember to allocate space for the NUL character. If I did this:-

char students[3][2][7] =

{“9w2bpy”, “Yau”,

“9w2dtr”, “Dexter”,

9m2cf”, “Uncle Chow”};

The complier will generate an error, because “Uncle Chow exceeds 7 characters.

Alternatively I could do this :-

char students[3][2][] =

{“9w2bpy”, “Yau”,

“9w2dtr”, “Dexter”,

“9m2cf”, “Uncle Chow”};

Let the compiler check for the max length of string.

lcd_out(1,1, students[1][0]); // will output 9w2dtr.

lcd_out(1,1,students[0][1]); // will output Yau.

So, important thing to remember is that character arrays is :-

Remember to allocate 1 extra character for NUL.

The last [] specifies number of characters.

All arrays, be it character or numerical, elements number (the numbers between []), can only be unsigned integer (no negative and no decimal point values).


Anonymous said...

just a question - does this board need any external devices in order to flash the program into it?
looking at futurlec site i see: "Programs can be downloaded or updated directly to the microcontroller using the USB in-circuit programming interface" - so it looks like the board has a built-in in-circuit programmer (like the development boards sold by mikroe)

Unknown said...

Yes, you need external programmer unless you're using USB bootloader...