Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Similar documents
Continued from previous lecture

The Design of C: A Rational Reconstruction: Part 2

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

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

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

2/12/17. Goals of this Lecture. Historical context Princeton University Computer Science 217: Introduction to Programming Systems

Princeton University Computer Science 217: Introduction to Programming Systems The C Programming Language Part 1

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

SWEN-250 Personal SE. Introduction to C

CSCI 171 Chapter Outlines

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Lecture 02 C FUNDAMENTALS

The Design of C: A Rational Reconstruction"

Fundamental of Programming (C)


C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

The Design of C: A Rational Reconstruction

Standard C Library Functions

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

C programming basics T3-1 -

Reserved Words and Identifiers

Work relative to other classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

Standard File Pointers

Computers Programming Course 6. Iulian Năstac

Programming for Engineers Iteration

Fundamentals of Programming

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

C-LANGUAGE CURRICULAM

SECTION II: LANGUAGE BASICS

Course Outline Introduction to C-Programming

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

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)

Lecture 03 Bits, Bytes and Data Types

The Design of C: A Rational Reconstruction

Unit 3. Operators. School of Science and Technology INTRODUCTION

File IO and command line input CSE 2451

Arithmetic Operators. Portability: Printing Numbers

ENG120. Misc. Topics

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

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

Advanced C Programming Topics

A Fast Review of C Essentials Part I

C - Basics, Bitwise Operator. Zhaoguo Wang

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

The Design of C: A Rational Reconstruction" Jennifer Rexford!

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

Flow Control. CSC215 Lecture

Input / Output Functions

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Operators. Java operators are classified into three categories:

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Your Instructor. CSE Content. Notes. Notes. Notes. Summer May 4, 2010

C Programming Language (Chapter 2 of K&R) Variables and Constants

Variables and literals

Chapter 3: Operators, Expressions and Type Conversion

C Basics And Concepts Input And Output

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

Computers Programming Course 5. Iulian Năstac

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

Chapter 7. Expressions and Assignment Statements ISBN

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Lecture 3. More About C

Programming in C and C++

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

UNIT IV INTRODUCTION TO C

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

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

Chapter 2: Basic Elements of C++

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

Language Reference Manual simplicity

Quick Reference Guide

EE 209: Programming Structures for Electrical Engineering

Chapter 7. Expressions and Assignment Statements

Programming Languages: Part 2. Robert M. Dondero, Ph.D. Princeton University

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

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Full file at

Introduction to Programming Using Java (98-388)

Structure of this course. C and C++ Past Exam Questions. Text books

File (1A) Young Won Lim 11/25/16

Values and Variables 1 / 30

C for C++ Programmers

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

ISA 563 : Fundamentals of Systems Programming

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Fundamentals of Programming

BASIC ELEMENTS OF A COMPUTER PROGRAM

Expressions & Flow Control

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Introduction to C. History of C:

Transcription:

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C C is quirky, flawed, and an enormous success. While accidents of history surely helped, it evidently satisfied a need for a system implementation language efficient enough to displace assembly language, yet sufficiently abstract and fluent to describe algorithms and interactions in a wide variety of environments. -- Dennis Ritchie 1

Goals of this Lecture Help you learn about: The decisions that were made by the designers* of C Why they made those decisions and thereby The fundamentals of C Why? Learning the design rationale of the C language provides a richer understanding of C itself A power programmer knows both the programming language and its design rationale * Dennis Ritchie & members of standardization committees 2

Goals of C Designers wanted C to: Support system programming Be low-level Be easy for people to handle But also: Support application programming Be portable Be easy for computers to handle Conflicting goals on multiple dimensions! Result: different design decisions than Java 3

Operators Issue: What kinds of operators should C have? Thought process Should handle typical operations Should handle bit-level programming ("bit twiddling") Should provide a mechanism for converting from one type to another 4

Operators Decisions Provide typical arithmetic operators: + - * / % Provide typical relational operators: ==!= < <= > >= Each evaluates to 0 FALSE, 1 TRUE Provide typical logical operators:! && Each interprets 0 FALSE, non-0 TRUE Each evaluates to 0 FALSE, 1 TRUE Provide bitwise operators: ~ & ^ >> << Provide a cast operator: (type) 5

Logical vs. Bitwise Ops Logical AND (&&) vs. bitwise AND (&) 2 (TRUE) && 1 (TRUE) => 1 (TRUE) Decimal Binary 2 00000000 00000000 00000000 00000010 && 1 00000000 00000000 00000000 00000001 ---- ----------------------------------- 1 00000000 00000000 00000000 00000001 2 (TRUE) & 1 (TRUE) => 0 (FALSE) Decimal Binary 2 00000000 00000000 00000000 00000010 & 1 00000000 00000000 00000000 00000001 ---- ----------------------------------- 0 00000000 00000000 00000000 00000000 Implication: Use logical AND to control flow of logic Use bitwise AND only when doing bit-level manipulation Same for OR and NOT 6

Assignment Operator Issue: What about assignment? Thought process Must have a way to assign a value to a variable Many high-level languages provide an assignment statement Would be more expressive to define an assignment operator Performs assignment, and then evaluates to the assigned value Allows assignment to appear within larger expressions Decisions Provide assignment operator: = Define assignment operator so it changes the value of a variable, and also evaluates to that value 7

Assignment Operator Examples Examples i = 0; /* Side effect: assign 0 to i. Evaluate to 0. j = i = 0; /* Assignment op has R to L associativity */ /* Side effect: assign 0 to i. Evaluate to 0. Side effect: assign 0 to j. Evaluate to 0. */ while ((i = getchar())!= EOF) /* Read a character. Side effect: assign that character to i. Evaluate to that character. Compare that character to EOF. Evaluate to 0 (FALSE) or 1 (TRUE). */ 8

Special-Purpose Assignment Issue: Should C provide tailored assignment operators? Thought process The construct a = b + c is flexible The construct i = i + c is somewhat common The construct i = i + 1 is very common Special-purpose operators make code more expressive Might reduce some errors May complicate the language and compiler Decisions Introduce += operator to do things like i += c Extend to -= *= /= ~= &= = ^= <<= >>= Special-case increment and decrement: i++ i-- Provide both pre- and post-inc/dec: x = ++i; y = i++; 9

iclicker Question Q: What are i and j set to in the following code? A. 5, 7 B. 7, 5 C. 7, 11 D. 7, 12 E. 7, 13 i = 5; j = i++; j += ++i;

sizeof Operator Issue: How to determine the sizes of data? Thought process The sizes of most primitive types are un- or under-specified Provide a way to find size of a given variable programmatically Decisions Provide a sizeof operator Applied at compile-time Operand can be a data type Operand can be an expression, from which the compiler infers a data type Examples, on courselab using gcc217 sizeof(int) evaluates to 4 sizeof(i) evaluates to 4 (where i is a variable of type int) 11

iclicker Question Q: What is the value of the following sizeof expression on the courselab machines? int i = 1; sizeof(i + 2L) A. 3 B. 4 C. 8 D. 12 E. error

Other Operators Issue: What other operators should C have? Decisions Function call operator Should mimic the familiar mathematical notation function(param1, param2, ) Conditional operator:?: The only ternary operator: inline if statement Example: (i < j)? i : j evaluates to min of i and j See King book for details Sequence operator:, See King book Pointer-related operators: & * Described later in the course Structure-related operators (. ->) Described later in the course 13

Operators Summary: C vs. Java Java only >>> new instanceof right shift with zero fill create an object is left operand an object of class right operand? C only -> structure member select * dereference & address of, sequence sizeof compile-time size of 14

History of programming languages: goto, if-then-else, while-do What the computer does: /* add up the first n numbers */ 1. s = 0; 2. i = 1; 3. if (i>n) goto 7 4. s = s + i; 5. i = i + 1; 6. goto 3 7. /* answer in s */ Early programming languages (1950s) s=0; i=1; LOOP: if i>n goto DONE s=s+1; i=i+1; goto LOOP; DONE:

Control Statements Algol-60 language (1960) if-then-else, while-do, for loop, goto Scientific background Boehm and Jacopini proved (1966) that any algorithm can be expressed as the nesting of only 3 control structures: Barry Boehm Sequence Selection Repetition statement1 TRUE condition FALSE TRUE FALSE condition statement2 statement1 statement2 statement 16

Control Statements (cont.) Thought Process (cont.) Dijkstra argued that any algorithm should be expressed using only those control structures (GOTO Statement Considered Harmful paper, 1968) C language design (1972) Basically follow ALGOL-60, but use { braces } instead of the more heavyweight BEGIN END syntax. Edsgar Dijkstra 17

Sequence Statement Sequence Compound statement, alias block statement1 statement2 { } statement1; statement2; 18

Selection Statements Selection if (expr) statement1; TRUE condition FALSE statement1 statement2 if (expr) statement1; else statement2; 19

Selection Statements switch and break statements, for multi-path decisions on a single integerexpr switch (integerexpr) { case integerliteral1: break; case integerliteral2: break; default: } What happens if you forget break? 20

Repetition Statements while statement; test at leading edge statement expr while (expr) statement; for statement; test at leading edge, increment at trailing edge body incr init test do while statement; test at trailing edge for (initexpr; testexpr; increxpr) bodystatement; statement expr do statement; while (expr); 21

Other Control Statements Issue: What other control statements should C provide? Decisions break statement (revisited) Breaks out of closest enclosing switch or repetition statement continue statement Skips remainder of current loop iteration Continues with next loop iteration When used within for, still executes incrementexpr goto statement grudgingly provided Jump to specified label 22

Declaring Variables Issue: Should C require variable declarations? Thought process: Declaring variables allows compiler to check spelling Declaring variables allows compiler to allocate memory more efficiently 23

Declaring Variables Decisions: Require variable declarations Provide declaration statement Programmer specifies type of variable (and other attributes too) Examples int i; int i, j; int i = 5; const int i = 5; /* value of i cannot change */ static int i; /* covered later in course */ extern int i; /* covered later in course */ 24

Declaring Variables Decisions (cont.): Unlike Java, declaration statements must appear before any other kind of statement in compound statement { } int i; /* Non-declaration stmts that use i. */ int j; /* Non-declaration stmts that use j. */ { } int i; int j; /* Non-declaration stmts that use i. */ /* Non-declaration stmts that use j. */ Illegal in C Legal in C 25

Repetition Statements Decisions (cont.) Similarly, cannot declare loop control variable in for statement { } { } for (int i = 0; i < 10; i++) /* Do something */ int i; for (i = 0; i < 10; i++) /* Do something */ Illegal in C Legal in C 26

Statements Summary: C vs. Java Java only Declarations anywhere within block Declare immutable variables with final Conditionals of type boolean Labeled break and continue No goto C only Declarations only at beginning block Declare immutable variables with const Conditionals of any type (checked for zero / nonzero) No labeled break and continue goto provided (but don t use it) 27

iclicker Question Q: What does the following code print? int i = 1; switch (i++) { case 1: printf("%d", ++i); case 2: printf("%d", i++); } A. 1 B. 2 C. 3 D. 22 E. 33

I/O Facilities Issue: Should C provide I/O facilities? Thought process Unix provides the file abstraction A file is a sequence of characters with an indication of the current position Unix provides 3 standard files Standard input, standard output, standard error C should be able to use those files, and others I/O facilities are complex C should be small/simple 29

I/O Facilities Decisions Do not provide I/O facilities in the language Instead provide I/O facilities in standard library Constant: EOF Data type: FILE (described later in course) Variables: stdin, stdout, and stderr Functions: 30

Reading Characters Issue: What functions should C provide for reading characters from standard input? Thought process Need function to read a single character from stdin Function must have a way to indicate failure, that is, to indicate that no characters remain Decisions Provide getchar() function Make return type of getchar() wider than char Make it int; that's the natural word size Define getchar() to return EOF (a special non-character int) to indicate failure Note There is no such thing as "the EOF character" 31

Writing Characters Issue: What functions should C provide for writing a character to standard output? Thought process Need function to write a single character to stdout Decisions Provide a putchar() function Define putchar() to accept one parameter For symmetry with getchar(), parameter should be an int 32

Reading Other Data Types Issue: What functions should C provide for reading data of other primitive types? Thought process Must convert external form (sequence of character codes) to internal form Could provide getshort(), getint(), getfloat(), etc. Could provide one parameterized function to read any primitive type of data Decisions Provide scanf() function Can read any primitive type of data First parameter is a format string containing conversion specifications See King book for details 33

Writing Other Data Types Issue: What functions should C provide for writing data of other primitive types? Thought process Must convert internal form to external form (sequence of character codes) Could provide putshort(), putint(), putfloat(), etc. Could provide one parameterized function to write any primitive type of data Decisions Provide printf() function Can write any primitive type of data First parameter is a format string containing conversion specifications See King book for details 34

Other I/O Facilities Issue: What other I/O functions should C provide? Decisions fopen(): Open a stream fclose(): Close a stream fgetc(): Read a character from specified stream fputc(): Write a character to specified stream fgets(): Read a line/string from specified stream fputs(): Write a line/string to specified stream fscanf(): Read data from specified stream fprintf(): Write data to specified stream Described in King book, and later in the course after covering files, arrays, and strings 35

Summary C design decisions and the goals that affected them Data types (last time) Operators Statements I/O facilities Knowing the design goals and how they affected the design decisions can yield a rich understanding of C 36

Appendix: The Cast Operator Cast operator has multiple meanings: (1) Cast between integer type and floating point type: Compiler generates code At run-time, code performs conversion f 11000001110110110000000000000000-27.375 i = (int)f i 11111111111111111111111111100101-27 37

Appendix: The Cast Operator (2) Cast between floating point types of different sizes: Compiler generates code At run-time, code performs conversion f 11000001110110110000000000000000-27.375 d = (double)f d 11000000001110110110000000000000 00000000000000000000000000000000-27.375 38

Appendix: The Cast Operator (3) Cast between integer types of different sizes: Compiler generates code At run-time, code performs conversion i 000000000000000000000000000000102 2 c = (char)i c 00000010 2 39

Appendix: The Cast Operator (4) Cast between integer types of same size: Compiler generates no code Compiler views given bit-pattern in a different way i 111111111111111111111111111111102-2 u = (unsigned int)i u 11111111111111111111111111111110 4294967294 40