home *** CD-ROM | disk | FTP | other *** search
- /*
- * P R I N T S T R
- *
- * Compare the printing of strings using several
- * methods, including indirect access via a pointer.
- */
-
- #include <stdio.h>
- #include <string.h>
-
- int
- main(void)
- {
- int i; /* loop index */
- int len; /* string length */
- char *cp; /* character pointer */
-
- static char string[] = {
- "What goes up must come down. Ask a market analyst!\n"
- };
-
- /*
- * Print the string by using the printf() library function.
- */
- printf("Using printf(): ");
- printf("%s", string);
-
- /*
- * Print the string by using array indexing.
- */
- printf("Using an array index: ");
- len = strlen(string); /* length of string */
- for (i = 0; i < len; ++i)
- putchar(string[i]);
-
- /*
- * Print the string by using a character pointer.
- */
- printf("Using a character pointer: ");
- cp = string; /* beginning of string */
- while (*cp) {
- putchar(*cp);
- ++cp;
- }
-
- return (0);
- }
-