Type Checking. Prof. James L. Frankel Harvard University

Similar documents
Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language

Intermediate Representation Prof. James L. Frankel Harvard University

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

A flow chart is a graphical or symbolic representation of a process.

Type Conversion. and. Statements

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

by Pearson Education, Inc. All Rights Reserved.

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

Contents of Lecture 3

1.3b Type Conversion

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Expressions and Precedence. Last updated 12/10/18

Pointers, Dynamic Data, and Reference Types

Data Representation and Storage. Some definitions (in C)

Fundamentals of Programming Session 20

Chapter 3 Structure of a C Program

Structures, Operators

6.096 Introduction to C++ January (IAP) 2009

A Fast Review of C Essentials Part I

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

The New C Standard (Excerpted material)

Computers Programming Course 5. Iulian Năstac

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Tokens, Expressions and Control Structures

LECTURE 3 C++ Basics Part 2

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

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

Objectives. In this chapter, you will:

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Introduction to Computer Science Midterm 3 Fall, Points

Variation of Pointers

Introduction to Programming Using Java (98-388)

Expressions and Casting

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Computers Programming Course 6. Iulian Năstac

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

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

Types, Variables, and Constants

Computer System and programming in C

Everything Else C Programming and Software Tools. N.C. State Department of Computer Science

C-LANGUAGE CURRICULAM

More about BOOLEAN issues

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Algorithms & Data Structures

Lecture 02 C FUNDAMENTALS

CSCI 171 Chapter Outlines

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Arrays and Pointers in C & C++

Data Representation and Storage

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

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Numeric Encodings Prof. James L. Frankel Harvard University

Short Notes of CS201

Fundamental of Programming (C)

Midterm CSE 131B Spring 2005

CSCE 110 PROGRAMMING FUNDAMENTALS

Programming in C++ 6. Floating point data types

The New C Standard (Excerpted material)

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

CS201 - Introduction to Programming Glossary By

CSE 431S Type Checking. Washington University Spring 2013

C Programming Multiple. Choice

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

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

Information Science 1

Organization of Programming Languages CS3200 / 5200N. Lecture 06

LECTURE 02 INTRODUCTION TO C++

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

The New C Standard (Excerpted material)

Generalized Constant Expressions

Arithmetic Operators. Portability: Printing Numbers

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

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

Infix to Postfix Conversion

Introduction to Programming (Java) 2/12

The New C Standard (Excerpted material)

Pointer Declarations

MISRA-C:2012 Standards Model Summary for C / C++

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

Polymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1

Programming. Elementary Concepts

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Computer Organization & Systems Exam I Example Questions

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

C Syntax Out: 15 September, 1995

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

a data type is Types

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam


Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

Transcription:

Type Checking Prof. James L. Frankel Harvard University Version of 7:10 PM 27-Feb-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved.

C Types C Types Type Category Type Category Type Category short, int, long, long long (signed and unsigned) Integral type Arithmetic type Scalar type char (signed and unsigned) Integral type Arithmetic type Scalar type _Bool Integral type Arithmetic type Scalar type enum { } Integral type Arithmetic type Scalar type float, double, long double Floating-point type Arithmetic type Scalar type float _Complex, double _Complex, long double _Complex, float _Imaginary, double _Imaginary, long double _Imaginary Floating-point type Arithmetic type Scalar type T * Pointer type Pointer type Scalar type T [ ] Array type Array type Aggregate type struct { } Structure type Structure type Aggregate type union { } Union type Union type Union type T ( ) Function type Function type Function type void Void type Void type Void type

Arithmetic Identifier Representation Identifier in Symbol Table Type field Arithmetic type

Pointer Identifier Representation Identifier in Symbol Table Type field Pointer to Base type field Base type

Pointer Type Representation Pointer to Base type field Base type

Pointer Type Details Pointers can point to any type Pointers are either an: Object pointer Function pointer Void pointer void * is a generic pointer Generic pointers cannot be dereferenced Null pointer is equal to the integer constant 0

Array Type Representation Array of Number of elements field Base type field Number of elements Base type

Array Type Details Arrays can be formed of any type except: void any incomplete type a function type Arrays always have a 0-origin When an array of T appears in an expression, it is converted to type pointer to T and its value is a pointer to the first element except when the array is an operand of the sizeof or address-of (&) operator

Multidimensional Array Type Details Multidimensional arrays are stored in row-major order (i.e., adjacent elements in memory differ by one in their last subscript) Conversion of array to pointer happens for multidimensional arrays as for singly dimensioned arrays, but only for the toplevel array-ness So, an expression A of type i-by-j-by- -by-k array of T is converted to type pointer to j-by- -by-k array of T

enum Types Integer values represented by identifiers Type identifiers have type int Example: enum boats { power, sail, dinghy } boat1, boat2;

Structure Types (1 of 2) Example: struct node { int value; struct node *next; } mynode; value and next are components (or members or fields) The struct node is an incomplete type from just after its appearance until the end of the complete declaration

Structure Types (2 of 2) structs may contain holes in their storage allocation components are laid out in the order in which they are declared bit fields may be specified for components in structs, as in: struct DiskReg { unsigned int ready:1; unsigned int erroroccured:1; unsigned int diskspinning:1; unsigned int writeprotect:1; unsigned int headloaded:1; unsigned int errorcode:8; unsigned int track:9; unsigned int sector:5; unsigned int command:5; };

Union Types Example: union overlay { int i; float f; char c[4]; } myoverlay; Storage is allocated for each component starting at the beginning of the union Storage is allocated for the union at an appropriate alignment for any component in the union Unions are used to support variant records Unions can be used in a non-portable way to discover the underlying representation of data

Function Types Functions cannot return arrays or functions (but can return pointers to arrays or functions) The name of a function (i.e., an expression of type function returning ) when used in an expression is converted into a pointer to function returning except when used as a function call or as the operand of address (&) or sizeof

Minimum Integer Precision and Range ( 5.1.1, p. 125 & Table 5-2, p. 127) char at least 8 bits short at least 16 bits int at least 16 bits long at least 32 bits long long at least 64 bits Concerning integer type range: Do not depend on the implementation using twos-complement representation See 5.1.1, p. 125-126 & Table 5-2, p. 127) For example, -32,768 may not be representable in 16 bits

Integral & Floating-Point Number Representations Detour to the Numeric Encodings slides

Character Representations Detour to the Character Encodings slides

Some Additional Type Names Complex types float _Complex double _Complex long double _Complex float _Imaginary double _Imaginary long double _Imaginary Non-Complex Arithmetic types are also called real types

Usual Conversions: Casting Destination (cast) type Any arithmetic type Any integer type Pointer to (object) T, or (void *) Pointer to (function) T Structure or union Array of T, or Function returning T void Permitted source types Any arithmetic type Any pointer type Any integer type, (void *), pointer to (object) Q, for any Q; pointer to (function) Q, for any Q Any integer type, pointer to (function) Q, for any Q; pointer to (object) Q, for any Q None; not a permitted cast None; not a permitted cast Any type

Usual Conversions: Assignment Left side type Any arithmetic type _Bool A structure or union type Permitted right side types Any arithmetic type Any pointer type A compatible structure or union type (void *) The constant 0, pointer to (object) T, (void *) Pointer to (object) T 1 The constant 0, pointer to T 2, where T 1 and T 2 are compatible, (void *) Pointer to (function) F 1 The constant 0, pointer to F 2, where F 1 and F 2 are compatible

Conversion Rank Rank Types of that rank 60 long long int, unsigned long long int 50 long int, unsigned long int 40 int, unsigned int 30 short, unsigned short 20 char, unsigned char, signed char 10 _Bool

Usual Unary Conversions (Choose first that applies) Operand type float Array of T Function returning T An integer of rank greater or equal to int A signed type of rank less than int An unsigned type of rank less than int, all of whose values can be represented in type int An unsigned type of rank less than int, all of whose values cannot be represented in type int Standard C converts it to (no conversion) Pointer to T Pointer to function returning T (no conversion) int int unsigned int

Usual Binary Conversions (Choose first that applies) If either operand type Other operand type Standard C converts both to long double any real type long double double any real type double float any real type float any unsigned type any unsigned type The unsigned type with the greater rank any signed type any signed type The signed type with the greater rank any unsigned type a signed type of less or equal rank The unsigned type any unsigned type any unsigned type a signed type of greater rank that can represent all values of the unsigned type a signed type of greater rank that cannot represent all values of the unsigned type The signed type The unsigned version of the signed type any other type any other type (no conversion)

Examples of Types and Conversions Names Identifiers ( 7.3.1, p. 208) Literals ( 7.3.2, p. 209) Unary minus and plus ( 7.5.3, p. 222) Assignment expressions ( 7.9, p. 246) Simple assignment ( 7.9.1, p. 247-248) Compound assignment ( 7.9.2, p. 248-249) Additive operators ( 7.6.2, p. 229-231) Logical operator expressions ( 7.7, p. 242-244)