[Home]

CS 223 - Spring 2017. 1/20

Welcome to CS 223!

Logical problem of the day

lp0120.c
int main(void) {
   int ch;
   while (ch = getchar() != EOF)
     putchar(ch);
}
Does this program copy its input to its output, or what? (Note: see operator precedence)

Lecture 2: Introduction to C: atoi, gcc, make

If you're thinking of taking CPSC 223, please do the following.

Administrivia

  • Office hours:
    We have added the public tests for assignment 1. In addition, we have met ULAs and scheduled office hours. The office hours are Sun/Tue/Thu 8-11 PM at Hillhouse 17 Rm 111. Also, we will hold a tutorial to cover zoo and UNIX fundamentals from 7-8 PM this Sunday.
  • There was a question posted to piazza about an "invalid group" error with submit.
    $ /c/cs223/bin/submit 1 Makefile time.log Total.c
    invalid group: 
    $ /c/cs223/bin/check 1
    invalid group: 
    
    My initial instinct was to blame myself. (That's just me. I have been married for 30 years, so it is usually the best strategy.) My ultimate diagnosis was:
    OK. The script looks to see if you have a directory in /c/cs223/class If not, you are UNKNOWN. You need to set up your zoo account with the zookeepeer: : https://ivy.yale.edu/zookeeper/
    It looks like that fixed the problem. Let me know if it recurs.

    Course Feedback

    Debugging is not limited to computer programs. It can be broadly applied to many parts of life. I, for one, believe that life should be a process of continuous improvement.

    I, like many of you, have reviewed the evaluations for this course from last semester. You are enrolled today because of or in spite of those comments. Be that as it may, I am taking the suggestions seriously. I intend to address the following issues this semester. (Some of the reported bugs were intended as features, e.g., using tech interview questions on exams.)

    In addition, I plan to have a midcourse evaluation, to give you a chance to express your opinions in a timely manner so that you may benefit from any improvements before the end of the term.

    Lecture Starts Here

    Data Structures == Abstractions

    CS 223 covers the intersection of the following topics: One way to think about data structures (as well as computer science itself) is as an abstract representation of knowledge.

    C Paradox

    I don't think C gets enough credit. Sure, C doesn't love you. C isn't about love--C is about thrills. C hangs around in the bad part of town. C knows all the gang signs. C has a motorcycle, and wears the leathers everywhere, and never wears a helmet, because that would mess up C's punked-out hair. C likes to give cops the finger and grin and speed away. Mention that you'd like something, and C will pretend to ignore you; the next day, C will bring you one, no questions asked, and toss it to you with a you-know-you-want-me smirk that makes your heart race. Where did C get it? "It fell off a truck," C says, putting away the boltcutters. You start to feel like C doesn't know the meaning of "private" or "protected": what C wants, C takes. This excites you. C knows how to get you anything but safety. C will give you anything but commitment. In the end, you'll leave C, not because you want something better, but because you can't handle the intensity. C says "I'm gonna live fast, die young, and leave a good-looking corpse," but you know that C can never die, not so long as C is still the fastest thing on the road

    We conclude with a discussion of the simple C program for counting characters in standard input. This is the basis for the first homework assignment. s17h1.v0.

    See /c/cs223/hw1. Specifically, check out the sample file /c/cs223/hw1/Total.c

    atoi()

    You are encouraged to expand on the algorithm for atoi() to implement Total.c. In particular, you need to handle a variety of bases: 10, 8, 16, and 2. K&R pdf section 2.7, page 61. Remember that you do not need to handle floating point numbers or scientific notation.

    gcc

    Compiling with GCC. See C In a Nutshell, (2nd edition) chapter 18.
      ## get version number
      gcc --version
    
      ## get warnings.  Produce a.out
      gcc -Wall Total.c
    
      ## get warnings.  Produce Total 
      gcc -Wall -o Total Total.c
    
      ## preprocessing to standard output
      gcc -E Total.c
    
      ## preprocessing to file
      gcc -E -o Total.i Total.c
    
      ## preprocessing, do not remove comments
      gcc -E -C -o Total.i Total.c
    
      ## compile, but do not assemble.
      gcc -S Total.c
    
      ## compile, but do not assemble.  Leave C names as comments
      gcc -S -fverbose-asm Total.c
    
      ## compile, produce object file: Total.o
      gcc -c Total.c
    
      ## compile, link in the math library 
      gcc -o Total -lm Total.c
    
          
    

    make

    make is a utility that knows how to compile your programs, particularly when there are multiple files. Imagine that you are cooking a meal. You may need to purchase the ingrediants, pre-heat the oven, marinade the meat, cook the meat, make the gravy, and serve the dish. These steps are not independent. You need to shop before your cook. You have to pre-heat the oven before roasting the meat. You need to cook the meat before making the gravy.

    A C program may comprise multiple header (.h), source (.c), object (.o), and library (.a) files. If the .o file is older than the .c or .h files on which it depends, you need to recompile. If not, the .o file is up to date.

    make knows which file is older than others by looking at the modification date. You can fool make using the touch command. For example, if you want to recompile a .o file even if the .c file older, you can touch the .c file.

    You inform make of these dependencies with the makefile

    See /c/cs223/hw1/Makefile


    [Home]