CS31 Weekly Lab: Week 9

command line args, 2-D arrays, file I/O, gdb and valgrind

Week 9 lab topics:

  1. command line arguments in C and atoi function
  2. dynamically allocated 2-D arrays in C
  3. file I/O in C
  4. a reminder about using gdb and valgrind for debugging C programs
  5. FYI: some information on using ascii escape codes to change the color of text printed to the terminal

Create a week09 subdirectory in your weeklylab subdirectory and copy over some files:
    cd cs31/weeklylab		
    pwd
    mkdir week09
    ls
    cd week09
    pwd
    cp ~newhall/public/cs31/week09/* .
    ls
     Makefile          commandlineargs.c  students.txt
     color_printing.c  fileio.c           twoDarray.c

command line arguments and atoi
Let's look at an example C program that takes command line arguments. Open commandlineargs.c in an editor:
vim commandlineargs.c
The first thing to note is the change in main's definition:
int main(int argc, char *argv[]) { ...
The first parameter to main, argc, is the number of command line arguments. For example, if the user enters:
./a.out 10 11 200
argc will be 4 (a.out counts as one of the command line arguments, and 10, 11, and 200 as three more).

Each command line argument is passed to main in the second parameter, argv, as a string (argv is an array of strings):

        -----
argv[0]:| *-|-----> "./a.out"
        -----
argv[1]:| *-|-----> "10"
        -----
argv[2]:| *-|-----> "11"
        -----
argv[3]:| *-|-----> "200"
        -----
argv[4]:| *-|-----|     (NULL pointer: no more command line strings)
        -----
C has functions that can convert strings of numeric characters to their int, float, and other basic types, values.
int x = atoi(argv[1]);  // x gets the int value 10
See the man page for atoi for more information (man atoi).

Let's try compiling and running this program with different command line arguments and see what happens.

Dynamically Allocated 2-D Arrays in C
We are going to compare variable declaration, memory layout, and access syntax for a statically declared 2D array and one way to dynamically allocating a 2D array ( Method 1: the memory efficient way).

More information about arrays in C, including dynamically allocated 2D arrays: Arrays in C (we will use Method 1 for dynamically allocated 2D arrays).

In the file twoDarray.c is an example of a statically declared 2D array and a dynamically allocated (via Method 1) 2D array. Look at how the code accesses individual bucket values. Compile and run and see how the buckets are layed out in memory.

file I/O in C
We are going to look at an example of file I/O in C.

First lets look at some documentation about file I/O in C: File I/O in C

Next, open the fileio.c and let's see what this code is doing. Then try running it with the students.txt file as a command line argument.

./fileio students.txt

A reminder about gdb and valgrind for C program debugging
We are back to C programming this week, with pointers and functions. You are all experts at using gdb to debug at the assembly level, but remember that you also know how to use gdb (and valgrind) to debug programs at the C code level.

In lab on week 5 we looked at some examples of using both gdb and valgrind. Take a look at the the week 5 weekly lab page and the examples we talked about as a reminder of how to use these debugging tools.

Also, here are links to my guides for gdb, ddd, and valgrind:



Printing in color with ascii escape codes
In the file color_print, is an example of how to print text in colors and with diffent attributes (like bold, underscore, or please don't blink) using ascii escape sequence to specify features. The general form of an escape sequence is the following (any missing component is assumed to be zero):
\e[attribute code;text color code;background color codem
The values for these are:
Attribute codes:
----------------
0=none 1=bold 4=underscore 5=blink 7=reverse 8=concealed
Text color codes:
-----------------
30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
Background color codes:
----------------------
40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
The effect is ended by specifying:
\e[0m
For example, to print out the string Hello in red:
printf("\e[0;31mHello\e[0m");
For example, to print out the string Hello in bold blue:
printf("\e[1;34mHello\e[0m");
The color_printing.c file contains some examples. Try running to see what is printed.