C Primer Plus读书笔记 · 15

这是我读 C Primer Plus 时的读书笔记。之前都发布在其他平台上。
从第15次以后均发布在CSDN上。

#2019.9.2#

#include<string.h>

The string.h file contains function prototypes for several string-related functions.

sizeof() give the size of the whole array while strlen() stops where there is a null character.

strlen() doesn’t catch the null character while sizeof() would.

The program didn’t tell the computer how much memory to set aside to store the phrase. It had to count the number of characters between the double quotes itself.

Using a %zd specifier for the type used by the sizeof() operator; this also applied for type returned by strlen().

You would use sizeof(char) or sizeof(float) but can use sizeof name or sizeof 6.28, however, sizeof(6.28) is also OK.

#define statement

Constants and the C Preprocessor

#define TAXRATE 0.015

When your program is compiled, the value 0.015 will be substituted everywhere you have used TAXRATE. This is called a compile-time-substitution.

Such defined constants ae often termed manifest constants.

(#define NAME value should be placed before ***#include<stdio.h>***)

The reason why we use capitalized NAME is because that when you encounter one in the depth of your program, you know immediately that it is a constant.

The names you use for symbolic constant must satisfy the same rules that the names of variables do.

The %f1.2 in the printf() statement causes the printout to be rounded to two decimal places.

The #define statement can be used for character and constants, too.

#define BEEP ‘/a’
#define TEE ‘T’
#define ESC ‘\033’
#define OOPS “Now you have done it!”

The const Modifier

A second way to create symbolic constant

Tis makes MONTHS into a read-only value, which means you cannot alter the value of MONTHS.

A third way to create symbolic constants—enum facility (see ch14)

Manifest Constants on the Job

The C header files limits.h and float.h supply detailed information about the size limits of integer types and floating types, respectively. Each files defines a series of manifest constants that apply to your implementation.

(For more info please check the textbook from P109 – P112)

Exploring and Exploiting printf() and scanf()

These functions, like all other functions in the C library, were not part of the C definitions, which were left to the compiler writers for the compatibility of various implementation.

The printf() Function

Notations like %d, %c are called conversion specifications.
在这里插入图片描述
在这里插入图片描述

Using printf()

printf(Control-string, item1, item2)

If you want to print a % on the screen, just use two % symbols in your statement.

Conversion Specification Modifiers for printf()

(see The printf() Modifiers in the textbook from P116-P117)

Note Type Portability
The size of operator returns the size in bytes of a type or value. However, you were to use %u(unsigned) on one system, %lu(unsigned long) one another, and %llu(unsigned long long) on a third, which means you would need to research the correct usage for your system and you might be alter you program if you move it to another system.

  1. The stddef.h header file, included together with stdio.h, defines size_t to be whatever your system uses for sizeof (so called underlying type);
  2. printf() uses the z modifier to indicate the corresponding type for printing. Similarly, the ptrdiff_t type and t modifier to indicate whatever underlying signed interger type the system used for difference between two addresses.

Note Conversion of float Arguments
Under either K&R C or ANSI C, no special conversion specifier is needed for displaying type float. (Wondering reason for this check P117)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值