homework n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Homework PowerPoint Presentation
Download Presentation
Homework

Loading in 2 Seconds...

play fullscreen
1 / 20

Homework - PowerPoint PPT Presentation


  • 646 Views
  • Uploaded on

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. gcc. At UNIX prompt, type “gcc hello.c –o hello”

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Homework' - lotus


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
homework
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.
slide2
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”
running your program hello
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
handing in assignments
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)

debugging a c program error
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
compiler error messages
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!
compiler error messages1
Compiler Error Messages
  • Introduced error is a missing closing brace:

#include <stdio.h>

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!
variables
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 */

symbolic constants
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 */

example program k r p 15
Example Program (K&R, P 15)

#include <stdio.h>

#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;

}

for statement
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

printf statement
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!

printf formats
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
printf formats1
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.
functions
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);

character i o getchar
Character I/O – getchar( )
  • A standard function/macro defined in <stdio.h>
  • 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”
character i o putchar
Character I/O – putchar( )
  • A standard function/macro defined in <stdio.h>
  • 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”
file copying
File Copying

# include <stdio.h>

int main ( )

{

int c;

c = getchar( );

while (c != EOF) {

putchar (c);

c = getchar ( );

}

return 0;

}

file copying alternative
File Copying - Alternative

# include <stdio.h>

int main ( )

{

int c;

while ( (c = getchar( ) ) != EOF) {

putchar (c);

}

return 0;

}

redirecting stdin and stdout
Redirecting stdin and stdout
  • copy <hello.c >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)