这是我读 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.
- 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);
- 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)