CPS 202 Lecture 1 Introductions o Instructor o Students The Text o Web site: http://www.knking.com o Type in and try the example code in the text o Practice transcribing, compiling, testing, with code that you know will work The Course o Homework - exercises out of the text, to be submitted via the course web site. Counts for 20% of the grade. Homework will be reviewed at the beginning of the next class. o Quizzes - four take-home quizzes during the course, which, in total, count for 40% of the grade. Quizzes must be submitted via the course web page. All quizzes are take-home and open-book. o Mini-Quiz - an in-class quiz, given at the end of class 6. Counts for 10% of your grade. Open-book. This quiz will be submitted via the course web site. o Exam - Counts for 30% of your grade, due before class on the day of our last class. Open-book. Must be submitted via the course web site. o Lectures - lectures will take up the bulk of the time spent in class, but there is almost always time at the end of class to practice C skills. In this time, I encourage you to meet with me to ask questions, to transcribe code from the text, to practice with Visual C++, or to work on the next quiz. Other points o Academic honesty. o No IMs in class during lecture. o No pagers during lecture. o No beepers during lecture. Questions? ----------------------------------- C - What is it? --------------- C is a programming language. Programs are written to tell the computer what to do. Other examples are Perl, Java, C++, C#, Python, and PHP. All of these languages have their roots in C. Other languages include Basic, Pascal, COBOL, FORTRAN, and Ada. C - Why learn it? ----------------- The questions I hear most often when I tell people I teach C, or that I make a living programming in C is, Isn't C dead? Isn't C replaced by C++ or Java or Perl or C#? Why bother teaching or learning C? C is not dead. The very fact that I make my living writing programs in C is a testament to that. I work for a company that has a thriving, and growing business in products written in C. C is fast, relatively simple, and very powerful. C, because it is the basis for so many other languages, is also a great stepping-stone language. Because Java is based on C, you can quickly skip over the basic of Java and move into what is different about Java, and then about what is new in Java. Same for Perl or C++. It gives you a frame of reference for a lifetime of programming language learning. C is not dead. It was recently updated in the C99 standard, a standard which took a look at all the good things C can do and asked, what can it do better? The route the authors of the C99 standard did not do is migrate C towards C++. C++ is a language all its own, and adding a lot of C++ functionality to C made no sense. But there are plenty of things that made perfect sense, and the C programming community is anxiously awaiting compilers that implement the C99 changes. C is not dead. There are thousands of programs that can be found on the Internet that are written in C. Even some programming languages themselves are written in C - Perl for example. The DeCSS program, which caused a furor in the DVD maker world not two years ago, was written in C. It is a lingua franca. Part of the reason for this lies in C's history. The history of C ---------------- C was first created in about 1973, for the Unix operating system. In those days, a powerful computer had 8 or 16K of memory (that's K, not M). Most programming was done in assembly language, where the programmer writes the individual instructions that the computer needed to complete a task. Assembly is very non-intuitive. It is hard to write and hard to maintain. It has little semblance to English, and so is hard to learn. C evolved out of two ancient programming languages called Algol and BCPL, and became the language of choice for programmers at Bell Labs, where the Unix system was being developed. In the late 1970's, Brian Kernighan and Dennis Ritchie wrote the book, The C Programming Language. This thin little book described the language completely in just over a hundred pages. The White Book, as it became known, was, and still is, the standard bearer for C. Because everyone who wrote a compiler designed it from the White Book, the C language was very standard. From one machine to another, a program could be written once and run on many machines. In the 1980's, the ANSI organization decided that the portability of C was becoming extremely important, and to prevent the language from splitting off into different hybrids, a committee was formed whose job was to standardize C. The ANSI C standard, which we will be learning, was published in 1989, and is known as ANSI C, or C89. The second edition of the White Book was published under this new standard, and that edition can still be bought online or in larger bookstores. I have a dog- eared copy that I keep at my desk at work and refer to often. In 1985, C++ was created at Bell Labs to deal with some of C's shortcomings when it came to dealing with objects. Objects are a programming concept that C can handle to a degree, but which C++ was built around. C++ is particularly well-suited for writing Windows-based programs, because Windows is based on an object model. Of particular import to us is that all C++ compilers must be able to compile straight C code. In 2001, Microsoft announced that it was creating C#, based on C and C++, for its .NET strategy. What are C's strengths? ----------------------- C is very strong in the Unix development environment, and is a mainstay of the open source movement. The reason is its portability. A program written on one computer can almost always be compiled and run on another computer without any changes, especially if the programmer kept his mind on portability (by not using features specific to one computer). C is low-level, meaning that there is little between the program you write and the code that is generated for the computer to run. Because of this, there is a drawback, and that is that C executables are not portable from one machine to another. In Perl, for example, you can write code that will run on any machine with no changes at all, no extra steps. In C, your code can be written to compile on any machine, but separate compiles must be created. This seems like a drawback at first, but when you think of the performance benefits it makes a huge difference. One web page I found tracked the same program in C as in Perl. The C program took 10 minutes to run, and the Perl program was automatically killed by the system after 300 minutes. Another drawback of C, though, is that because of its low-levelness, it can be very hard to write a program in C that can be very easy to write in other languages. This can greatly increase development time, though if the costs of development can be absorbed, the cost of deployment are often well worth the effort. Part of the reason that it can take so long to write a C program of any real use is that C is very small. The core language has only a few dozen keywords, and the extended standard language has only another 100 or so. By contrast, Perl has thousands of keywords, if you take the standard Modules into account. These modules allow you to, say, connect to an ftp site, but in doing so, adds real complexity and size to the language. C can be hard for first-time programmers because it is not very forgiving. The text says that C is forgiving, and that's true - C assumes that the programmer knows what he's doing, and has no comment on potential bugs. But this can also be a trap, because if you're not aware of a bug, it will be harder to find. C can be hard to read, but this is less of a problem of the language than it is of the programmer. Programmers who have worked with the language for a long time tend to know exactly what it is they intend to do when they write 100 lines of code in one short sitting. But because of the terseness of the language, that meaning can be lost on another programmer, ever on one of similar experience and ability. For this reason, I will try to emphasize in my comments to you the importance of explaining your code, in your code. This is called "commenting," and we will focus on it in the next class. Questions? ----------------------------------- Practice with Visual C++ Type in this program and try to compile, using the instructions on the web page. #include main() { printf("Hello World!\n"); return(0); } Symbols are very important in C - be sure to use the correct ones at all times. Learning them and what they do is a crucial skill in C programming.