04-cintro-post

04-cintro-post - Introduction to C Readings: Readings from...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to C Readings: Readings from CP:AMA (King), chapters 1-10, as detailed in the document available on the Web site. CS 136 Fall 2009 04: Introduction to C 1 The C programming language C was developed at Bell Labs in the early 1970s, in connection with the Unix operating system. It is a good language in which to do systems programming, because it provides some of the expressivity of high-level languages without obscuring what is happening at the machine level. The C language was standardized by the American National Standards Institute (ANSI) in 1989, and the standard was revised in 1999. CS 136 Fall 2009 04: Introduction to C 2 The first C program in the textbook #include <stdio.h> int main( void ) { printf("To C, or not to C: "); printf("that is the question.\n"); return 0; } Well explain this program line by line. CS 136 Fall 2009 04: Introduction to C 3 The C preprocessor #include <stdio.h> is an instruction to the C preprocessor , which rewrites the program before the compiler processes it. In this case, the instruction includes a header file which contains information about the standard I/O library. Well explain such header files as necessary, and not always put the #include statements on the slides, but they must be in your programs. CS 136 Fall 2009 04: Introduction to C 4 The main function The rest of the program defines one function, int main( void ) . The main function has no parameters, but produces an int value, which is a 32-bit integer. In general, we must list the types of all parameters and return types of functions. There are defaults which one should avoid using. CS 136 Fall 2009 04: Introduction to C 5 The body of the function main consists of three statements . Each one is terminated with a semicolon. They are grouped into a block by curly braces. The rough equivalent in Scheme would be ( define ( main ) ( begin ( printf " To Scheme, or not to Scheme: " ) ( printf " that is the question \ n " ) )) ( main ) CS 136 Fall 2009 04: Introduction to C 6 Every complete C program must include a main function, which is called when the program is executed. The return value, which is always 0 in our program, is provided to the operating system or the shell which invoked the program. 0 indicates a successful exit without error. Other values can be used to signal various errors. We will not be using this feature in this course. CS 136 Fall 2009 04: Introduction to C 7 Compiling and running a C program We can store the program in the file pun.c , and invoke the GNU C compiler ( gcc ) on it using the command-line interface. gcc -std=c99 -O -Wall -o pun pun.c The-o pun part puts the machine-language translation into the file pun ( pun.exe on Windows)....
View Full Document

This note was uploaded on 10/30/2011 for the course CS 136 taught by Professor Becker during the Spring '08 term at Waterloo.

Page1 / 55

04-cintro-post - Introduction to C Readings: Readings from...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online