CSC 161 Grinnell College Spring, 2009
 
Imperative Problem Solving and Data Structures
 

Characters and Strings in C

Readings

Please read the following materials carefully:

References

This lab assumes that you have access to a C programming language reference, particularly for the C string function library. If you do not have a published reference in paper form, you might consider the following on-line reference:

Additional Notes

The following example shows that char is considered to be a type of integer, and integer arithmetic may be performed on their values.

    #include <stdio.h>

    int main() {
       char ch;
       for (ch = 'A'; ch < 127; ch++) {
         printf("Character: %c", ch);
         printf("\t ASCII: %d \n", ch);
       }

       return 0;
    }

The following example illustrates that some characters represent actions rather than just printing a symbol.

    #include <stdio.h>

    int main() {
        char ringBell = '\a';
        char tab = '\t';
        char backspace = '\b';
        char ch;
        
        printf("Now hit the Enter key. \n");
        ch = getchar();   /* wait for the user to enter something */
        printf("Beep %c \n", ringBell);
        
        printf("Now hit the Enter key. \n");
        ch = getchar();   /* wait for the user to enter something */
        printf("These %c words %c have %c tabs %c between %c them.\n",
               tab, tab, tab, tab, tab);
        
        printf("Now hit the Enter key. \n");
        ch = getchar();   /* wait for the user to enter something */
        printf("Can you read the word: hockey? %c%c%c%c%c%c%c%c         \n",
               backspace, backspace, backspace, backspace, 
               backspace, backspace, backspace, backspace );
    
        return 0;
    }


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/161.sp09/readings/reading-strings-c.shtml

created 10 April 2008
last revised 18 January 2009
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.