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():
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:
printf("value of i is %d\n", i);
fprintf(stderr, "value of x is %lu\n", x);
fp, fp, fp);
fprintf(stdout, "sprintf output is \"%s\"\n", line);
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$
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:
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.