DOC PREVIEW
Princeton COS 217 - Types

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Types Page 26 September 16, 1997 Types • The type of an object determinesthe values it can have and the operations that can be performed on it • Basic types char a “character”; typically a “byte” int an integer; typically a “word” float single-precision floating point double double-precision floating point • int qualifiers (optional) short int “smaller” intlong int “bigger” int , but not double precision • Unsigned integers: non-negative modulo where n is #bits/integer unsigned int unsigned short int unsigned char • Is char signed or unsigned? 2 nCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Type Sizes Page 27 September 16, 1997 Type Sizes Note: C did not exist in 1964; this table just reflects typical sizes year 72–81 80–92 64–92 93–?computer DEC-10 PCsIBM360VAX68020SPARCMIPSR4000DEC Alpha char 7 8 8 8 short 18 16 16 16,32 int 36 16,32 32 32,64 type long 36 32 32 64 float 36 32 32 32 double 72 64 64 64 pointer 18 16, 32 32 64Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Types of Constants Page 28 September 16, 1997 Types of Constants char 'a' character constant (single quote) '\035' character code 35 octal '\x29' character code 29 hexadecimal '\t' tab ( '\011' , do “ man ascii ” for details) '\n' newline ( '\012' ) '\\' backslash '\'' single quote '\b' backspace ( '\010' ) '\0' null character int 156 decimal constant 0234 octal 0x9c hexadecimal long 156L156l for sanity, use upper-case L float 15.6f1.56e1F double 15.6 “plain” floating point constants are double s15.6L15.6lCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Constant Expressions Page 29 September 16, 1997 Constant Expressions • Const qualifier identifies read-only variables const double Pi = 3.14159;const double TwoPi = 2*3.14159; • Constant expressions are evaluated at compile time int p = 1 - 1;int p = 1/0, x = 1 ? 0 : 1/0; • Use constant expressionsto reduce the number of #define constantsto increase readabilityto improve changeability, e.g. #define MAXLINE 120...char buf[2*MAXLINE + 1];Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Arrays Page 30 September 16, 1997 Arrays • Array declarations specify the number of elements, not the upper bound int digits[10]; digits is an array of 10 int s digits[0], digits[1], ..., digits[9] • Arrays may be indexed by any integer expression digits[f(x)/2 + BASE] • N o bounds checking! • Multi-dimensional arrays float matrix[3][4][5] a 3-dimensional array with elements • Arrays are stored in row-major order ; last subscript varies “fastest” matrix[0][0][0], matrix[0][0][1], ... 3 4 5 60=Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Strings & Initialization Page 31 September 16, 1997 Strings & Initialization • “Strings” are arrays of characters " hello\n "the compiler always provides a terminating '\0' • Array length can be derived from initialization char hello[] = "hello\n"; is equivalent to char hello[7] = "hello\n";char hello[7] = { 'h', 'e', 'l', 'l', 'o', '\n', '\0' } • Ditto for arrays int x[] = { 1, 2, 3 };int y[][3] = {{ 1, 3, 5 },{ 2, 4, 6 },{ 3, 5, 7 },{ 4, 6, 8 }}; see K&R, sections 2.4 & 4.9 for more information 'h' 'e' 'l' 'l' 'o' '\12' '\0' these braces can be omittedwill be 4 — number of 3-element rowsCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Enumerations Page 32 September 16, 1997 Enumerations • Enumerations associate constant values with identifiers enum boolean { NO, YES };enum color { RED, GREEN, BLUE }; • Values are generated and may be printed symbolically by debuggers • Values can be given and unspecified ones automatically continue enum escapes { BELL='\a', BACKSPACE='\b', TAB='\t'}; enum months { Jan=1, Feb, Mar, Apr, May, Jun, Jul,Aug, Sep, Oct, Nov, Dec }; • enum identifiers are int constants, but enumeration type may take less space sizeof NO is 4 bytes enum boolean flag; may occupy 1–4 bytes • enum identifiers should have no conflicts • What is the difference between enum and


View Full Document

Princeton COS 217 - Types

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?