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

Similar documents
Friday, February 10, Lab Notes

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

File IO and command line input CSE 2451

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

COMP s1 Lecture 1

CSC UNIX System, Spring 2015

Basic and Practice in Programming Lab 10

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

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

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

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

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

AMCAT Automata Coding Sample Questions And Answers

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

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

Today s Learning Objectives

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

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

C programming basics T3-1 -

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

2009 S2 COMP File Operations

Lecture 8: Structs & File I/O

Introduction to Computing Lecture 03: Basic input / output operations

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

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

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

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

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

Lecture 03 Bits, Bytes and Data Types

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

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

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

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

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

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

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

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*

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

COMsW Introduction to Computer Programming in C

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

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

1. The Mac Environment in SIE 1222

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

CSE 333 Autumn 2014 Midterm Key

Computational Methods of Scientific Programming Fall 2007

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

Programming and Data Structure

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

Figure 1 Ring Structures

CSC111 Computer Science II

do { write_program; OK = test_program ( ); } while (!OK); upload program to customer;

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

1. The Mac Environment in Sierra Hall 1242

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

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

Binghamton University. CS-220 Spring Includes & Streams

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

EECS2031 Software Tools

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

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

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

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

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

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

Problem Set 1: Unix Commands 1

Programming in C week 1 meeting Tiina Niklander

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

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

Programming & Data Structure

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

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

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

Creating, Compiling and Executing

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

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CSE 374 Midterm Exam Sample Solution 2/6/12

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

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

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Introduction to Supercomputing

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

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

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

8. Characters, Strings and Files

CSE 303: Concepts and Tools for Software Development

File I/O. Preprocessor Macros

CS3157: Advanced Programming. Outline

System Programming. Introduction to Unix

PROGRAMMAZIONE I A.A. 2017/2018

File Handling. 21 July 2009 Programming and Data Structure 1

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

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

Transcription:

Friday, September 16, 2016 Lab Notes Topics for today Redirection of input and output Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week 1. Redirection of input and output 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 1 of 5 September 16, 2016

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 2 of 5 September 16, 2016

2. Command line arguments In general, the main function can have the following heading main(int argc, char *argv[]) or 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 3 of 5 September 16, 2016

3. 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 4 of 5 September 16, 2016

#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. 4 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. You can start with a placeholder for date-checking function isvalid int isvalid (int day, int month, int year) return 1; Then fill this out for extra credit if you have time. Consider creating a file with a test sequence then use redirection as described in section 1 above. Comp 162 Lab Notes Page 5 of 5 September 16, 2016