types
play

Types Purpose of types for variables and constants Tell compiler - PDF document

Types Purpose of types for variables and constants Tell compiler appropriate amount of space to Principles of Computer Science II reserve in memory Nadeem Abdul Hamid Allow compiler to use proper machine instructions to carry out


  1. Types  Purpose of types for variables and constants • Tell compiler appropriate amount of space to Principles of Computer Science II reserve in memory Nadeem Abdul Hamid • Allow compiler to use proper machine instructions to carry out operations CSC121A - Spring 2005  Expressions (made up of constants, Lecture Slides 13 - Data Types variables, fn calls) also have a value and type 2 Characters Basic C Data Types  Any integral type can be used to represent a character • Constants such as 'a' and '+' are of type int not char  Each char is stored in one byte of memory (usually 8 bits)  At the bit level: char c = 'a'; 0 1 1 0 0 0 0 1 Floating point types  One byte (8 bits) can store 256 distinct values Integral types  Type char is equivalent to either signed char or unsigned char Signed char range: -128 … 127 • 3 Unsigned char range: 0 … 255 4 • Data Type int Types short , long , unsigned  The principal working type of C  Intended for specialized use  The default type of integers worked with on a machine  Storage is a concern: use short  Typically… • Compiler may provide less storage for a short than an 2 bytes (16 bits) on personal computers, or • int (not required to do so) 4 bytes (32 bits) on high-end workstations/mainframes •  In 2 bytes the range is: -32768 … 32767  Large integer values needed: use long  In 4 bytes the range is: -2147483648 … 21477483647  Typically  * Be careful about integer overflow in programs short = 2 bytes •  Besides decimal integer constants, also long = 4 bytes • Hexadecimal: 0xa1 •  unsigned : to store integer values without a sign Octal: 0377 •  Note: 11 != 011 /* with leading zero is octal constant */ 5 6 1

  2. Floating Point Types Types of Constants  C’s default floating type is double  On 2-byte int machine, compiler treats • Usually, float stored in 4 bytes • About 6 decimal places of accuracy • 32000 as int double stored in 8 bytes • • 33000 as long • About 15 decimal places of accuracy  Precision: number of significant decimal places  Programmer can append suffixes to specify  Range: limits of largest and smallest values types of integer constants  Example: float precision is about 6 and range is • 37u or 37U - unsigned approx. 10 -38 to 10 +38 • 37l or 37L - long 0.d 1 d 2 d 3 d 4 d 5 d 6 x 10 n /* -38 <= n <= 38 */ • • 37ul or 37UL - unsigned long  double precision is about 15 places and range is approx. 10 -308 to 10 +308 7 8 sizeof Operator Floating Point on the Computer  Looks like a function, but is an operator  Not all real numbers are representable sizeof a + b == sizeof(a) + b •  Returns an integer (usually unsigned ) representing using “floating point” types number of bytes needed to store the object (or an  Floating point arithmetic (unlike integer object of that type) in memory arithmetic) may not be exact  Guarantees sizeof(char) == 1 • For large computations, especially rounding sizeof(short) <= sizeof(int) <= sizeof(long) effects, etc. need to be taken into account sizeof(signed) == sizeof(unsigned) == sizeof(int) sizeof(float) <= sizeof(double) <= sizeof(long double) (numerical analysis) • Sizeof program… • See float.h and limits.h (pg.528) for some 9 10 predefined constants Look over in textbook Enumeration Types (Ch. 7)  User-defined types to name a finite set of  Section 6.8 (Mathematical functions) elements (essentially represented as integers)  Section 6.9 (Conversions and casts)  Enumerators (elements) can be initialized  6.12 (Common programming errors) explicitly • Repeated values ok, but identifiers must be unique  6.13 (System considerations)  Variables can be declared at the same time as the enum declaration  In general, used as programmer-specified constants • Can be converted to int representation with a cast 11 12 2

  3. Defining “Synonyms”: typedef typedef int color; color red, blue, green;  A common use is with enumeration types 13 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend