CS010 Practice 2

C Basics

 

Today's practice will familiarize you with the C language and compiler. The command to compile your program is:

gcc -Wall -o <output-filename> <c-filename>

output-filename is the name of the file that the compiler will create. It will contain the executable program. c-filename is the name of the C file you want to compile. Its name should end in .c.

  1. Use the X copy-and-paste commands to copy face.c from the lecture notes and paste it into an Emacs buffer. Save the buffer to a file and compile and execute it.
  2. Write a C function that returns the minimum value in an array of integers. Write a main program that calls this function with arrays of different sizes and prints out the minimum value found.
  3. Write a C program that reads characters from the keyboard. When the user types q, it should output the number of characters that the user typed and then quit.
  4. Modify the previous program so that it also counts lines and words and outputs all three counts when the user quits.
  5. What does the following program do and why? Try to reason it through first, then try running it to see what happens. You might want to add some printf statements to help understand its behavior.
    int main () {
        int i, a[4];
       
        for (i = 1; i <= 5; i++) {
            a[i] = 0;
        }
    }
       

    (Remember how to use C-z, kill and C-c to stop processes!)