Download presentation
Presentation is loading. Please wait.
Published byShannon O’Neal’ Modified over 9 years ago
1
1 Homework Done the reading? –K&R 1.1-1.9 –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!) Making progress on HW1? Due next class! HW2 will be on line by next class.
2
2 gcc At UNIX prompt, type “gcc hello.c –o hello” If you get any compiler error messages: –Figure out what they mean –Study and correct your source code –Repeat “gcc hello.c –o hello” until you get no messages If you get no output except a new prompt: –The compiler has accepted your source code –You should now have a file named “hello”
3
3 Running your program “hello” At UNIX prompt, type “hello” If you get the printout “Hello World!” and a new prompt, your program ran successfully If not, –Study any UNIX error messages for clues –Study your source code for logical errors –Probably logical errors - compiler didn’t catch –Fix your source code and recompile / rerun
4
4 Handing in Assignments Turn in assignments as hard copy of typescript file % script(Start recording typescript file) Script started, file is typescript % ls –l(list directory entries) % cat hello.c(display source file) % gcc hello.c –o hello(compile source file) % hello(run executable file) % exit script done on Thu Aug 22 14:30:02 2002 % lpr typescript(printout typescript file)
5
5 Debugging a C program error There is a big difference between: –The program compiling correctly –The program doing what you want it to do You hope the compiler will catch your errors –These errors will be easier to find If the compiler does not catch your errors –These errors will be harder to find
6
6 Compiler Error Messages A compiler error message may direct you to a specific error in your program A compiler error message may be vague about what the error is and why it is an error Some compilers are better than others at providing useful error messages!
7
7 Compiler Error Messages Introduced error is a missing closing brace: #include int main ( ) { printf("Hello, World!"); return 0; /* missing “}” */ % gcc hello.c –o hello hello.c: In function `main': hello.c:6: parse error at end of input Not a very helpful message – Gotta figure it out!
8
8 Variables Defined Data Type, Name, and (= value) int lower = 0;/* Note: “=“ and “;” */ lower case by convention for readability An executable statement Memory location assigned to hold the value Value can be changed as program executes lower = 20;/* Legal */
9
9 Symbolic Constants Defined Name and Value #define LOWER 0/* Note: No “=“ or “;” */ UPPER CASE by convention for readability Not an executable statement No memory location assigned to hold value Value can’t be changed as program executes LOWER = 20;/* NOT Legal */
10
10 Example Program (K&R, P 15) #include #define LOWER 0/* Symbolic Constants */ #define UPPER 300 #define STEP 20 /* Print out Fahrenheit – Celsius Conversion Table */ int main ( ) { int fahr;/* Variable */ for (fahr = LOWER; fahr <= UPPER; fahr =fahr + STEP) printf(“%3d,%6.1f\n”, fahr, (5.0/9.0)*(fahr – 32)); return 0; }
11
11 For Statement for (fahr = LOWER; fahr <=UPPER; fahr = fahr + STEP) { statements within the loop; } next statement after the loop terminates; for (A; B; C) - controls executing statement(s) within the loop A is initialization (executed once when loop is started) B is the loop test statement (when to stop looping) C is a statement to execute at end of each loop
12
12 Printf statement printf (“%3d, %6.1f\n”, fahr, (5.0/9.0)* (fahr - 32)); First Argument to printf = “%3d, %6.1f\n” %3d = integer format with 3 digits %6.1f = floating point format with 6 digits and 1 decimal \n = end of line character just as in “Hello World!” Second Argument to printf = fahr variable to print in %3d format Third Argument to printf = (5.0/9.0)*(fahr – 32.0) expression to calculate and print in %6.1f format Note: For open book tests, remember K&R, Page 154!
13
13 printf formats printf ("Values:∆%3d,∆%6.1f\n", fahr, (5.0/9.0)*(fahr-32)); Where the characters specified by "∆" are what we write to show a space explicitly. Quoted string "Value:..." is just like "Hello, world", but %3d and %6.1f are special placeholders, called conversion specifications. This means that the two expressions following the quoted string, fahr, and (5.0/9.0)*(fahr-32), are to be printed according to the prescription given. The table here would look like: –Values:∆∆∆0,∆∆-17.8 –Values:∆∆20,∆∆∆-6.7 –Values:∆∆40,∆∆∆∆4.4
14
14 printf formats Other characters in "Values:...", such as "," and "∆" are printed literally. The %3d means that an integer is printed so as to take up 3 spaces, right adjusted -- "∆40", but no initial space for "100" -- still have space before 100 because came after "Values:∆". The %6.1f means to print a float number (floating point or “double” by default, represented with a fractional part), with a total of 6 spaces used up and 1 digit after the decimal point: thus "∆-17.8" uses 6 spaces.
15
15 Functions A function is a separate block of code that you can call as part of your program A function executes and returns to next line after you call it in your program Arguments within parentheses may be passed in Arguments are passed by value! function_name (arguments); A return value may be passed back return_value = function_name (arguments);
16
16 Character I/O – getchar( ) A standard function/macro defined in No arguments are passed to “getchar ( )” getchar ( ) returns an int value from stdin int c; … c = getchar ( ); Next character input is returned as value of “c”
17
17 Character I/O – putchar( ) A standard function/macro defined in Character to print to stdout is passed as argument There is no return value int c; … putchar (c); Next character output is based on value of “c”
18
18 File Copying # include int main ( ) { int c; c = getchar( ); while (c != EOF) { putchar (c); c = getchar ( ); } return 0; }
19
19 File Copying - Alternative # include int main ( ) { int c; while ( (c = getchar( ) ) != EOF) { putchar (c); } return 0; }
20
20 Redirecting stdin and stdout copy junk.c Copies FROM the contents of existing file hello.c as stdin TO the contents of a new file junk.c as stdout (Note: will overwrite any existing file junk.c)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.