Examples -- Characters

An example...

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

/********************************************* ** File: alpha.c ** Author: Chang ** Date: ? ** Modified by: Sue Evans ** Modification date: 10/02 ** Section: Any ** EMail: bogar@cs.umbc.edu ** ** print out a run of characters. **********************************************/ #include <stdio.h> int main ( ) { char c ; /* print the alphabet in upper case */ printf("Upper case:\n ") ; for (c = 'A' ; c <= 'Z'; c++) { printf("%c", c) ; } printf("\n\n") ; /* print the alphabet in lowercase */ printf("Lower case:\n ") ; for (c = 'a' ; c <= 'z'; c++) { printf("%c", c) ; } printf("\n\n") ; /* print 0 - 9 */ printf("Digits:\n ") ; for (c = '0' ; c <= '9'; c++) { printf("%c", c) ; } printf("\n\n") ; return 0; }

Here's the output...

An example...

We can list the entire ASCII character set using the following program.
/********************************************* ** File: ascii.c ** Author: Chang ** Modified by: Sue Evans ** Date: 9/05 ** Section: 101 ** EMail: bogar@cs.umbc.edu ** ** print out the ascii characters. **********************************************/ #include <stdio.h> #include <ctype.h> int main ( ) { char c ; printf("An ASCII table:\n\n") ; for(c = 0; c <= 127; c++) { if(isprint(c)) { /* printable characters - print the value & its character */ printf("%3d='%c' ", c, c) ; } else { /* unprintable characters - print the value & some spaces*/ printf("%3d= ", c) ; } /* print newline every 8 characters */ if (c % 8 == 7) { printf("\n") ; } } /* the last newline character */ printf ("\n"); return 0; }

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 ** Author: Chang ** Modified by: Sue Evans ** Date: 9/05 ** Section: 101 ** EMail: bogar@cs.umbc.edu ** ** Try some special ascii characters. **********************************************/ #include <stdio.h> int 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) ; return 0; }

Sample run...

Last Modified -