The C Language Reference Manual

Similar documents
Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

The C Language COMS W Prof. Stephen A. Edwards Fall 2002 Columbia University Department of Computer Science

The C Language Prof. Stephen A. Edwards

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

A Shotgun Introduction to C

A Shotgun Introduction to C

Language Reference Manual simplicity

The Challenges of Hardware Synthesis from C-like Languages

A Fast Review of C Essentials Part I

Holtek C and ANSI C Feature Comparison User s Guide

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

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

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

Programming in C Quick Start! Biostatistics 615 Lecture 4

Chapter 1 & 2 Introduction to C Language

Programming in C and C++

Programming in C and C++

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

Typescript on LLVM Language Reference Manual

C Language, Token, Keywords, Constant, variable

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Presented By : Gaurav Juneja

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

Computers Programming Course 5. Iulian Năstac

C Programming Multiple. Choice

1 Lexical Considerations

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

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

C Reference Manual. Dennis M. Ritchie Bell Telephone Laboratories Murray Hill, New Jersey 07974

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Object-Oriented Programming

d-file Language Reference Manual

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

Programming Language Basics

Preview from Notesale.co.uk Page 6 of 52

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

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

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

BLM2031 Structured Programming. Zeyneb KURT

Data Type Fall 2014 Jinkyu Jeong

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental of Programming (C)

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Work relative to other classes

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

CSCI 171 Chapter Outlines

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

This lists all known errors in The C Programming Language, Second Edition, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988).

Lexical Considerations

C: How to Program. Week /Mar/05

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

EL2310 Scientific Programming


Chapter 2: Overview of C. Problem Solving & Program Design in C

6.096 Introduction to C++ January (IAP) 2009

YOLOP Language Reference Manual

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

CSE 374 Programming Concepts & Tools

Informática Ingeniería en Electrónica y Automática Industrial

Chapter 2 - Introduction to C Programming

Lexical Considerations

Programming in C++ 5. Integral data types

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

ECE 2400 Computer Systems Programming Fall 2018 Topic 11: Transition to C++

Fundamentals of Programming

Programming in C. What is C?... What is C?

GAWK Language Reference Manual

Lecture 03 Bits, Bytes and Data Types

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Review of the C Programming Language for Principles of Operating Systems

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

PROGRAMMAZIONE I A.A. 2017/2018

Chapter 15 - C++ As A "Better C"

Introduction to Computing Lecture 01: Introduction to C

Unit 7. Functions. Need of User Defined Functions

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Have examined process Creating program Have developed program Written in C Source code

INTRODUCTION 1 AND REVIEW

ET156 Introduction to C Programming

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CS201 Some Important Definitions

OBJECTIVE QUESTIONS: Choose the correct alternative:

Introduction to Programming Using Java (98-388)

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

Chapter 2: Basic Elements of C++

UNIT- 3 Introduction to C++

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

Transcription:

The C Language Reference Manual Stephen A. Edwards Columbia University Summer 2014 Katsushika Hokusai, In the Hollow of a Wave off the Coast at Kanagawa, 1827

Part I The History of C

C History Developed between 1969 and 1973 along with Unix Due mostly to Dennis Ritchie Designed for systems programming Operating systems Utility programs Compilers Filters Evolved from B, which evolved from BCPL

BCPL Martin Richards, Cambridge, 1967 Typeless Everything a machine word (n-bit integer) Pointers (addresses) and integers identical Memory: undifferentiated array of words Natural model for word-addressed machines Local variables depend on frame-pointer-relative addressing: no dynamically-sized automatic objects Strings awkward: Routines expand and pack bytes to/from word arrays

BCPL Example: 8 Queens GET "libhdr" GLOBAL { count:ug; all LET try(ld, row, rd) BE TEST row=all THEN count := count + 1 ELSE { LET poss = all & ~(ld row rd) WHILE poss DO { LET p = poss & -poss poss := poss - p try(ld+p << 1, row+p, rd+p >> 1) LET start() = VALOF { all := 1 FOR i = 1 TO 16 DO { count := 0 try(0, 0, 0) writef("number of solutions to %i2-queens is %i7*n", i, count) all := 2*all + 1 RESULTIS 0

C History Original machine, a DEC PDP-11, was very small: 24K bytes of memory, 12K used for operating system Written when computers were big, capital equipment Group would get one, develop new language, OS

C History Many language features designed to reduce memory Forward declarations required for everything Designed to work in one pass: must know everything No function nesting PDP-11 was byte-addressed Now standard Meant BCPL s word-based model was insufficient

Euclid s Algorithm in C int gcd(int m, int n) { int r; while ((r = m % n)!= 0) { m = n; n = r; return n; New syle function declaration lists number and type of arguments. Originally only listed return type. Generated code did not care how many arguments were actually passed, and everything was a word. Arguments are call-by-value

Euclid s Algorithm in C int gcd(int m, int n) { int r; while ((r = m % n)!= 0) { m = n; n = r; return n; n m FP PC r Ignored SP Automatic variable r Allocated on stack when function entered, released on return Parameters & automatic variables accessed via frame pointer Other temporaries also stacked

Euclid on the PDP-11.globl _gcd GPRs: r0-r7.text r7=pc, r6=sp, r5=fp _gcd: jsr r5, rsave Save SP in FP L2: mov 4(r5), r1 r1 = n sxt r0 sign extend div 6(r5), r0 r0, r1 = m n mov r1, -10(r5) r = r1 (m % n) jeq L3 if r == 0 goto L3 mov 6(r5), 4(r5) m = n mov -10(r5), 6(r5) n = r jbr L2 L3: mov 6(r5), r0 r0 = n jbr L1 non-optimizing compiler L1: jmp rretrn return r0 (n)

Euclid on the PDP-11.globl _gcd.text _gcd: jsr r5, rsave L2: mov 4(r5), r1 sxt r0 div 6(r5), r0 mov r1, -10(r5) jeq L3 mov 6(r5), 4(r5) mov -10(r5), 6(r5) jbr L2 L3: mov 6(r5), r0 jbr L1 L1: jmp rretrn Very natural mapping from C into PDP-11 instructions. Complex addressing modes make frame-pointer-relative accesses easy. Another idiosyncrasy: registers were memory-mapped, so taking address of a variable in a register is straightforward.

Part II The Design of C Taken from Dennis Ritchie s C Reference Manual (Appendix A of Kernighan & Ritchie)

Lexical Conventions Identifiers (words, e.g., foo, printf) Sequence of letters, digits, and underscores, starting with a letter or underscore Keywords (special words, e.g., if, return) C has fairly few: only 23 keywords. Deliberate: leaves more room for users names Comments (between /* and */) Most fall into two basic styles: start/end sequences as in C, or until end-of-line as in Java s //

Lexical Conventions C is a free-form language where whitespace mostly serves to separate tokens. Which of these are the same? 1+2 1 + 2 foo bar foobar return this returnthis Space is significant in some language. Python uses indentation for grouping, thus these are different: if x < 3: y = 2 z = 3 if x < 3: y = 2 z = 3

Constants/Literals Integers (e.g., 10) Should a leading - be part of an integer or not? Characters (e.g., a ) How do you represent non-printable or characters? Floating-point numbers (e.g., 3.5e-10) Usually fairly complex syntax, easy to get wrong. Strings (e.g., "Hello") How do you include a " in a string?

What s in a Name? In C, each name has a storage class (where it is) and a type (what it is). Storage classes: 1. automatic 2. static 3. external 4. register Fundamental types: 1. char 2. int 3. float 4. double Derived types: 1. arrays 2. functions 3. pointers 4. structures

Objects and lvalues Object: area of memory lvalue: refers to an object An lvalue may appear on the left side of an assignment a = 3; /* OK: a is an lvalue */ 3 = a; /* 3 is not an lvalue */

Conversions C defines certain automatic conversions: A char can be used as an int int and char may be converted to float or double and back. Result is undefined if it could overflow. Adding an integer to a pointer gives a pointer Subtracting two pointers to objects of the same type produces an integer

Expressions Expressions are built from identifiers (foo), constants (3), parenthesis, and unary and binary operators. Each operator has a precedence and an associativity Precedence tells us 1 * 2 + 3 * 4 means (1 * 2) + (3 * 4) Associativity tells us 1 + 2 + 3 + 4 means ((1 + 2) + 3) + 4

C s Operators in Precedence Order f(r,r,...) a[i] p->m s.m!b ~i -i ++l --l l++ l-- *p &l (type) r sizeof(t) n * o n / o i % j n + o n - o i << j n < o i >> j n > o n <= o n >= o r == r r!= r i & j i ^ j i j b && c b c b? r : r l = r l += n l -= n l *= n l /= n l %= i l &= i l ^= i l = i l <<= i l >>= i r1, r2

Declarators Declaration: string of specifiers followed by a declarator basic type {{ static unsigned {{ int (*f[10])(int, char*); {{ specifiers declarator Declarator s notation matches that of an expression: use it to return the basic type. Largely regarded as the worst syntactic aspect of C: both pre- (pointers) and post-fix operators (arrays, functions).

Storage-Class Specifiers auto static extern register Automatic (stacked), default Statically allocated Look for a declaration elsewhere Kept in a register, not memory C trivia: Originally, a function could only have at most three register variables, may only be int or char, can t use address-of operator &. Today, register simply ignored. Compilers try to put most automatic variables in registers.

Type Specifiers int char float double struct { declarations struct identifier { declarations struct identifier

Declarators identifier ( declarator ) Grouping declarator () Function declarator [ optional-constant ] Array * declarator Pointer C trivia: Originally, number and type of arguments to a function wasn t part of its type, thus declarator just contained (). Today, ANSI C allows function and argument types, making an even bigger mess of declarators.

Declarator syntax Is int *f() a pointer to a function returning an int, or a function that returns a pointer to an int? Hint: precedence rules for declarators match those for expressions. Parentheses resolve such ambiguities: int *(f()) int (*f)() Function returning pointer to int Pointer to function returning int

Statements expression ; { statement-list if ( expression ) statement else statement while ( expression ) statement do statement while ( expression ); for ( expression ; expression ; expression ) statement switch ( expression ) statement case constant-expression : default: break; continue; return expression ; goto label ; label :

External Definitions A C program consists of a sequence of external definitions Functions, simple variables, and arrays may be defined. An external definition declares an identifier to have storage class extern and a specified type

Function definitions type-specifier declarator ( parameter-list ) type-decl-list { declaration-list statement-list Example: int max(a, b, c) int a, b, c; { int m; m = (a > b)? a : b ; return m > c? m : c ;

More C trivia The first C compilers did not check the number and type of function arguments. The biggest change made when C was standardized was to require the type of function arguments to be defined: Old-style int f(); int f(a, b, c) int a, b; double c; { New-style int f(int, int, double); int f(int a, int b, double c) {

Data Definitions type-specifier init-declarator-list ; declarator optional-initializer Initializers may be constants or brace-enclosed, comma-separated constant expressions. Examples: int a; struct { int x; int y; b = { 1, 2 ; float a, *b, c;

Scope Rules Two types of scope in C: 1. Lexical scope Essentially, place where you don t get undeclared identifier errors 2. Scope of external identifiers When two identifiers in different files refer to the same object. E.g., a function defined in one file called from another.

Lexical Scope Extends from declaration to terminating or end-of-file. int a; int foo() { int b; if (a == 0) { printf("a was 0"); a = 1; b = a; /* OK */ int bar() { a = 3; /* OK */ b = 2; /* Error: b out of scope */

External Scope file1.c: int foo() { return 0; int bar() { foo(); /* OK */ file2.c: int baz() { foo(); /* Error */ extern int foo(); int baff() { foo(); /* OK */

The Preprocessor Violates the free-form nature of C: preprocessor lines must begin with #. Program text is passed through the preprocessor before entering the compiler proper. Define replacement text: # define identifier token-string Replace a line with the contents of a file: # include " filename "

C s Standard Libraries <assert.h> Generate runtime errors assert(a > 0) <ctype.h> Character classes isalpha(c) <errno.h> System error numbers errno <float.h> Floating-point constants FLT_MAX <limits.h> Integer constants INT_MAX <locale.h> Internationalization setlocale(...) <math.h> Math functions sin(x) <setjmp.h> Non-local goto setjmp(jb) <signal.h> Signal handling signal(sigint,&f) <stdarg.h> Variable-length arguments va_start(ap, st) <stddef.h> Some standard types size_t <stdio.h> File I/O, printing. printf("%d", i) <stdlib.h> Miscellaneous functions malloc(1024) <string.h> String manipulation strcmp(s1, s2) <time.h> Time, date calculations localtime(tm)

Language design Language design is library design. Bjarne Stroustroup Programs consist of pieces connected together. Big challenge in language design: making it easy to put pieces together correctly. C examples: The function abstraction (local variables, etc.) Type checking of function arguments The #include directive