What did we talk about last time? Selection Loops Lab 3

Similar documents
CSE 333 SECTION 3. POSIX I/O Functions

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Operating systems. Lecture 7

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

CSE 410: Systems Programming

ECEN 449 Microprocessor System Design. Review of C Programming

CSE 333 SECTION 3. POSIX I/O Functions

CS240: Programming in C

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

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

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

Lecture 03 Bits, Bytes and Data Types

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Iteration. Side effects

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

All the scoring jobs will be done by script

The four laws of programs

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Chapter 10. The UNIX System Interface

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Input and Output System Calls


Section 3: File I/O, JSON, Generics. Meghan Cowan

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Naked C Lecture 6. File Operations and System Calls

CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

A brief introduction to C programming for Java programmers

File Descriptors and Piping

Flow Control. CSC215 Lecture

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

UNIX System Calls. Sys Calls versus Library Func

PROGRAMMAZIONE I A.A. 2017/2018

COMP 110/L Lecture 10. Kyle Dewey

Variables and literals

Operating System Labs. Yuanbin Wu

CS 201. Files and I/O. Gerson Robboy Portland State University

C for C++ Programmers

The C standard library

Operating System Labs. Yuanbin Wu

Fall 2015 COMP Operating Systems. Lab #3

Fundamentals of Programming Session 12

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

All the scoring jobs will be done by script

Learning to Program with Haiku

Process Creation in UNIX

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Introduction. C provides two styles of flow control:

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

CSI 402 Lecture 2 Working with Files (Text and Binary)

Recitation 8: Tshlab + VM

Lecture 8: Structs & File I/O

Operating System Labs. Yuanbin Wu

Module 4: Decision-making and forming loops

Continued from previous lecture

BLM2031 Structured Programming. Zeyneb KURT

Process Management! Goals of this Lecture!

CS 253: Intro to Systems Programming 1/21

ELEC 377 Operating Systems. Week 4 Lab 2 Tutorial

CSE 333 Lecture 16 - network programming intro

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

CSC 271 Software I: Utilities and Internals

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

8. Characters and Arrays

The C Programming Language Guide for the Robot Course work Module

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Files and the Filesystems. Linux Files

Chapter 7 Functions. Now consider a more advanced example:

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

File IO and command line input CSE 2451

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CSC209 Fall Karen Reid 1

Final CSE 131B Spring 2004

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Processes. Johan Montelius KTH

My malloc: mylloc and mhysa. Johan Montelius HT2016

A process. the stack

CS C Primer. Tyler Szepesi. January 16, 2013

Introduction to C. CS2023 Winter 2004

The course that gives CMU its Zip! I/O Nov 15, 2001

CS113: Lecture 4. Topics: Functions. Function Activation Records

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Operating Systems, laboratory exercises. List 2.

This is CS50. Harvard University Fall Quiz 0 Answer Key

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

CS 11 C track: lecture 8

Transcription:

Week 4 - Monday

What did we talk about last time? Selection Loops Lab 3

Unix was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things. Doug Gwyn

The break command is a necessary part of the functioning of a switch statement But, it can also be used to jump out of a loop Whenever possible (i.e. always), it should not be used to jump out of a loop Everyone once in a while, it can make things a little clearer, but usually not Loops should have one entry point and one exit for(value = 3; value < 1000; value += 2) { if(!isprime(value) ) break; for(value = 3; value < 1000 && isprime(value); value += 2) {

The continue command is similar to the break command It will cause execution to jump to the bottom of the loop If it is a for loop, it will execute the increment For all loops, it will return to the top if the condition is true It makes things easier for the programmer up front, but the code becomes harder to follow The effect can be simulated with careful use of if statements

A goto command jumps immediately to the named label Unlike break and continue, it is not a legal command in Java Except in cases of extreme (EXTREME) performance tuning, it should never be used Spaghetti code results for(value = 3; value < 1000; value += 2) { if(!isprime(value) ) goto stop; printf("loop exited normally.\n"); stop: printf("program is done.\n");

A system call is a way to ask the kernel to do something Since a lot of interesting things can only be done by the kernel, system calls must be provided to programmers via an API When making a system call, the processor changes from user mode to kernel mode There is a fixed number of system calls defined for a given system

The most common implementation of the Standard C Library is the GNU C Library or glibc Some of the functions in the glibc perform systems calls and some do not There are slight differences between the versions of the glibc Microsoft also has an implementation of the Standard C Library that doesn't always behave the same

There are no exceptions in C Instead, when a system call fails, it usually returns -1 To find out why the system call failed First, make sure you #include <errno.h> Then check the value of the integer errno in your program after the system call fails Use the man pages to determine what a given value of errno means The perror() function is often used to print errors instead of printf() It sends the output to stderr instead of stdout

#include <stdio.h> #include <fcntl.h> #include <errno.h> int main(){ int fd = open("eggplant.txt", O_WRONLY O_CREAT O_EXCL); if (fd == -1) { perror("failure to create file: "); if( errno == EACCES ) perror("insufficient privileges\n"); else if( errno == EEXIST ) else return 0; perror("file already exists\n"); perror("unknown error\n"); exit(exit_failure);

C has a feature called typedef which allows a user to give a new name to a type System types are often created so that code is portable across different systems The most common example is size_t, which is the type that specifies length It's usually the same as unsigned int There are named types for process IDs (pid_t), group IDs (gid_t), user IDs (uid_t), time (time_t), and many others

type name( arguments ) { statements

You don't have to specify a return type But you should int will be assumed if you don't If you start calling a function before it has been defined, it will assume it has return type int and won't bother checking its parameters

Because the C language is older, its compiler processes source code in a simpler way It does no reasonable typechecking if a function is called before it is defined To have appropriate typechecking for functions, create a prototype for it Prototypes are like declarations for functions They usually come in a block at the top of your source file

Parameter names in the prototype are optional (and don't have to match) Both of the following work: int root(int); int root(int blah); You can also declare a prototype locally (inside a function), but there isn't a good reason to do so #include <stdio.h> int root(int value); //integer square root int main() { int output = root(19); printf("value: %d\n", output); return 0; int root(int value) { int i = 0; while( i*i <= value ) i++; return i 1;

If your method takes nothing, you should put void in the argument list of the prototype Otherwise, type checking is turned off for the arguments double stuff(); int main() { double output = stuff(6.4, "bang"); //legal return 0; double stuff(void); int main() { double output = stuff(6.4, "bang"); //error return 0;

C does not force you to return a value in all cases The compiler may warn you, but it isn't an error Your function can "fall off the end" Sometimes it works, other times you get garbage int sum(int a, int b) { int result = a + b; return result; int sum(int a, int b) { int result = a + b;

Let's write a function that: Takes an unsigned integer as a parameter Returns the location of the highest 1 bit in the integer (0-31) or -1 if the integer is 0 Parameter Return Value 0-1 2 1 3 1 2000 10 4294967295 31

Let's update the function from the lab so that it can read negative integers too int readint() { int c = 0; int i = 0; while( (c = getchar())!= EOF && c!= '\n' ) { if( c >= '0' && c <= '9') i = i * 10 + (c - '0'); return i;

More on functions Variable scope

Keep reading K&R chapter 4 Start working on Project 2 Get your teams sorted out by the end of the day!