Homework 1
Introduction
In this homework assignment, you'll learn some of the basics of C: how to compile and run a C program, and how to write C code to examine the Unix command line and take some action based on what it finds.
This homework assignment includes both an "exercise" and a "problem". An exercise is something for you to do, but you don't need to hand it in. You must hand in the results of doing problems, and we grade those. You hand them in via tinyvincent.
Exercise
Type or paste the following into a file called example.c. (Recommendation: Type it and think about the code as you type it in, rather than pasting it. It only takes a little longer, and it gets your mind into the details.)
#include <stdio.h> /* We include this to get the definition of printf */
#include <string.h> /* We include this to get the definition of strlen */
/*
* Here we begin our main block of code, this is where the program will begin
* its execution. "argc" is the number of arguments appearing on the
* command line (including the name of our program) and
* "argv" is the array of those strings.
*/
int main (int argc, char * argv[])
{
/* Defines variables we'll use later, remember _all_ variables must be
* defined at the beginning of a code block.
*/
int i;
/* Prints "Hello, World!" and a new line */
printf ("Hello, World!\n");
/* Prints our number of arguments */
printf ("You passed %d arguments.\n", argc);
/* Print out the program name
* (The first element of argv[] is the name of the program.) */
printf ("Your program is named '%s'.\n", argv[0]);
/* We can create a for loop */
for (i = 0; i < 5; i++)
{
printf ("i is: %d\n", i); /* prints 1 through 5 on new lines */
}
i = 4; /* sets i to 4 */
if (i == 4)
{
printf ("i is four!\n");
}
else
{
printf ("i is not four!\n");
}
/* In the exercises below we'll also use the "strlen" function. strlen
* takes a string as an argument and returns its length.
*/
i = strlen ("Hello");
printf ("Hello is %d characters long.\n", i);
/* Don’t forget to return a value before ending main(); */
return 0;
}
Compile and run example.c as follows: For this program set we'll be using the default gcc compiler. We can compile our example program above as follows:
~$ gcc example.c -o example ~$ ./example Hello, World! You passed 1 arguments. Your program is named './example'. i is: 0 i is: 1 i is: 2 i is: 3 i is: 4 i is four! Hello is 5 characters long.
Notice that you used the compiler called gcc to compile the program.
You had to type ./example rather than just example in order to tell the Unix shell where to find your program. The . means "current directory".
Problem
Write a C program, called hw1.c, that does all of the following:
1. Creates a variable named str that contains the null-terminated string "Hello, World!", and prints its content.
2. Prints 'not enough arguments' if fewer than three arguments passed are to it on the command-line.
3. If at least three arguments are on the command line:
a) prints each argument and its length, on a separate line (see below for an example).
b) Prints the total number of arguments whose first character is 'a'.
c) Prints the sum of the lengths of all the arguments.
Note that the name of the program is not a command-line argument.
Extra Credit Point: Tell me how long did this assignment take you to finish? (Not including the time spent in answering this question) Put your answer in a comment inside hw1.c.
Examples:
Assuming your program is called hw1, here are some inputs and their expected outputs:
~$ ./hw1 testing
Hello, World!
not enough arguments
~$ ./hw1 testing a homework assignment one pleasant afternoon
Hello, World!
testing 7
a 1
homework 8
assignment 10
one 3
pleasant 8
afternoon 9
number of arguments that start with 'a': 3
sum of lengths of all arguments: 46
Getting information
You'll need to know how to find the length of a string. At the shell prompt, type:
man strlen
The Unix man command can provide detailed information about most C library functions. Another good one to look at is man printf. To exit man, type q.
Some excellent sources of information about C are the The C Programming Language (2nd edition) by Kernighan and Ritchie, and C, A Reference Manual (5th edition) by Samuel P. Harbison, III, and Guy L. Steele, Jr.
What to hand in
On tinyvincent, submit your hw1.c file.
Tip: Run and test your program before you submit it.


