Friday, February 10, Lab Notes

Similar documents
Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Lecture 7: file I/O, more Unix

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/9/17

Overview (1A) Young Won Lim 9/25/17

Basic and Practice in Programming Lab 10

File Access. FILE * fopen(const char *name, const char * mode);

Lesson 6.1: Structs. This declares a collection of two integer variables to denote the two coordinates of a point in a plane.

File IO and command line input CSE 2451

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Today s Learning Objectives

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

COMP s1 Lecture 1

CSC UNIX System, Spring 2015

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Introduction to Computing Lecture 03: Basic input / output operations

Contents. A Review of C language. Visual C Visual C++ 6.0

Software Development With Emacs: The Edit-Compile-Debug Cycle

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

AMCAT Automata Coding Sample Questions And Answers

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Lecture 03 Bits, Bytes and Data Types

Lecture 8: Structs & File I/O

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

C programming basics T3-1 -

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Subject: Fundamental of Computer Programming 2068

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Friday, February 3, Lab Notes. Functions in C Arrays as parameters Pass by value vs pass by reference Programs 1 and 2

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

2009 S2 COMP File Operations

Computational Methods of Scientific Programming Fall 2007

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Intermediate Programming, Spring 2017*

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Princeton University. Testing. Computer Science 217: Introduction to Programming Systems

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

COMsW Introduction to Computer Programming in C

The following program computes a Calculus value, the "trapezoidal approximation of

CSE 333 Autumn 2014 Midterm Key

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

System Programming. Introduction to Unix

EXTERNAL TESTING. Princeton University Computer Science 217: Introduction to Programming Systems. Why Test? Testing. Why Test? Why Test?

1. The Mac Environment in SIE 1222

Homework 2 Answers. Due Date: Monday, April 29, 2002, at 11:59PM Points: 100

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

ENG120. Misc. Topics

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Programming and Data Structure

Worksheet 4 Basic Input functions and Mathematical Operators

CSE 124 Discussion (10/3) C/C++ Basics

Introduction to Supercomputing

Symbols, Compilation Units, and Pre-Processing

CSE 303: Concepts and Tools for Software Development

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Today s class. Review of more C Operating system overview. Informationsteknologi

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

1. The Mac Environment in Sierra Hall 1242

EECS2031 Software Tools

ch = argv[i][++j]; /* why does ++j but j++ does not? */

An Ungentle Introduction to C

CSC209F Midterm (L5101) Fall 1998 University of Toronto Department of Computer Science

8. Characters, Strings and Files

Goals of this Lecture

Arrays and Pointers (part 2) Be extra careful with pointers!

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads

Main differences with Java

211: Computer Architecture Summer 2016

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Arrays and Pointers (part 2) Be extra careful with pointers!

Problem Set 1: Unix Commands 1

Stream Model of I/O. Basic I/O in C

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

CSE 303 Lecture 8. Intro to C programming

Princeton University Computer Science 217: Introduction to Programming Systems. Testing

Programming & Data Structure

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Transcription:

Friday, February 10, 2017 Lab Notes Topics for today Structures in C Redirection of input and output in a Unix-like environment Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due 2/24 1. Structures in C An array is a collection of variables of the same type. C lets us define structures which potentially contain items of different types. Here is an example similar to Fig. 2.40 struct person char initial; char last[15]; int age; int salary; char gender; Having defined type person we can declare variables of the type person Bill, Amy, Mike, Kate; And access the individual fields using dot notation as follows Bill.gender = M ; Bill.salary = Bill.age * 10; printf( %s\n,bill.last); Amy.salary = Mike.salary; Kate.age++; We can have arrays of structures Assign Bill person Department [50]; Department[0] = Bill; and find the average salary of the department members sum = 0; for (i=0; i<50; i++) sum+=department[i].salary; printf( Average is %d\n,sum/50); Comp 162 Lab Notes Page 1 of 6 February 10, 2017

2. Redirection of input and output in a Unix-like environment A C program has three built-in file streams: stdin, stderr and stdout. When you use scanf you are reading from stdin. When you use printf you are writing to stdout. You can write to stderr by using fprintf(stderr,.. When running programs in a Unix environment, a simple way to read data from a file into a program and to save results and/or error messages to a file without making any changes to the program source code is to use the Unix shell redirection. The following table shows how you can redirect the three streams when calling a.out. Command Stdin Stdout Stderr a.out Keyboard Screen Screen a.out < Datafile Datafile Screen Screen a.out > Results Keyboard Results Screen a.out < Datafile > Results Datafile Results Screen a.out >& Allout Keyboard Allout Allout a.out < Datafile >& Allout Datafile Allout Allout (a.out > Results) >& Errors Keyboard Results Errors (a.out < Datafile > Results) >& Errors Datafile Results Errors Comp 162 Lab Notes Page 2 of 6 February 10, 2017

Example: stdin, stdout, stderr psmith@redwood:~/comp162$ cat averages.c #include <stdio.h> // reads N followed by N integers. Outputs average. int main() int i, count, number, sum=0; scanf("%d",&count); if (count==0) fprintf(stderr,"error: Count is zero\n"); return; for (i=0; i<count; i++) scanf("%d",&number); sum+=number; printf("average of %d numbers is %5.3f\n",count,(float)sum/(float)count); psmith@redwood:~/comp162$ cat gooddata 7 34 21 12 55 90 2 83 psmith@redwood:~/comp162$ cat baddata 0 psmith@redwood:~/comp162$ gcc averages.c psmith@redwood:~/comp162$ (./a.out < gooddata > goodresults ) >& errorlist psmith@redwood:~/comp162$ cat goodresults Average of 7 numbers is 42.429 psmith@redwood:~/comp162$ cat errorlist psmith@redwood:~/comp162$ (./a.out < baddata > badresults ) >& errorlist psmith@redwood:~/comp162$ cat badresults psmith@redwood:~/comp162$ cat errorlist Error: Count is zero psmith@redwood:~/comp162$ exit Comp 162 Lab Notes Page 3 of 6 February 10, 2017

3. Command line arguments In general, the main function in a C program can have the following heading int main(int argc, char *argv[]) or int main(int argc, char **argv) When the program runs, the operating system: puts into argc (argument count) the number of tokens on the command line. This count includes the program name itself. Puts into the elements of array argv (argument vector) pointers to the texts of the tokens. Here is a simple program; it just echoes the components of the command line back to the standard output. Example #include <stdio.h> int main (int argc, char* argv[]) int i; printf("argc is: %d\n", argc); for (i=0; i<argc; i++) printf("argv [%d] is %s\n",i,argv[i]); Here is the compilation and a run of the program. hp9k2 22: gcc ctesta.c hp9k2 23: a.out one two three argc is: 4 argv [0] is a.out argv [1] is one argv [2] is two argv [3] is three Comp 162 Lab Notes Page 4 of 6 February 10, 2017

4. More pre-processor options The pre-processor runs before the compiler and processes lines beginning with #. Thus, it handles the #include lines. We can use #define to define symbols as in #define MAXMEM 4096 #define PI 3.14159 The pre-processor replaces instance of the symbol name by the corresponding number. For example, we could declare int memory[maxmem]. Also we can define symbols to control debugging as in the following example. #define DEBUG 1 main() int a[] = 2, 4, 3, -1, 7 ; int i,sum=0; for (i=0; i<5; i++) sum += a[i]; #ifdef DEBUG printf ("sum is now %d\n",sum); #endif printf("sum is %d\n", sum); The text between #ifdef and #endif is only included in the output from the preprocessor (and hence input to the compiler) if the symbol DEBUG is defined (ifdef) with a non-zero value. There is also #ifndef (if not defined) hp9k2 22: gcc ctestb.c hp9k2 23: a.out sum is now 2 sum is now 6 sum is now 9 sum is now 8 sum is now 15 sum is 15 To remove the debugging information from the input to the compiler we need only change the value of the symbol to 0 (or to omit its definition altogether) thus Comp 162 Lab Notes Page 5 of 6 February 10, 2017

#define DEBUG 0 main() int a[] = 2, 4, 3, -1, 7 ; int i,sum=0; for (i=0; i<5; i++) sum += a[i]; #ifdef DEBUG printf ("sum is now %d\n",sum); #endif printf("sum is %d\n", sum); Now when the program is compiled the debug statements are omitted hp9k2 26: gcc ctestb.c hp9k2 27: a.out sum is 15 You can probably see how debugging can also be controlled through command-line arguments. 5. Programs Program 1 is due today. Submit source code and testing results electronically. For program 2, breaking the solution into functions will help in development and testing. Think about setting up an index to the cellar perhaps using an array of structures. When testing the program, consider creating a file with a test sequence then use redirection as described in section 2 above. Comp 162 Lab Notes Page 6 of 6 February 10, 2017