basic types
play

Basic Types C s basic (built-in) types: o Integer types, including - PDF document

2/4/14 Basic Types C s basic (built-in) types: o Integer types, including long integers, short integers, Basic Types and unsigned integers o Floating types ( float , double , and long double ) o char Based on slides from K. N. King


  1. 2/4/14 ¡ Basic Types • C ’ s basic (built-in) types: o Integer types, including long integers, short integers, Basic Types and unsigned integers o Floating types ( float , double , and long double ) o char Based on slides from K. N. King o _Bool (C99) Bryn Mawr College CS246 Programming Paradigm Integer Types Integer Types • C supports two fundamentally different kinds of • Typical ranges of values for the integer types on a numeric types: integer types and floating types. 16-bit machine: • Values of an integer type are whole numbers. Type Smallest Value Largest Value • Values of a floating type have a fractional part. short int –32,768 32,767 unsigned short int 0 65,535 • The integer types, in turn, are divided into two int –32,768 32,767 categories: signed(default) and unsigned. unsigned int 0 65,535 • The leftmost bit of a signed integer (known as the long int –2,147,483,648 2,147,483,647 sign bit ) is unsigned long int 0 4,294,967,295 o 0 – the number is positive or zero , o 1 – negative. Integer Types Integer Types • Typical ranges on a 32-bit machine: • Typical ranges on a 64-bit machine: Type Smallest Value Largest Value Type Smallest Value Largest Value short int –32,768 32,767 short int –32,768 32,767 unsigned short int 0 65,535 unsigned short int 0 65,535 –2,147,483,648 2,147,483,647 –2,147,483,648 2,147,483,647 int int unsigned int 0 4,294,967,295 unsigned int 0 4,294,967,295 long int –2,147,483,648 2,147,483,647 long int –2 63 2 63 –1 4,294,967,295 2 64 –1 unsigned long int 0 unsigned long int 0 • The <limits.h> header defines macros that represent the smallest and largest values of each integer type. 1 ¡

  2. 2/4/14 ¡ Integers Constants Octal and Hexadecimal Numbers • Octal numbers use only the digits 0 through 7. • Constants are numbers that appear in the text of a program. • Each position in an octal number represents a power of 8. • C allows integer constants to be written in decimal o The octal number 237 represents the decimal (base 10), octal (base 8), or hexadecimal (base 16). number 2 × 8 2 + 3 × 8 1 + 7 × 8 0 = 128 + 24 + 7 = 159. • A hexadecimal (or hex) number is written using the digits 0 through 9 plus the letters A through F, which stand for 10 through 15, respectively. o The hex number 1AF has the decimal value 1 × 16 2 + 10 × 16 1 + 15 × 16 0 = 256 + 160 + 15 = 431. Integer Constants Integer Constants • Decimal constants contain digits between 0 and 9, but must • To force the compiler to treat a constant as a long not begin with a zero: integer, just follow it with the letter L (or l ): 15 255 32767 15L 0377L 0x7fffL • Octal constants contain only digits between 0 and 7, and • To indicate that a constant is unsigned, put the must begin with a zero: letter U (or u ) after it: 017 0377 077777 • Hexadecimal constants contain digits between 0 and 9 and 15U 0377U 0x7fffU letters between a and f , and always begin with 0x : • L and U may be used in combination: 0xf 0xff 0x7fff 0xffffffffUL • The letters in a hexadecimal constant may be either upper or The order of the L and U doesn ’ t matter, nor does lower case: their case. 0xff 0xfF 0xFf 0xFF 0Xff 0XfF 0XFf 0XFF Integer Overflow Reading and Writing Integers • When arithmetic operations are performed on integers, • When reading or writing an unsigned integer, use the it ’ s possible that the result will be too large to letter u , o , or x instead of d in the conversion represent. specification. • If the result can ’ t be represented as an int (because it unsigned int u; requires too many bits), we say that overflow has scanf("%u", &u); /* reads u in base 10 */ occurred. printf("%u", u); /* writes u in base 10 */ o When overflow occurs during an operation on signed scanf("%o", &u); /* reads u in base 8 */ integers, the program ’ s behavior is undefined. printf("%o", u); /* writes u in base 8 */ o When overflow occurs during an operation on unsigned scanf("%x", &u); /* reads u in base 16 */ integers, the result is defined: we get the correct answer printf("%x", u); /* writes u in base 16 */ modulo 2 n , where n is the number of bits used to store the result. 2 ¡

  3. 2/4/14 ¡ Reading and Writing Integers Floating Types • When reading or writing a short integer, put the • C provides three floating types, corresponding to letter h in front of d , o , u , or x : different floating-point formats: o float Single-precision floating-point short s; o double Double-precision floating-point scanf("%hd", &s); printf("%hd", s); o long double Extended-precision floating-point (rarely used) • When reading or writing a long integer, put the letter l ( “ ell, ” not “ one ” ) in front of d , o , u , or x . • Macros that define the characteristics of the floating types can be found in the <float.h> header. Reading and Writing Floating Constants Floating-Point Numbers • By default, floating constants are stored as double- • %e , %f , and %g : reading and writing single-precision precision numbers. floating-point numbers. • To indicate that only single precision is desired, put • When reading a value of type double , put the letter l in front of e , f , or g : the letter F (or f ) at the end of the constant (for double d; example, 57.0F ). scanf("%lf", &d); • To indicate that a constant should be stored in • Use l only in a scanf format string, NOT a printf string. long double format, put the letter L (or l ) at the • In a printf format string, the e , f , and g conversions can end ( 57.0L ). be used to write either float or double values. • When reading or writing a value of type long double , put the letter L in front of e , f , or g. Use of char (character) Characters are Integers • A char type represents an integer value from 0 • Basic operations to 255 (1 byte) or –128 to 127. o Declaration: char c; • A single quoted character is called a “ character o Assignment: c = 'a'; constant ” . o Reference: c = c + 1; • C characters use ASCII representation: • 'A' = 65 … 'Z' = 'A' + 25 = 90 • Constants • 'a' = 97 … 'z' = 'a' + 25 = 122 o Single-quoted character (only one) • '0'!= 0 (48), '9' - '0' = 9 o Special characters: '\n' , '\t' (tab), • Never make assumptions of char values '\"' (double quote), '\'' (single quote), o Always write 'A' instead of 65 '\\' (backslash) 3 ¡

  4. 2/4/14 ¡ ASCII Table Escape Sequences • A character constant is usually one character American Standard Code enclosed in single quotes. for Information Interchange • Escape sequences provide a way to represent A standard way of special characters that are invisible (nonprinting) or representing the alphabet, can ’ t be entered from the keyboard. numbers, and symbols • There are two kinds of escape sequences: (in computers) character escapes and numeric escapes. wikipedia on ASCII Character Escapes Numeric Escapes • A complete list of character escapes: • Character escapes Name Escape Sequence o don ’ t exist for all nonprinting ASCII characters. Alert (bell) \a o useless for representing characters beyond the basic Backspace \b 128 ASCII characters. Form feed \f New line \n • Numeric escapes can represent any character. Carriage return \r Horizontal tab \t • A numeric escape for a particular character uses the Vertical tab \v character ’ s octal or hexadecimal value. Backslash \\ Question mark \? • For example, the ASCII escape character (decimal Single quote \' value: 27) has the value 33 in octal and 1B in hex. \ " Double quote Escape Sequences Escape Sequences • An octal escape sequence consists of the \ • When used as a character constant, an escape character followed by an octal number with at most sequence must be enclosed in single quotes. three digits, such as \33 or \033 . o E.g., '\33' (or '\x1b' ) for decimal value 27. • A hexadecimal escape sequence consists of \x • It’s often a good idea to use #define to give followed by a hexadecimal number, such as \x1b them names: or \x1B . #define ESC '\33' • The x must be in lower case, but the hex digits can • Escape sequences can also be embedded in strings. be upper or lower case. 4 ¡

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