CSE 12 Spring 2018 Week One, Lecture Two

Similar documents
CSE 12 Spring 2016 Week One, Lecture Two

211: Computer Architecture Summer 2016

Input / Output Functions

CSE 12 Week Three Lecture One Tomorrow s discussion: Getting started on hw4

C programming basics T3-1 -

File IO and command line input CSE 2451

Standard C Library Functions

CS2141 Software Development using C/C++ C++ Basics

System Software Experiment 1 Lecture 7

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

PROGRAMMAZIONE I A.A. 2017/2018

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Programming refresher and intro to C programming

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Lecture 03 Bits, Bytes and Data Types

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

CSE 303: Concepts and Tools for Software Development

CA341 - Comparative Programming Languages

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

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

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

Work relative to other classes

Standard I/O in C and C++

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

SWEN-250 Personal SE. Introduction to C

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

CSC 1600 Memory Layout for Unix Processes"

C mini reference. 5 Binary numbers 12

Text Output and Input; Redirection

Continued from previous lecture

Course organization. Course introduction ( Week 1)

The Design of C: A Rational Reconstruction (cont.)

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Standard File Pointers

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

M1-R4: Programing and Problem Solving using C (JULY 2018)

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS240: Programming in C

CSE 333 Lecture 9 - intro to C++

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)

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

CSE 410: Systems Programming

C++ Programming Basics

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

The Design of C: A Rational Reconstruction: Part 2

CS & IT Conversions. Magnitude 10,000 1,

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Final CSE 131B Spring 2004

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

PRINCIPLES OF OPERATING SYSTEMS

Intermediate Programming, Spring 2017*

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

The C++ Language. Arizona State University 1

Lecture 7: file I/O, more Unix

C Basics And Concepts Input And Output

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

Binary Representations and Arithmetic

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Inf2C - Computer Systems Lecture 2 Data Representation

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Today s Learning Objectives

UNIX input and output

IO = Input & Output 2

Presented By : Gaurav Juneja

CSE 12 Week Eight, Lecture One

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

Programming & Data Structure

ECE551 Midterm Version 1

Representation of Information

Variables and literals

Simple Output and Input. see chapter 7

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

File Handling in C. EECS 2031 Fall October 27, 2014

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Organization of a file

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

Reserved Words and Identifiers

CS Programming In C

Introduction to Computing Lecture 03: Basic input / output operations

Result: original lp in main is. Goal was.

Fundamentals of Programming

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Fundamental of Programming (C)

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

CSE 30 Spring 2007 Final Exam

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

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

BINARY SYSTEM. Binary system is used in digital systems because it is:

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

Transcription:

CSE 12 Spring 2018 Week One, Lecture Two Homework One and Two: - Introduction to C - Review of basic programming principles - Building from fgetc and fputc - Input and output strings and numbers - Introduction to pointers - Emphasize limits (arrays bounds) Hello World: //Java: public class Driver { public static void main (String [] args) { } } System.out.println( Hello World\n ); C++: #include <iostream> using namespace std; int main (int argc, char * const * argv) { } cout << Hello World\n ; C: #include <stdio.h> int main (int argc, char * const * argv) { } fprintf (stdout, Hello World\n );

Standard output: The regular display output by your program. Java: System.out.println C++: cout << sdklfjsdlkf ; C: fprinft. fputc, fputs, printf Display is buffered: NOT displayed immediately to the user. Certain events must happen for the display to show: 1. Explicit flush (uncommon) 2. Print a newline ( \n ) - common 3. Program ends normally - uncommon 4. Request input from the user - common 5. Buffer gets full - uncommon When is display to standard output lost: Ex: When is the following output lost: fprintf (stdout, Hello World ); Ans: 1. Infinite loop while (1) { 2. Run time error after line of code: if (aaa/0 == 3. Maybe line never executed Standard error: The display for Java: C++: C: Display is : displayed immediately to the user When is display to standard output lost: Ex: When is the following output lost: fprintf (stderr, Hello World ); Ans: 1.

Numbering Systems: (Why: ) Decimal Hexadecimal Octal Binary Base Base Base Base prefix needed prefix required prefix required Not representable Digits: Digits: Digits: Digits:

Standard input: The normal input from for applications. Java: C++: C: Buffered input: (2 buffers: 1 keyboard buffer, 2 standard input buffer) Keyboard buffer: processes all input (backspaces finish keyboard input: \n ) Certain event must happen for the input to get into the standard input buffer: 1. Ex: A - A goes in the buffer, standard input is B - B goes in the buffer, standard input is C - C goes in the buffer, standard input is <ret> - goes into the buffer, keyboard buffer is. How to get input in C: char character = NULL; while (character!= \n ) { character = fgetc (stdin); } // <ret> Hexadecimal: Mapping of binary easy way to 32 bits = 1 word (size of int, long (C/C++) reading direct contents of memory. Largest Number in 32 bits: - Most significant bit (left most bit: 0 = positive, 1 = negative) Largest Number in 32 bits: - unsigned number: all bits are no bit

The Apple Analogy: 1 distinct plate of 10 decimal apples another distinct plate of 012 octal apples another distinct plate of 1010 binary apples another distinct plate of 0xa hexadecimal apples Put them all in one bag how many do you have? Answer: apples! ASCII Codes: American Standard Codes for Information Interchange - displayed to the user - input from the keyboard Example keyboard input: 123\n Example string in C/C++ 123\n NULL = \0 = 0 = 0x0 = the last character in every. Newline = \n = 10 = 0xa = the last character.

Standard input is when program begins therefore, a call to fgetc when stdin is empty caused your program to:. Once standard input has contents, then fgetc will and it will return. Exception: Control D input 1) It s the input to end all test cases in the autograder. 2) Your execution score will be 0 if you fail to handle ^D. 3) It s the indication that you are done with your application. 4) To enter End of File also known as EOF. 5) Side note: ^D is in UNIX what ^Z is on Windows. (Do not ^Z on UNIX). Works only when stdin is Input is: ^D Not: 123^D 123^D<ret> ^D<ret>

Numeric input: C: unsigned long ul; // always positive, 32 bits of magnitude signed long sl; // 1 sign bit, 31 bits of magnitude The computer represents negative numbers in a notation called: Two s complement Two s complement is both an algorithm and a representation - How: o Binary number: flip all bits o Add one Ex: 1 in decimal is 0000000000.000001 in 32 bits in binary How do you represent -1 in two s complement? Here s how: 00000000..00000001: start with a positive number 11111111..11111110: flip +1: add one_ ----------------------------- Ans: Ex: Given an unknown negative number, what it its magnitude? 11111111..11111111: -??? 00000000..00000000: flip +1: add one ------------------------------

Therefore: going positive to negative, negative to positive is the. 111111111 11111111: -1 + 000000000 00000001: +1 ----------------------------- Observation:. Is this a problem? Ans:. Ex in 4 bits, signed addition: 1 bit of sign, 3 bits of magnitude 0101: 5 + 0110: +6 ------- -- Problem: Overflow a bit of magnitude or. 0100: flip + 1: add one ------- Observation: overflow occurred: o o o When a computer program produces overflow what happens? Ans: Ramifications:.

Layout of memory: (You need to know where are the variables that you use, how long they exist, what other variables are nearby). Text: Where your program resides while it is executing. - Function/methods definitions, while loops, if statements, constant strings - Read only Data: - global memory, static variables - created when your program is loaded into memory - go away when your program ends - 0 initialized Stack: (Run-Time Stack RTS) - local variables, parameters, return results (user controlled) - old registers (whatever that is) (by the operating system) - return addresses (whatever that is) (by the operating system) - created when your function/method is called - go away when your function/method ends - uninitialized initialized with contents from the last use of space Heap: - dynamic memory (memory allocated at run-time) - C: malloc (m:memory, alloc: allocate), free frees up the memory - C++: new creates, delete frees up the memory - Java: new creates, garbage collector frees up the memory - C/C++ if you don t free your memory memory leak bad thing!

CSE 12, Week One Discussion Two (hw2) Thoughts to consider for hw2: getline: - loop of: character = - what type should character be and why? o character; Ans: character = fgetc (stdin); // in a loop - the loops stops in three ways: 1. 2. 3. The means while ( ) Problems: 1. 2. Advice:

clrbuf: - if we enter 99 characters and \n then call getline, how many characters remain in stdin without the presence of clrbuf to fill an array where maxlength is 80 characters? - Ans: decin: - To check user input: 0 9 from raw input from user - Or: 0 9 after the ASCII to digit conversion Avoid MAGIC NUMBERS hard coded in your program - 0x30 0x39 (bad) - 48 57 (bad) - 10 is not magical in decin/decout - 16 is not magical in hexout - you should use #define constants instead of magic numbers o #define ASCII_ZERO 0 o #define ASCII_NINO 9 o use: ASCII_ZERO ASCII_NINE o using 0 or 9 is okay, 49 or 57 is not User input: 1 2 3 \n.. 123 (ASCII conversion plus combine digits) Same as: 0x31 0x32 0x33 0xa Same as: 49 50 51 10 user input desired value 1 0 = 1 (subtract ASCII code for 0 from user input) 49 48 = 1 (subtract ASCII code for 0 from user input) 0x31 0x30 = 1 (subtract ASCII code for 0 from user input) How to you put it all together? We need a loop: // processing 1 // processing 2 // processing 3 // processing \n Overflow: how to detect: -