Today s Learning Objectives

Similar documents
Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Lecture 03 Bits, Bytes and Data Types

File IO and command line input CSE 2451

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

File I/O. Arash Rafiey. November 7, 2017

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Understanding Pointers

C programming basics T3-1 -

Organization of a file

Lecture 04 Introduction to pointers

211: Computer Architecture Summer 2016

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

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

25.2 Opening and Closing a File

2009 S2 COMP File Operations

today cs3157-fall2002-sklar-lect05 1

Introduction to file management

Recitation 2/18/2012

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

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

CSE 12 Spring 2016 Week One, Lecture Two

Standard File Pointers

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

CS C Primer. Tyler Szepesi. January 16, 2013

MA 511: Computer Programming Lecture 15: File & command line parameters. Partha Sarathi Mandal

High Performance Programming Programming in C part 1

EL2310 Scientific Programming

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

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

M.CS201 Programming language

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

MPATE-GE 2618: C Programming for Music Technology. Syllabus

A Crash Course in C. Steven Reeves

PROGRAMMAZIONE I A.A. 2017/2018

CSCI 171 Chapter Outlines

Lectures 5-6: Introduction to C

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

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Engineering program development 7. Edited by Péter Vass

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

EM108 Software Development for Engineers

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

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

Lectures 5-6: Introduction to C

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Pointers and File Handling

Programming & Data Structure

Dynamic memory allocation

Final CSE 131B Spring 2004

Recitation: C Review. TA s 20 Feb 2017

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Programming refresher and intro to C programming

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Input/Output and the Operating Systems

File Descriptors and Piping

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CSE 333 Midterm Exam July 24, Name UW ID#

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

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

Input/Output: Advanced Concepts

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

CSE 12 Spring 2018 Week One, Lecture Two

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

Few reminders and demos

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

EL2310 Scientific Programming

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Friday, February 10, Lab Notes

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Carnegie Mellon. Cache Lab. Recitation 7: Oct 11 th, 2016

Goals of this Lecture

Computer System and programming in C

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

8. Characters, Strings and Files

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

CSE 333 SECTION 3. POSIX I/O Functions

These are reserved words of the C language. For example int, float, if, else, for, while etc.

ENG120. Misc. Topics

Chapter 14 - Advanced C Topics

Computer Programming Unit v

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

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

System Software Experiment 1 Lecture 7

Advanced C Programming Topics

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

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.

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Project 1: How to Make One Dollar

Input / Output Functions

Lecture 05 Pointers ctd..

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

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

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

UNIT III (PART-II) & UNIT IV(PART-I)

Intermediate Programming, Spring 2017*

Transcription:

Today s Learning Objectives 15-123 Systems Skills in C and Unix We will Review ints and modular arithmetic Learn basic Data types and Formats How Conditionals and loops work How Arrays are defined, accessed, and stored How Functions work How Strings are defined, accessed, changed How to perform File I/O We will show some code Disclaimer:All of these topics need deeper investigation. We will do that as the course progresses Announcements Finding unix/c help Unix man pages is your friend > man ls > man stdio.h Fill out the background survey and complete prior knowledge salon (if you have not done so) Start working on Lab1 Salon (graded) Due Friday 1/21/11 Think about Lab 1 Due Sunday 1/23/11 (graded) SL1 is due today 1/18/11 Tuesday (graded) Asciitable on course web site From last lecture Representing integers in base b Modular arithmetic x = y mod (n) n / (x-y) 2 s compliment representation of negative numbers Ranges max, min for types int, short and long max for unsigned int, short and long Representing integers in base b Efficient calculation of a sum given the digits and the base Modular Arithmetic integers and ints Not the same ints have fixed precision Machines have word length intsperform all operations +, -, /, * in 32-bit (or 64-bit) Definition of x = y mod (n) What does it mean to say x + y = 0 mod (n)? 1

Modular Table (4-bits) 16 = 0 mod(16) -16 = 0 mod (16) 17 = 1 mod(16) -15 = 1 mod (16) 18 = 2 mod (16) Also we can write -1 = 15 mod (16) Hence in modular [16] arithmetic -1 = 15 So what is -0 in modular [16] arithmetic? 2 s compliment representation of negative numbers Definition of One s compliment Two s compliment Binary additions Computers can perform subtraction using 2 s compliment. Two s compliment arithmetic works and efficient way to deal with negative numbers max/min of unsigned/signed ints signed 32-bits ints max min unsigned 32-bit ints max min Brief intro Conditionals & Loops if (condition) { if (condition) { else { if (condition) { else if (condition) { else { switch (condition) { case 1 : ; break; case 2 :.; break;.. default:.; Careful of alignment Break is important More efficient than if-else in some cases. Why? for (initial condition; exit condition; loop control) { loop body while (condition) { loop body Loop semantics for (inti=0; i<n; i++) { loop body i=0; while (i<n) {.; i++; Assuring that loop ends 2

Print Formats Writing output to STDOUT/File Prototype intprintf( const char * format,... ); intfprintf(file*, const char * format,... ); Example printf( This is a test %d %.4f %10.2f %c\n,134,56.455, 3355.5346, 65); File output FILE* fp = fopen( filename, w ); fprintf(fp, This is a test %d %.4f %10.2f %c\n,134,56.455, 3355.5346, 65); Source: cplusplus.com Reading Data scanf( %d, &x); int getchar(void) getcharreturns the ASCII value of the next input character or EOF, a constant defined in stdio.h sscanf. The prototype for sscanf is: int sscanf(char*s,char* format, arg1, arg2, ) Reading from a file FILE* fp= fopen( filename, r ); intx; while (fscanf(fp, %d, &x)>0) { See man fscanf (on how to use fscanf) What are the arguments? What does it return? Writing to a file FILE* fp= fopen( filename, w ); /* writing from an array to a file */ for (inti=0; i<n; i++) fprintf(fp, %d, A[i]); See man fprintf What are the arguments? What does it return? Arrays in C Declarations <type> arrayname[size]; inta[ ] = {1,2,3; /* implicit size definition*/ char A[10]; Name of the array is the address of the first element of the array (a const pointer) Access A[i] = 10; printf( %d, A[i]); Is it possible to say: A = {1,2,3;? Arrays are statically allocated Fixed size Resizing arrays will be discussed later 3

2D Arrays <type> nameofarray[rows][cols]; Arrays are allocated as a block of m*n*sizeof(type) bytes. Consider: int A[5][2]; Strings in C A valid C String is an array of char s ending with NULL character \0 G U N A \0 C strings are mutable Functions <return_type> function_name(type1 n1, type2 n2,.); Suppose swap is a function defined as follows void swap(intx, inty){ inttmp= x; x = y; y = tmp; int x=10, y=15; swap(x,y); /* what are the values of x and y */ How functions work int main(int argc, char* argv[]){ int x=10, y=15; swap(x,y,); /* next instruction */ void swap(intx, inty){ inttmp= x; x = y; y = tmp; Not all functions are created equal Functions have overhead Runtime stack use Some functions are straight forward and some are not Inductive definition of power(x,y) Fast algorithms for computing power(x,y) Function efficiency matters in this course Runtime stack 4

Coding Examples 5