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

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

Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
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.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program.
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
CS201 – Expressions & I/O. Precedence What is the value of * 4 ? 20 or 14 ? Without parentheses, there are a set of rules to fall back on. Unary.
Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
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%
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Introduction to C Language
Homework Reading Programming Assignments
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3: Assignment, Formatting, and Interactive Input.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Minimal standard C program int main(void) { return 0 ; }
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
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.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
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.
Last week: We talked about: History of C Compiler for C programming
User-Written Functions
Input from STDIN STDIN, standard input, comes from the keyboard.
© 2016 Pearson Education, Ltd. All rights reserved.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Getting Started with C.
A First Book of ANSI C Fourth Edition
Programming in C Input / Output.
Programming in C Input / Output.
A First Book of ANSI C Fourth Edition
CS111 Computer Programming
Introduction to C Topics Compilation Using the gcc Compiler
Perl I/O Learning Objectives:
A First Book of ANSI C Fourth Edition
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Programming in C Input / Output.
Homework Reading Programming Assignments Finish K&R Chapter 1
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C EECS May 2019.
Variables in C Topics Naming Variables Declaring Variables
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

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!) Making progress on HW1? Due next class! HW2 will be on line by next class.

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 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 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: % lpr typescript(printout typescript file)

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 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 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 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 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 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 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 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 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 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 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 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 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 File Copying # include int main ( ) { int c; c = getchar( ); while (c != EOF) { putchar (c); c = getchar ( ); } return 0; }

19 File Copying - Alternative # include int main ( ) { int c; while ( (c = getchar( ) ) != EOF) { putchar (c); } return 0; }

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)