#2019.7.19#
Chapter 3 Data and C
A Sample Program
What’s New in This Program?
Data Variables and Constants
Data: Data-Type Keywords
Bits , Bytes and Words
- The smallest unit of memory is called a bit, which can hold 0 or 1 (‘off’ or ‘on’) —Basic building block;
- The Byte is the usual unit of computer memory, a byte is 8 bits (nearly all machines). There are 256 possible bit patterns of 0s and 1s that can fit in a8-bit byte, which can be used to represent the integers from 0 to 255 or to represent a set of characters.
- A word is the natural unit of memory for a given computer design. At the present, a word is 64 bits (Larger word sizes enables faster transfer of data and allow more memory to be accessed).
#2019.7.20#
Integers Versus Floating-Point Types
The Integer
The Floating-Point Number
3.16E7 means to multiply 3.16 by 10 to the 7th power, the 7 would be termed the exponent of 10;
The decimal analogy would be to write 7.0 as 0.7E1. Here, 0.7 is the fractional part, and the 1 is the exponent part.
Differences between two data types:
- A floating=point number has a fractional part while an integer dose not.
- Floating-point can represent a much larger range of values.
- For some arithmetic operations, floating point numbers are subject to greater loss of precision.
- Floating-point values are often approximations of a true value. For example, 7.0 might be stored as a 6.99999 float value— more about precision.
- Floating-point operations were once much slower than integer operations, however, we now have floating-point processors that close the gap.
Basic C Data Type
The int Type
ISO C specified that the minimum range for type int should be from -32768 to 32767;
Declaring an int Variable
Initializing a Variable
Type int Constants (also called int literals)
Printing int Values
%d is called a format specifier because it indicates the form that printf() uses to display a valus.
Octal and Hexadecimal
8 and 16 are powers of 2 while 10 is not, so in some ways it’s provides more conveniences to use numbers in octal or hexadecimal.
In C, special prefixes indicate what number base you are using. 0x or 0X for hexadecimal and 0 for octal.
No matter which way you choose to present your numbers, they are stored exactly the same way internally.
Displaying Octal and Hexadecimal
To display an integer in in these three number systems, you can use their respective specifiers: %o for octal, %x for hexadecimal, %d for decimal. If you want to display the C prefixes, you can use %#o, %#x, and %X to display the 0, 0x, and 0X prefixes.
Other Integer Types:
- short int: Using less storage than int; signed;
- long int: Using more storage than int; signed;
- long long int: Using more storage than long; signed;
- unsigned int (or unsigned): Used for variables that have only nonnegative values, which released a bit so that we have a range of 0 to 65535;
- unsigned long int (or unsigned long) & unsigned short int (or unsigned short), C99 adds unsigned long long int (or unsigned long long);
- The keyword signed can be used with any of the signed types to make your intent explicit.
Declaring Other Integer Types
Why Multiple Integer Types?
- The printf() function uses the %u specifier to display unsigned int values.
- The unsigned integer and integers are acting like cars’ odometers, for when they reaches their maximum value, they start over at the beginning. Also, you are not informed that they have exceeded (overflowed) their maximum value.
long Constants and long long Constants
- Integers you enter will be realized as the type that could include them exactly unless you add suffixes for them;
- Appending l (or L) to indicate a long type, appending ll (or LL) to indicate a long long type and adding a u (or U) to the suffix for unsigned type. If you want to a unsigned long long type, adding LLU or ULL to the suffix.