CSC161 2010F Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
-
[Assignment]
[Lab]
Groupings:
[EBoards]
[Assignments]
[Examples]
[Handouts]
[Labs]
[Outlines]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Summary:
Prerequisites: Familiarity with basic C (arrays,
main
, compiling, etc.).
a. Log in to your MathLAN workstation. (Of course, you've probably already done that if you're reading this laboratory.)
b. Open a terminal window into which you can type commands.
c. Create a directory for this lab, such as ~/CSC161/CommandLineLab/
.
d. In that directory, create the standard Makefile.
a. Write a program that prints out the number of arguments it receives.
That is, you just need to print argc
and then exit. Call
your program argc
.
b. Determine what happens for each of the following calls.
./argc
./argc 5
./argc a b c
./argc "a b c"
./argc `ls`
Feel free to ask me or the class mentor if you don't understand one of your results.
As you may have just noticed, programs always seem to have at least one argument. Let's figure out what that argument is.
a. Write a program, arg0
that prints out the value of
argv[0]
(a string).
b. Determine what output you get for each of the following.
./arg0
./arg0 5
./arg0 a b c
./arg0 "a b c"
c. Copy arg0
to argzero
using the cp
command in the shell. What do you expect to have happen when you repeat
the previous commands, substituting argzero
for arg0
?
d. Check your answer experimentally.
e. Make a soft link from arg0
to argh0
.
ln -s arg0 argh0
Then try running argh0
with a few different arguments.
Consider the following program.
#include <stdio.h> #include <string.h> /** * hi.c - A not-so-simple "Hello world!" program. */ int main (int argc, char *argv[]) { if (strcmp (argv[0], "./surprise") == 0) { printf ("*** SURPRISE ***\n"); } // if else { printf ("Hi, my name is %s.\n", argv[0]); } return 0; } // main
a. Save the file as hi.c
and compile it so that the executable
is named hi
.
b. What do you expect to have happen when you run it with the following
command? (Note the strcmp
returns 0 if given two identical
strings; you can think of the 0 as representing no difference
.)
./hi
c. Check your answer experimentally.
d. Link hi
to hello
using the following
command.
ln -s hi hello
e. What do you expect to have happen when you run the following command?
./hello
f. Check your answer experimentally.
g. Link hi
to suprise
using the following
command.
ln -s hi surprise
h. What do you expect to have happen when you run the following command?
./surprise
i. Check your answer experimentally.
a. Write a program, arg1
that prints out the element with
index 1 from the argv
array. (That element is a string.)
b. What do you expect that program to print out for each of the following commands?
./arg1 hello
./arg1 a b c
./arg1 "a b c"
./arg1 `ls`
./arg1
c. Check your answer experimentally.
a. Write a program, square
that takes one number on the command
line and prints out the square of that number.
You may find it useful to use the atoi
procedure which you
can access when you include stdlib.h
.
b. Determine what happens when you provide square
with no
arguments.
c. Determine what happens when you provide square
with something
other than a number as the first argument.
a. Write a programs, args
, that prints out all of the arguments,
with each argument preceded by the argument number.
b. Predict the output for each of the following commands.
./args
./args a b c
./args "a b c"
./args `ls`
./args a*
c. Check your predictions experimentally.
Write a program, sum
, that assumes all of its command-line
arguments are integers and computes their sum.
Your program should report an error message if it receives no command-line arguments.
q2l
, Revisited
Rewrite q2l
from the the I/O
lab so that the program checks for a value on the command line.
q2l
Re-revisited
Add error checking (including error checking for the command line) to
the revised q2l
.
Write a program, compute
, that takes as its first argument
an operation (initially, *
or +
) and as its
remaining arguments a series of numbers, and that computes the specified
operation on the numbers.
% ./compute * 3 4 5 60 % ./compute + 3 4 5 12 % ./compute * 5 5 % ./compute + 5 5
Sunday, 12 September 2010 [Samuel A. Rebelsky]
Tuesday, 14 Septembe 2010 [Samuel A. Rebelsky]
[Skip to Body]
Primary:
[Front Door]
[Schedule]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
-
[Assignment]
[Lab]
Groupings:
[EBoards]
[Assignments]
[Examples]
[Handouts]
[Labs]
[Outlines]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Sep 14 11:32:05 2010.
The source to the document was last modified on Tue Sep 14 11:32:04 2010.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/command-line-lab.html
.
A PDF version of this document may be found at
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/command-line-lab.pdf