Chapter 16: Standard Function Library
Github
Link: ch16. Standard function library
The library function can greatly simplify the usual business processing, and there is no need to re-create the wheels, and the quality of the wheels made by myself is not good...but I personally think that some library functions that are not commonly used can be understood and know what they do. That’s fine, and it’s really only used in certain business processes. Just check the documentation when you use it.
Summary of this chapter and points to note
Answers to some after-school exercises
16.12 Question
-
This is the
UB
behavior of the basebase
in the range of 0,2 to 36, in the bookP329
can be seen definitions. This may be able to output a string of results, but it is meaningless and portability will be greatly reduced. -
Yes, all that is needed are numbers that have no obvious relationship to each other. Just enough, it can be used to create data and write a match!
-
Obviously, if 100 random numbers are generated and the range is limited between 0 and 1, then the result depends on the "quality" of the random number.
Haha, my random numbers are not so regular yet, see you
demo01.c
. -
Did not understand what the question means. The answer to the very good use of sleep function
sleep()
statistical whether this time will be able to be counted separately out. See the answer: It is definitely not easy to do. Some implementations provide a sleep function to pause the program for some time; ifa
during the sleep clock value continues to increase, and then measuring the elapsed time. If you don't have this, you can try to read from standard input without input for ten seconds. If the clock continues to increase at this time, it is a measure of the running time on the machine or operating system is not very good at managing theirI/O
equipment. Another option is to start another project; if the clock is in the first while the second program is running, it is measuring the running time. -
Why do I only realize that the month needs to be incremented by 1? You really need to pay attention to several functions passing parameters! See
P332
andP333
. -
The main problem is the call to
setjmp
invoke the function returnslongjmp
. This means that the state information stored in the jump buffer is no longer valid, so the result is unpredictable. In contrast to this, the fact is that the main function does not check whether the number of command line parameters given is small. What happens when this is executed? It depends on the specific machine. Some will abort the return address illegal activities returned from the numberset_buffer
function. Others will enter an infinite loop somewhere. In particularRISC
machine, it does not have the function parameters are stored in the return address on the stack, it will enter an infinite loop. SeeP337
Warnings section. -
Most
OJ
directlySE
, but in my native compiler has onlywarning
estimated open warning level is too low, the programmer should avoid this situation, the implementation of the two results are random. -
Sort in descending order.
16.13 Programming Exercises
-
Very good one subject, simple realization of a
p
band, though by means of library functions. Using themain()
function parameter passing command line arguments to the reception is very good, very standard for the code! Seedemo03.c
. -
It is actually a very common type of questions, related to the probability, if you think you simply need to
%6
be mapped to a simple, but apparently still too young ah. See answer: -
A simple mock question, see
demo04.c
. -
There is a formula for calculating the week by year, which seems to be called Kim Larson's formula.
tm_wday
Do not need to manually assign, after the conversiontm
structure will be formatted, you can easily determine the day of the week! ! ! SeeP335
. Seedemo05.c
. -
Note
△t
on the line. Seedemo06.c
. -
Common mistakes: the
payment
number of parameters passed as an integer, the number of years and failed to interest converted to monthly equivalent, failed to convert decimals interest, not rounded to the nearest penny. -
I'm too lazy to do this experiment, after referring to the answer book.
-
sscanf
Will no longer work because there may be any number of ages. Instead, we will usestrtol
.strtol
In this leading spaces can be ignored, and the conversion in the decimal sense, is really too convenient, directly to the second argument tochar *buf
fetch address is&buf
passed as a secondary pointer into very spiritual. Seedemo08.c
. Contrast can lookch15 README.md
programming 7. -
I did this experiment when I talked about probability in a high school math class. The teacher also helped select a pair of classmates in the same year and the same month. The answer is surprising: in a group of 30 people, there is a 70% chance that at least two of their birthdays will be on the same day. Only a group of around 23 members can win the tie. My computer is not being introduced
<malloc.h>
... should<stdlib.h>
include space allocation function, and can also be normal use, do not go toss. Seedemo09.c
. -
I have summarized before: [Sorting Algorithm] 0. Column introduction and links to articles. Insertion is better than bubbling. When the number of reverse order pairs is equal, the number of insertion exchanges will be less!
Essay
-
Random function
rand()
andsrand()
random seeds. Although I have written a lot of algorithmic questions, I haven't used the data to test the program myself. This has to be put on the agenda in the near future. -
String conversion functions are also very important. Just read it when you use it. But
LeetCode
in allows you to achieveatoi()
andatof()
is really quite disgusting. -
As
abs()
andfabs()
do not know nowabs()
support floating-point numbers yet, but still in accordance with the criteria on the line. -
I haven't used trigonometric functions and hyperbolic functions very much... I have proved the bottom-changing formula easily, and I'm born.
-
Floating-point number representation, this is not very understandable. Floating-point number standards may vary from machine to machine. Can these functions be used to transfer floating-point numbers?
-
floor()
,ceil()
The need to pay attention to a negative number, thenC++
in\
a simple rounding to 0, and-3 / 2
is simply -1, butfloor(-3/2)
is -2,ceil(-3/2)
-1. Please pay attentionC++
to this, take the value towards zero, and ignore the decimal places. If you want to round up, add 0.5 after it. And in some cases, it needs to be rounded down, and the rounding down formula has a corresponding identity transformation, I forgot... -
Just look at the date function.
-
This non-local jump is a bit showy, I haven't seen it before. Combined with
P337
examples to help. But I feel I still use less... -
The suggestion of signal is to look at the operating system directly, it will explain it more clearly. Stereotyped essays such as inter-process communication must be memorized. Signal processing functions,
volatile
etc. are all basic knowledge. -
Just print the variable parameters to understand the purpose.
-
assert()
Macro is still quite common, it is a macro, not a function! As for sortingqsort()
and binary searchbsearch()
function, also found in theP345
Notes program beginning inbsearch
spelling errors. But I really have not used these two functions in this, write a customcmp()
function, pass the function name equal to the transfer function pointers can be called. -
locale
Just look at it briefly, you can format numbers, currency and other representations, which is more convenient and can improve portability.
doubt
-
Distinction
floor()
andceil()
difference function, and when dealing with two different roles in the negative. -
Non-local jumps are a bit silly.
-
Signals and
locale
are simply looked at. -
4 weeks to determine the programming problem quite the show, it is convenient,
mktime()
after organizing internal formatted directly, call the structure memberstm_wday
can get a few weeks. -
8 using the programming problem
strtol()
to be converted to digital space-numeric string line spacing, and it is very beautiful. The feature of using its second parameter and ignoring leading spaces to solve this problem is that it can choose the base.strtol()
Is designed to handle integers, therebase
may be provided, andstrtod()
is designed to handle floating-point character string, it is no setbase
up