1 / 55

Basic Data Types

Basic Data Types. Lecture 3. Overview. The basic data types that C uses: int , short , long , unsigned , char , float , double Operator: s izeof () Function: s canf () The distinctions between integer types and floating-point types

gilles
Télécharger la présentation

Basic Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Data Types Lecture 3

  2. Overview The basic data types that C uses: int,short,long,unsigned,char,float, double Operator: sizeof() Function: scanf() The distinctions between integer types and floating-point types How to use the printf() and scanf() functions to read and write values of different types

  3. A Simple program To provide keyboard input to the program, use the scanf() function. The %finstructs scanf() to read a integer number from the keyboard, and the &weight tells scanf() to assign the input value to the variable named weight. Use the .2 modifier to the %f specifier to fine-tune the appearance of the output so that it displays two places to the right of the decimal.

  4. Interactive program The interactive approach makes programs more flexible; The sample program can be used for any reasonable weight; The scanf() and printf() functions make this interactivity possible.

  5. Warnings • A warning, means you've done something that is valid code but possibly is not what you meant to do. • A warning does not stop compilation.

  6. Errors • An error message means you did something wrong. • It prevents the program from being compiled. • For the other two types of errors see previous lecture!

  7. Data Variables and Constants Some types of data are preset before a program is used and keep their values unchanged throughout the life of the program. These are constants. Other types of data may change or be assigned values as the program runs; these are variables. In the sample program, weight is a variable and 38.53 is a constant. What about 1000? The price of gold isn't a constant in real life, but this program treats it as a constant.

  8. Constants Define a macro constant

  9. Constants

  10. Bits, Bytes, and Words The terms bit, byte, and word can be used to describe units of computer data or to describe units of computer memory. We'll concentrate on the second usage here. The smallest unit of memory is called a bit. It can hold one of two values: 0 or 1. (Or you can say that the bit is set to "off" or "on.") The byte is the usual unit of computer memory. For nearly all machines, a byte is 8 bits, and that is the standard definition, at least when used to measure storage. A word is the natural unit of memory for a given computer design.

  11. Constants • Entities that appear in the program code as fixed values. • Any attempt to modify a CONSTANT will result in error. • 4 types of constants: • Integer constants • Positive or negative whole numbers with no fractional part • Example: • constintMAX_NUM = 10; • constintMIN_NUM = -90; • Floating-point constants (float or double) • Positive or negative decimal numbers with an integer part, a decimal point and a fractional part • Example: • const double VAL = 0.5877e2; (stands for 0.5877 x 102)

  12. Constant • Character constants • A character enclosed in a single quotation mark • Example: • const char letter = ‘n’; • const char number = ‘1’; • printf(“%c”, ‘S’); • Output would be: S • Enumeration • Values are given as a list • Example:

  13. Basic Data Types • There are 4 basic data types : • int • float • double • char • int • used to declare numeric program variables of integer type • whole numbers, positive and negative • keyword: int intnumber; number = 12; -32,768 ~ 32,767 ( 16bit @ 16bit machine)

  14. Basic Data Types 3.4e-38 ~ 3.4e38 ( 32bit @ 16bit machine) • float • fractional parts, positive and negative • keyword: float float height; height = 1.72; • double • used to declare floating point variable of higher precision or higher range of numbers • exponential numbers, positive and negative • keyword: double double valuebig; valuebig = 12E-3; 1.7e-308 ~ 1.7e308 ( 64bit @ 16bit machine)

  15. Basic Data Types -128 ~ 127( 8bit @ 16bit machine) • char • equivalent to ‘letters’ in English language • Example of characters: • Numeric digits: 0 - 9 • Lowercase/uppercase letters: a - z and A - Z • Space (blank) • Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc • single character • keyword: char charmy_letter; my_letter = 'U’; The declared character must be enclosed within a single quote!

  16. Difference between integer and floating-point numbers An integer has no fractional part; a floating-point number can have a fractional part. Floating-point numbers can represent a much larger range of values than integers can. For some arithmetic operations, such as subtracting one large number from another, floating-point numbers are subject to greater loss of precision. Floating-point values are often approximations of a true value. Floating-point operations are normally slower than integer operations.

  17. Primary data types in C

  18. Floating point representation float

  19. Double precision representation double C allows for a third floating-point type: long double. The intent is to provide for even more precision than double. However, C guarantees only that long double is at least as precise as double.

  20. Hierarchy of Integer Types • C offers three adjective keywords to modify the basic integer type: short, long, and unsigned. • The type short intor, more briefly, short may use less storage than int, thus saving space when only small numbers are needed. Like int, short is a signed type. • The type longlong int, may use more storage than long, thus enabling you to express even larger integer values. Like int, longlong is a signed type. • The type unsigned int, or unsigned, is used for variables that have only nonnegative values. For example, a 16-bit unsigned intallows a range from 0 to 65535 in value instead of from –32768 to 32767.

  21. Why Multiple Integer Types? The idea is to fit the types to the machine. The most common practice today is to set up long longas 64 bits, long as 32 bits, short as 16 bits, and intto either 16 bits or 32 bits, depending on the machine's natural word size. The minimum range for both short and intis –32,767 to 32,767, corresponding to a 16-bit unit, and the minimum range for long is –2,147,483,647 to 2,147,483,647, corresponding to a 32-bit unit.

  22. long Constants and long long Constants When you use a number such as 2345 in your program code, it is stored as an inttype. What if you use a number such as 1000000 on a system in which intwill not hold such a large number? Then the compiler treats it as a longint, assuming that type is large enough. If the number is larger than the long maximum, C treats it as unsignedlong. If that is still insufficient, C treats the value as longlongor unsigned longlong, if those types are available. Octal and hexadecimal constants are treated as type intunless the value is too large. Then the compiler tries unsigned int. If that doesn't work, it tries, in order, long, unsigned long, longlong, and unsigned longlong.

  23. When do you use the various inttypes? First, consider unsigned types. It is natural to use them for counting because you don't need negative numbers, and the unsigned types enable you to reach higher positive numbers than the signed types. Use the long type if you need to use numbers that long can handle and that intcannot. Similarly, use long longif you need 64-bit integer values.

  24. Integer Overflow The unsigned integer j is acting like a car's odometer. When it reaches its maximum value, it starts over at the beginning. The integer i acts similarly. The main difference is that the unsigned intvariable j, like an odometer, begins at 0, but the intvariable i begins at –2147483648

  25. Octal and Hexadecimal • When dealing with a large number of bits, it is more convenient and less error-prone to write the binary numbers in hex or octal. VS C assumes that integer constants are decimal, or base 10, numbers. Octal (base 8) and hexadecimal (base 16) numbers are popular with many programmers. Because 8 and 16 are powers of 2, and 10 is not, these number systems occasionally offer a more convenient way for expressing computer-related values.

  26. Displaying Octal and Hexadecimal dec = 100; octal = 144; hex = 64 dec = 100; octal = 0144; hex = 0x64 To display an integer in octal notation instead of decimal, use %o instead of %d. To display an integer in hexadecimal, use %x. If you want to display the C prefixes, you can use specifiers%#o, %#x, and %#X to generate the 0, 0x, and 0X prefixes, respectively

  27. Printing short, long, long long, and unsigned Types To print an unsigned intnumber, use the %u notation. To print a long value, use the %ldformat specifier. %hddisplays a short integer in decimal form, and %ho displays a short integer in octal form. Both the h and l prefixes can be used with u for unsigned types.

  28. Printing short, long, long long, and unsigned types First, note that using the %dspecifier for the unsigned variable produces a negative number! The reason for this is that the unsigned value 3000000000 and the signed value –129496296have exactly the same internal representation in memory on our system. The third line of output illustrates this point. When the value 65537 is written in binary format as a 32-bit number, it looks like 00000000000000010000000000000001. Using the %hdspecifier persuaded printf() to look at just the last 16 bits; therefore, it displayed the value as 1

  29. Using Characters: Type char, Constants and Initialization character declaration: character initialization: A single letter contained between single quotes is a C character constant. If you omit the quotes, the compiler thinks that T is the name of a variable.

  30. The scanf function • Read data from the standard input device (usually keyboard) and store it in a variable. • General format: • scanf(“%d”, &variable); • Notice ampersand (&) operator : • C address of operator • it passes the address of the variable instead of the variable itself • tells the scanf() where to find the variable to store the new value or other specifiers depending on the variable type 0x124 0x128 0x12C 0x130 address: variable memory: &variable  0x128 4 bytes

  31. The scanffunction • If you want the user to enter more than one value, you serialize the inputs.  You can serialize input Common Conversion Identifier used in printf() and scanf() functions.

  32. Nonprinting Characters Example: • The single-quote technique is fine for characters, digits, and punctuation marks. • Some of the characters are nonprinting. • For example, some represent actions such as backspacing or going to the next line or making the terminal bell ring (or speaker beep) • There are two ways to define such variables. • Use reserved sequences • Use numeric codes form the character table (ASCII codes)

  33. Partial listing of ASCII code

  34. Printing Characters

  35. The showf_pt.c Program The printf() function uses the %f format specifier to print type float and double numbers using decimal notation, and it uses %e to print them in exponential notation To correctly print out dip use %LF and %LE Then the output will be

  36. Floating-Point Overflow and Underflow • This is an example of overflow—when a calculation leads to a number too large to be expressed. • The behavior for this case used to be undefined, but now C specifies that toobig gets assigned a special value that stands for infinity and that printf() displays either inf or infinity (or some variation on that theme) for the value. Suppose the biggest possible float value on your system is about 3.4E38 and you do this

  37. Floating-Point Round-off Errors 0.00000  older gcc on Linux -13584010575872.00000  Turbo C 1.5 4008175468544.000000  clang on Mac OS, MSVC++ Take a number, add 1 to it, and subtract the original number. What do you get? You get 1. A floating-point calculation, such as the following, may give another answer:

  38. Type Sizes

  39. The typesize.c program The sizeof operator gives the amount of storage, in bytes, required to store an object of the type of the operand. This operator allows you to avoid specifying machine-dependent data sizes in your programs.

  40. Number systems • Decimal • Binary • Octal • Hexadecimal

  41. Octal number system The octal number system has a base of eight. It has eight possible digits: 0,1,2,3,4,5,6,7. The digit positions in an octal number have weights as follows:

  42. Octal-to-Decimal Conversion An octal number can be converted to its decimal equivalent by multiplying each octal digit by its position weight

  43. Decimal-to-Octal Conversion 33 33 4 1 28 A decimal integer can be converted to octal by using the same repeated-division method, but with a division factor of 8 instead of 2.

  44. Octal-to-Binary Conversion Ex. 1 Ex. 2 The advantage of the octal number system is the ease with which conversion can be made between binary and octal numbers.

  45. Binary-to-Octal Conversion Ex. 2 Ex. 1 0 Converting from binary integers to octal integers is simply the revers of the foregoing process. The bits of the binary number are grouped into groups of three bits started at the LSB.

  46. Example • Thus, 17710 = 2618. It binary equivalent is 0101100012. Convert 17710 to eight-bit binary equivalent by first converting to octal

  47. Hexadecimal number system The hexadecimal number system used base 16. It used the digits 0 through 9 plus A,B,C,D,E and F.

  48. Hex-to-Decimal Conversion Ex. 1 Ex. 2 A hex number can be converted to its decimal equivalent by using the fact that each hex digit position has a weight that is a power of 16.

  49. Decimal-to-Hex Conversion Convert 42310 to hex Convert 21410 to hex D616 1A716 Decimal-to-hex conversion can be done using repeated division by 16.

  50. Hex-to-Binary Conversion The hexadecimal number system is used primarily as a “shorthand” method for representing binary numbers.

More Related