Main Contents Page
CTEC1401 Programming in C
Quick Reference
Week 1: The Unix Environment
Week 2: Introduction to C
Week 3: Types, Vars, Output
  1. Before the lab session read CFTTP Chapter 1.

  2. box

    Create a text file called box.c that contains the following C program:
    #include <stdio.h>
    
    int main()
    {
        printf ("\t***\n");
        printf ("\t***\n");
        printf ("\t***\n");
        return 0;
    }
    
    Save the file and then compile the program:
    gcc box.c -o box
    
    Assuming you copied the program correctly, the compiler will translate box.c successfully. Run the program:
    ./box
    
    Note that we have suggested you type "./box" rather than simply "box". The preceding "./" is used to tell Unix which directory to look in for the executable program: the dot means "here" (current directory). It may seem strange but, in fact, the Unix shell does not normally look for executable programs in the current directory.

    However, your system may be configured to search the current directory (i.e. ".") in which case you can leave out the preceding "./" and run the program by just typing "box".

    How can you tell if your system is configured to search your current directory for executable files?"

    At the shell prompt type in the command:

    echo $PATH
    
    and have a look at what you get back. You will see a colon-separated list of directories - these are the directories that are searched for executables when a command is typed at the terminal. Now look carefully - does the search path contain "."? It will look like this, ":.", with a preceding colon, if it occurs at the end of the search path, or it will be surrounded by colons if it occurs somewhere else within the search path ":.:".

    How can you add "." to your search path?"

    You need to add the following line to the .profile file which is found in your home directory.

    export PATH="$PATH:."
    

  3. frame

    Write, compile and run a C program, frame.c, that prints your name in a frame like so.
            +-------------+
            |  Your Name  |
            +-------------+
    

    Remember, to compile the program:

    gcc frame.c -o frame
    
    Assuming the program compiled correctly, run the program:
    ./frame
    

  4. diamond

    Write, compile and run a C program, diamond.c, which outputs a small diamond shape (made of *s) on the terminal.

    Remember, to compile the program:

    gcc diamond.c -o diamond
    
    Assuming the program compiled correctly, run the program:
    ./diamond
    

  5. tabs1

    Create a text file called tabs1.c that contains the following C program:
    #include <stdio.h>
    
    int main()
    {
        printf ("\t1\t2\t3\n");
        printf ("\t4\t5\t6\n");
        printf ("\t7\t8\t9\n");
        return 0;
    }
    
    Save the file and then compile the program:
    gcc tabs1.c -o tabs1
    
    Assuming you copied the program correctly, the compiler will translate tabs1.c successfully. Run the program:
    ./tabs1
    
  6. tabs2

    Create a text file called tabs2.c that contains the following C program:
    #include <stdio.h>
    
    int main()
    {
        printf ("\t1\t2\t3\n\t4\t5\t6\n\t7\t8\t9\n");
        return 0;
    }
    

    Save the file and then compile the program:

    gcc tabs2.c -o tabs2
    
    Assuming you copied the program correctly, the compiler will translate tabs2.c successfully. Run the program:
    ./tabs2
    

    How is tabs2.c different from tabs1.c?

    In what way are the two programs the same?

  7. poem1

    Write, compile and run a C program, poem1.c, that outputs the following poem on the terminal.

    Ensanguining the sky
    How heavily it dies
    Into the west away
    Past touch and sight and sound
    No longer to be found
    How hopeless underground
    Falls the remorseless day

    But, you are only allowed to use ONE printf statement.

  8. bigX

    Write, compile and run a C program, bigX.c, that outputs the following big X on the terminal.
    \\  //
    \\//
    //\\
    // \\

    (This time you can use as many printf statements as you like.)