Lab on Unix and C


Log into your Unix home directory (here are instructions for logging in and out). In your home directory, create another directory called csc2400 (unless you have already created it earlier). Change your working directory to csc2400 using the Unix command
     cd csc2400
In the directory csc2400, create another directory called Lab1. Change your working directory to Lab1 using the Unix command
     cd Lab1
Do the exercises below.

Exercise 1.

Step 1. Make sure you are in the directory csc2400/Lab1 of your home directory. Check this using the Unix command
     pwd
The directory listed should end in csc2400/Lab1.

Step 2. Invoke the pico editor using the command

     pico test1.c
and type in the following program. Use the menu listed at the bottom of the pico editor for help on text editting.
     #include <stdio.h>

     int main()
     {
	float x;
	int y;

	printf("\n Enter a float, then an int: \n");
	scanf("%f %d", &x, &y);
	printf("You have entered integer %d and float %f \n", y, x);	
	return 0;
     }
Save and exit your program (CTRL-O, then CTRL-X).
Here is all you need to know about printf and scanf.

Step 3. Open another terminal on tanner (follow these login steps). In this new terminal window, compile your program from Step 2 using the gcc compiler

     gcc test1.c
What files are created? Run the command
     ls -l
to list all files and associated permissions ('r' stands for read, 'w' stands for write and 'x' stands for execute). Run the executable you find.

Step 4. Compile again your program test1.c using the command:

     gcc -o test1 test1.c
What new file is created? Run that file using
     ./test1
What does the -o flag do?

Step 5. Run your executable test1 again using

     ./test1 > junk
Type in the two numbers your program expects: an integer and a float, then hit ENTER.

         What do you expect to happen?
         What happened?
          What does the operator > do in Unix?

The operator > is called redirection operator: redirect output to the file with the specified name.


Exercise 2.

Write a C program called test2.c that reads two integer values from the user and prints out their sum. Create an executable called test2 and run it.

Sample output:

     Enter the first integer value: 6
     Enter the second second integer value: 5
     The sum of 5 and 6 is 11
Here is all you need to know about printf and scanf.


Exercise 3.

C, C++ and Java allow you to input values into your program through the command line. You will only need to declare the main function in a slightly different form. In C (and C++), this is
     int main( int argc, char * argv[] )
Type the following program in a file called test3.c:
     int main( int argc, char * argv[] )
     {
          int i;

          printf("\nYour program name is %s\n", argv[0]);	
          printf("Your arguments are: \n");
          for(i = 1; i < argc; i++)
                 printf("\t \t %s starts with %c \n", argv[i], argv[i][0]);
          return 0;
      }
Create an executable called test3 and run it using the command
     test3 let us see what this does 


Exercise 4.

Use the manual pages on the Sun system to learn about the C function atoi. To learn about this function, use the command
     man atoi
You will find out that the atoi function takes a string (char * in C) as an argument, and converts it to an integer. For instance, in the C statement
     x = atoi("123")
the atoi function takes as argument the string "123" and returns the integer value 123 (one hundred and twenty three) in the int variable x.


Exercise 5.

Rewrite the program from Exercise 2 to get the two numbers from the command line. Save the program into a file called test4.c and run it using a command similar to the following:
     test4 7 11
Print a message if the command line is invalid:
           if(argc < 3)
           {
                   printf("Invalid command line: please supply two integer values\n");
                   exit(1);
           }
Note that you'll need to use the atoi function to convert the arguments (which are strings) to integer values.