Skip to main content
Engineering LibreTexts

6.1: Introduction

  • Page ID
    35825
  • Up to now we haven’t talked much about character strings, that is, variables that contain non-numeric data such as a person’s name or address. There is no string variable type in C (unlike Python). In C, strings are nothing more than arrays of characters. Arrays are a simple grouping of like variables in a sequence, each with the same name and accessed via an index number. They behave similarly to arrays in most other languages (or lists in Python). C arrays may have one, two, or more dimensions. Here are a few example declarations:

    float results[10];    // An array of 10 floats
    long int x[20];       // An array of 20 longs, or 80 bytes
    char y[10][15];       // A two-dimension array, 10 by 15 chars each, 150 bytes total
    

    Note the use of square brackets and the use of multiple sets of square brackets for higher dimension 6 arrays. Also, C arrays are counted from index 0 rather than 11. For example, the first item of results[] is results[0]. The last item is results[9]. There is no such item here as results[10]. That would constitute an illegal access. You can pre-initialize arrays by listing values within braces, each separated by a comma:

    double a[5] = {1.0, 2.0, 4.7, -177.0, 6.3e4};

    If you leave the index out, you get as many elements as you initialize:

    short int b[] = {1, 5, 6, 7}; /* four elements */

    If you declare more than you initialize, the remainder are set to zero if the array is global or static (but not if auto).

    short int c[10] = {1, 3, 20}; /* remaining 7 are set to 0 */

    If you are dealing with character strings you could enter the ASCII codes for the characters, but this would be tedious in most cases. C let’s you specify the character in single quotes and it will do the translation:

    char m[20] = {‘m’, ‘y’, ‘ ’, ‘d’, ‘o’, ‘g’, 0};

    (The reason for the trailing 0 will be evident in a moment.) Even easier is to specify the string within double quotes:

    char n[20]={“Bill the cat”};

    Consider the string n[] above. It contains 12 characters but was declared to hold 20. Why do this? Well, you may need to copy some other string into this variable at a future time. By declaring a larger value, the variable can hold a larger string later. At this point you might be wondering how the C library functions “know” to use just a portion of the allocated space at any given time (in this case, just the first 12 characters). This is possible through a simple rule: All strings must be null terminated. That is, the character after the final character in use must be null, numerically 0. In the case of the direct string initialization of n[], the null is automatically added after the final character, t. In the case of the character-by-character initialization of m[], we had to do it manually. The null is extremely important. Functions that manipulate or print strings look for the trailing null in order to determine when their work is done. Without a null termination, the functions will just churn through memory until they eventually hit a null, which may cause system violations and even application or operating system crashes. Note that

    char my_pet[] = {“fido”};

    actually declares five characters, not four (four letters plus the terminating null). As C counts from index 0, this declaration is equivalent to:

    my_pet[0]= ‘f’;
    my_pet[1]= ‘i’;
    my_pet[2]= ‘d’;
    my_pet[3]= ‘o’;
    my_pet[4]= 0;

    The trailing null may also be written as ‘\0’. It is important to note that without the backslash, this has an entirely different meaning! ‘\0’ means null, but ‘0’ means the numeral 0.

     


    1. The reason for this will be apparent when we cover addresses and pointers.

    • Was this article helpful?