Homework
Advertisement
This presentation is the property of its rightful owner.
1 / 20

Homework PowerPoint PPT Presentation

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”

Download Presentation

Homework

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.


Printable powerpoint

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)


  • Login