CST8204: Lab Exercise 2

Print formats

You are going to write a small program to explore the various print formats, and to test the stdout and stderr streams using some of the features of the bash shell. From the Lab Prep document, try each of the format conversions (all those %-thingies) listed there at least once in your program.

There's no need to make your program fancy unless you wish to. A simple set of variable definitions and initializations at the top, followed by a list of various printf()s is all you really need.

However, be sure to try printf()'s cousin functions, fprintf() and sprintf():

sprintf(name-of-a-big-enough-string, printf-format, args, ...)

fprintf(name-of-an-output-stream, printf-format, args, ...)

sprintf() is a handy way to construct a string at run time, while fprintf() lets you write to a disk file or whatever (we're just going to use stdout and stderr).

Here are some short samples you can develop further as you need them:

/* print a decimal value from a signed int */

printf("value of i is %d\n", i);

/* print a decimal value from an unsigned long to stderr*/

fprintf(stderr, "value of x is %lu\n", x);

/* print unsigned int, octal, and hex permissions; use 420 */
printf(" file permissions %u decimal, %04o octal, and 0x%04X\n",

fp, fp, fp);

/* print several values to a string, then print that to stdout */
sprintf(line, "string is *%-20s* for <%10s>, first char is %c",
text, text, text[0]);

fprintf(stdout, "sprintf output is \"%s\"\n", line);

/* print a string and its address */
printf("value of text is \"%s\" at address %p\n",

text, (void *) text);

As you will recall, you can separate the output to stdout and stderr by using the shell's redirection features. For example, this will put the normal stdout output to file1 and the error output to stderr into file2 (any names will work):

system prompt$ touch x
system prompt$ ls -l x y
ls: y: No such file or directory
-rw-r--r-- 1 allisor allisor 0 Jan 10 13:33 x
system prompt$ ls -l x y > file1 2> file2
system prompt$ cat file1
-rw-r--r-- 1 allisor allisor 0 Jan 10 13:33 x
system prompt$ cat file2
ls: y: No such file or directory

system prompt$

You can read input from the keyboard by using fgets() if you have time, but DO NOT use gets() or any form of scanf(). gets() is just plain evil and must never be used, while scanf() is merely difficult and awkward (and prone to misuse and errors). If you do decide to try some input, convert numbers by using strtol() and its cousins (they require #include <stdlib.h>, and you would also need #include <string.h> for the use of strlen() or any other string function), perhaps something like this:

char buffer[100];
char *err_ptr;
int abc;
...
fputs("enter a number: ", stdout); /* write prompt */
fflush(stdout); /* make sure output is written */
if (fgets(buffer, sizeof buffer, stdin) != NULL)
{
buffer[strlen(buffer) - 1] = '\0'; /* take off \n */
abc = strtol(buffer, &err_ptr, 10); /* convert number */
if (err_ptr[0] != '\0') /* last char must be string end */
printf("invalid number in \"%s\", ignored\n", buffer);
else
printf("input is \"%s\", number is %d\n", buffer, abc);
}
else /* end-of-file is signalled by control-D */

puts("end-of-file encountered");

Before the end of today's lab, demo your program(s) showing various forms of printf() family function output to stdout and stderr. If you have also tried stdin, demo that too.



Be sure that you have completed a sign-in sheet for recording your weekly attendance and lab marks if you didn't last week, and sign in for Week 2.