UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

Examples -- Characters

An example...

Here's a program that uses for loops to step through the letters of the alphabet.

/* File: run.c print out a run of characters. */ #include <stdio.h> main ( ) { char c ; printf("Upper case:\n ") ; for (c = 'A' ; c <= 'Z'; c++) { printf("%c", c) ; } printf("\n\n") ; printf("Lower case:\n ") ; for (c = 'a' ; c <= 'z'; c++) { printf("%c", c) ; } printf("\n\n") ; printf("Digits:\n ") ; for (c = '0' ; c <= '9'; c++) { printf("%c", c) ; } printf("\n\n") ; }

Here's the output...

An example...

We can list the entire ASCII character set using the following program. /* File: ascii.c print out the ascii characters. */ #include <stdio.h> #include <ctype.h> main ( ) { char c ; printf("An ASCII table:\n\n") ; for (c = 0 ; c <= 127; c++) { if (isprint(c)) { /* printable characters */ printf("%3d='%c' ", c, c) ; } else { /* unprintable characters */ printf("%3d= ", c) ; } /* print newline every 8 characters */ if (c % 8 == 7) { printf("\n") ; } } }

Note that in the output, not all characters are printable.

An example...

Special characters are denoted using the backslash symbol as in... /* File: special.c Try some special ascii characters. */ #include <stdio.h> #include <ctype.h> main ( ) { char beep, backspace ; char newline, tab, backslash ; char single_quote, double_quote ; beep = '\a' ; backspace = '\b'; newline = '\n' ; tab = '\t' ; backslash = '\\' ; single_quote = '\'' ; double_quote = '"' ; printf("BEEP: %c%c", beep, newline) ; printf("%c%cMove over\n", tab, tab) ; printf("Oops, ooo%c%c%cxxx\n", backspace, backspace, backspace) ; printf("I%cve written %cHello World%c many times\n", single_quote, double_quote, double_quote) ; }

Sample run...


CSEE | 201 | 201 F'98 | lectures | news | help