Lab 2 Preparation

You can add an alias (defined in man bash - do a /aliases to find a description, and /alias \[-p\] for the command format itself) to your .bashrc file to make compilation easier. First, make a backup of .bashrc:

cp .bashrc bashrc.bak

I typically leave the leading dot off the backup, but it's not necessary. Now use vi to add this line after the comment "# User specific aliases and functions":

alias cc='gcc -ansi -pedantic -Wall -Wextra -O2'

I've chosen the name cc, since it's often used as the default C compiler invocation on many systems; you can choose something else if you wish.

Use it like this to compile aaa.c and bbb.c into the xxx executable (or whatever names you like):

cc -o xxx aaa.c bbb.c

Note that you can add any gcc options after cc; here's the same example with -g to add debugging information to the executable:

cc -g -o xxx aaa.c bbb.c



In this lab, you are going to be looking at some of the formats for the printf() family of output statements. You should read over as much of K&R2 Section 7.2 (pages 153 to 155 as you can (the full specification is in Appendix B, Section B1.2 on pages 243 and 244, and the very top of page 245).

We will mostly use the conversions %d (or %i) for decimal integers and %s for strings, plus on occasion %o for octal (like file permissions), %x and %X for hex output, %u for unsigned integers, and %p for void pointers. We'll also use %ld and %lu (that's an ell there, not a one) for long and unsigned long, and may use %10d to force a 10 character field for a decimal number, %#X to put a "0X" prefix on a hex number, and perhaps %-30s for a left-justified 30-character wide string field.

As you saw last week, the function main() is defined as:

int main( int argc, char *argv[] )

It can also, if you're not going to use command-line arguments, be defined as:

int main( void )

In both cases, a return statement is needed to pass back a termination status to the shell.