1. Goals for this week
-
Learn about command line arguments in C and the
atoi
function -
Learn about file I/O in C, using C’s stream interface for file I/O:
fopen
,fclose
,fscanf
-
Some reminders about dynamically allocated 2-D arrays in C, and about using
gdb
andvalgrind
for debugging C programs -
Introduction to Lab 5.
2. Starting Point Code
Start by creating a 7 directory in
your cs31/weeklylabs
subdirectory and copying over some files:
$ cd ~/cs31/weeklylabs
$ mkdir week07
$ cd week07
$ pwd
/home/you/cs31/weeklylabs/week07
$ cp ~adanner/public/cs31/week07/* ./
$ ls
3. C command line arguments and atoi
Let’s look at an example C program that takes command line arguments.
Start by opening 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. For example, atoi
converts a
string to an int:
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.
4. File I/O in C
We are going to look at an example of file I/O in C, using C’s stream
interface for file I/O (fopen
, fclose
, fscanf
, fprintf
, etc.).
First lets look at some documentation about file I/O in C: File I/O in C
Next, open the fileio.c
program 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
5. Reminder about gdb and valgrind for C program debugging
We are back to C programming this week, with pointers and functions.
In lab on Weekly Lab 5 we looked at some examples of using both gdb and valgrind. Take a look at the Weekly Lab 5 weekly lab page and the examples we talked about as a reminder of how to use these debugging tools.
6. Dynamically Allocated 2-D Arrays in C
We are not going to look at this program together in lab today, but
the twoDarray.c
program contains examples of C statically
declared 2D arrays and dynamically allocated 2D arrays (using the single
malloc).
As you work on the Lab 5 assignment, you can refer to this example program dynamically allocate and accesses a 2D array.
7. Handy References
-
Textbook. Chapter 2: I/O, 2D arrays, command line arguments in C. Chapter 3: C debugging tools (valgrind, gdb for C, gdb for assembly
-
Some C Programming Resources and Links including the C Style Guide, File I/O in C, cmdline args, and arrays