C Primer Plus 读书笔记 · 7&8

#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

  1. The smallest unit of memory is called a bit, which can hold 0 or 1 (‘off’ or ‘on’) —Basic building block;
  2. 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.
  3. 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:

  1. A floating=point number has a fractional part while an integer dose not.
  2. Floating-point can represent a much larger range of values.
  3. For some arithmetic operations, floating point numbers are subject to greater loss of precision.
  4. 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.
  5. 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:

  1. short int: Using less storage than int; signed;
  2. long int: Using more storage than int; signed;
  3. long long int: Using more storage than long; signed;
  4. 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;
  5. unsigned long int (or unsigned long) & unsigned short int (or unsigned short), C99 adds unsigned long long int (or unsigned long long);
  6. 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?

  1. The printf() function uses the %u specifier to display unsigned int values.
  2. 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

  1. Integers you enter will be realized as the type that could include them exactly unless you add suffixes for them;
  2. 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值