Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Structure of a C program
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Homework Reading Programming Assignments
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Programming With C.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Introduction to C Topics Compilation Using the gcc Compiler
Last week: We talked about: History of C Compiler for C programming
C Formatted Input/Output
Chapter 1.2 Introduction to C++ Programming
CSCE 206 Structured Programming in C
User-Written Functions
The Machine Model Memory
Chapter 1.2 Introduction to C++ Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to C CSE 2031 Fall /3/ :33 AM.
Introduction to C Topics Compilation Using the gcc Compiler
Getting Started with C.
Lecture 13 & 14.
A First Book of ANSI C Fourth Edition
Introduction to C Topics Compilation Using the gcc Compiler
Intro to PHP & Variables
User-Defined Functions
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to CS Your First C Programs
CS111 Computer Programming
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
Program Breakdown, Variables, Types, Control Flow, and Input/Output
B. Ramamurthy University at Buffalo
Your questions from last session
C Programming Getting started Variables Basic C operators Conditionals
Homework Reading Programming Assignments Finish K&R Chapter 1
Introduction to C Topics Compilation Using the gcc Compiler
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Unit 3: Variables in Java
Introduction to C EECS May 2019.
An Overview of C.
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1? Read K&R chapter 1.1-1.9 Glass and Ables chapters 3

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

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

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

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

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

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

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

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

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

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

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;

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

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

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

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

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

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

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”

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

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;

int vs char type Values 0-127 decimal are ascii code characters. Fits in one byte: 01111111 (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

character Copying - Alternative # include <stdio.h> main ( ) { int c; while ( (c = getchar( ) ) != EOF) { putchar(c); }

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