Download presentation
Presentation is loading. Please wait.
Published byEne Eller Modified over 6 years ago
1
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Read K&R chapter Glass and Ables chapters 3
2
Compiling and Linking Source program Object Module Executable hello.c
hello.o hello compile gcc -c link gcc -o Gcc use 2 steps: compilation and linking Compile -> convert sudo English to numbers to computer understand Link -> bring everything together Hello.o and hello are not text file they’re binary files which computer understands You cant compile a code in a computer and use it in another machine. The compiler convert to numbers good for first machine
3
The gcc compiler At UNIX/LINUX prompt, type “gcc hello.c –o hello”
Or use gcc –m32 hello.c –o hello to build a 32-bit application If you get any compiler error messages: Figure out what they mean Study and correct your source code Repeat “gcc –m32 hello.c –o hello” until you get no error messages If you get no messages except a new prompt: The compiler has accepted your source code You should now have a file named “hello” If you forget to specify –o hello as in “gcc hello.c”, you will create a file name “a.out” Compiler error tells you something is wrong It gives you the line number that has problem too
4
Running your program “hello”
At UNIX/LINUX 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 2 kind of errors: Compiler error (syntax) – logical error Which one is harder to find? Fix all the warning massages before running
5
Debugging a C program error
There is a big difference between: The program compiling correctly (no compiler error) The program doing what you want it to do (no compiler and logical errors) 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
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
Compiler Error Messages
Introduced error is a missing closing brace: #include <stdio.h> int main(void ) { 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! It doesn’t give you details about the error but the line number
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 (known as definitions) Value can be changed as program executes lower = 20; /* Legal */ You should first define your variable and then use it. Assign a value to it. Initialization. You can put comment anywhere
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 (known as declarations) Value can’t be changed as program executes LOWER = 20; /* NOT Legal */ Using something many times. It’s not a statement. Compiler replace it before compiling Only variables could be changed Memory map diagram. Address and content. Reserve a location in RAM int is 4 B
10
for Statement 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 Example: for (fahr = LOWER; fahr <=UPPER; fahr = fahr + STEP) { statements within the loop; } next statement after the loop terminates; 1.A 2.B 3.Statement 4.C
11
For loop example Program (K&R, P15)
#include <stdio.h> #define LOWER 0 /* Symbolic Constants */ #define UPPER 300 #define STEP 20 /* Print out Fahrenheit – Celsius Conversion Table */ main() { int fahr; /* Variable type*/ for (fahr = LOWER; fahr <= UPPER; fahr =fahr + STEP) printf(“%3d,%6.1f\n”, fahr, (5.0/9.0)*(fahr – 32)); } declaration definition main() is also acceptable You can define and then assign something to it in 2 statements With or without Curly brackets. More than one statement statements
12
for Statement for (fahr = LOWER; fahr <=UPPER; fahr = fahr + STEP) { statement 1; } statement 2; statement 1 (fahr = 0 <= 300) statement 1 (fahr = 20 <= 300) . statement 1 (fahr = 300 <= 300) Statement 2
13
for Statement Note any part of a for loop can be left out.
For (init; loop_test; increment) If init or increment expression is left out, and just has the loop_test, program must initialize and increment by other means fahr = LOWER; for (;fahr <=UPPER;){ statement 1; fahr = fahr + STEP } statement 2;
14
for Statement Note any part of a for loop can be left out.
For (init; loop_test; increment) If loop_test is left out, it will loop for ever. (program must break) for (fahr = LOWER;; fahr = fahr + STEP){ if(fahr > UPPER) break; statement 1; } statement 2; Infinite loop is good like Microsoft windows
15
printf statement printf (“%3d, %6.1f\n”, fahr, (5.0/9.0)* (fahr - 32)); First Argument defines print format: “%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 fahr corresponds to the first % (print in %3d format) Third Argument (5.0/9.0)*(fahr – 32.0) corresponds to the second % (expression to calculate and print in %6.1f format) Note: Basic printf conversions shown in K&R, Page 154 Formatting statements and then variables % means format Show %6.1f on blackboard. 6 digits including dot and minus Use formats to have beautiful columns
16
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 For illustration only You can add spaces and other strings For illustration only
17
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 at least 3 spaces, right adjusted -- "∆40", but no initial space for "100" -- still have space before 100 because "Values:∆” prints out a space after “:”. The %6.1f means to print a float number (floating point or “double” by default, represented with a fractional part), with a total of at least 6 spaces and 1 digit after the decimal point: thus "∆-17.8" uses 6 spaces. The formatting characters will be substitute with the variables
18
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 (input arguments); A return value may be passed back return_value = function_name (arguments); You always have a main function
19
Character I/O – getchar( )
A standard function/macro defined in <stdio.h> No arguments are passed to “getchar( )” getchar( ) returns an int value int c; … c = getchar( ); Next character input is returned as value of “c” What ever you typed in, will save as a number in c Each key has a different number
20
Character I/O – putchar( )
A standard function/macro defined in <stdio.h> The character to print is passed as argument There is no return value int c; … putchar(c); Next character output is based on value of “c”
21
Character Copying # include <stdio.h> int main (void) { int c;
c = getchar( ); while (c != EOF) { putchar (c); c = getchar ( ); } return 0; EOF is a symbolic constant defined in <stdio.h> and is bigger than any char value Ctrl + D Reading something in and writing it out EOF is a constant defined in stdio.h EOF is the end of file = -1
22
while Statement while (A) - controls executing statement(s) within the loop A is the loop test statement (when to stop looping) Example: while (c != EOF) { statements within the loop; } next statement after the loop terminates;
23
int vs char type Values 0-127 decimal are ascii code characters.
Fits in one byte: (binary) is 127. int type c uses four bytes of significance, from -2^31 to 2^31 -1. Have another integer type, called char, which holds only 1 byte of significance from -128 to 127 (or else 0 to 255). How many bytes you want to allocate is important Int 4Bytes char 1Bytes
24
character Copying - Alternative
# include <stdio.h> main ( ) { int c; while ( (c = getchar( ) ) != EOF) { putchar(c); }
25
Redirecting stdin and stdout
Redirect getchar( ) to read from a file prog <input.txt Redirect putchar( ) to write to a file prog >output.txt The input.txt and output.txt files are located at the user’s directory It’s giving character from the file you provided and putting it in the file you provided
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.