Fear of Input
Fearful typing errors
While the previous section presents it as an example of a good program
Because I am now going to complain that there are problems with that program.
What in the world was wrong with the program I just described?
Actually, the problem is the use of the scanf function.
The scanf function is a function that has many problems.
Despite its relatively common use in introductory texts, it is not used in serious development.
Some companies (schools) even have a scanf function ban.
However, the program I just created worked without any particular problem.
What on earth is wrong with ・・・・, it is an input error.
As long as it is used correctly, the scanf function does not cause any particular problem.
A typing error can instantly cause terrible problems.
We will now present a case of input error, using the previous program as a subject.
In the previous program, the , was used to separate the two.
I dare to ignore this and try to separate them with a space.
The following is the execution result when the delimiter is typed incorrectly.
The sum of 100 to 307 is 9560.
The first 100 that I entered seems to have been entered correctly.
The 200 that follows is somehow being converted to a number such as 307.
This result depends on the compiler you use.
If you all try to run it, you may get different results.
Too large a number
Note that modern compilers allow you to input even quite large numbers.
If you want to try this yourself, use a 16-bit compiler (LSI C-86).
The following are the execution results when too large a number is entered.
The sum of 1-4464 is 4408.
I thought I entered 70000, but for some reason it is recognized as 4464.
Moreover, even the answer is too small by any stretch of the imagination to be the sum of 1-4464.
These are problems caused by exceeding the limits of what can be stored.
Even if you try to check the input, the input variable itself is assigned 4464, so the
It is impossible to distinguish whether you wanted to put 4464 or if you mistyped it and got 4464.
Fear of Strings
That is, you are asked to enter a number, but you try to enter a string.
The following are the execution results when a string is entered.
The sum of 11975 - 307 is -16199.
It is no longer a mess.
How this happened can be explained in part by the correspondence of the bits.
I won't explain because there is no point in explaining.
The important point is that you are entering a string, but somehow it is being calculated.
Where did the numbers 11957 or 307 come from?
Of course, it is impossible to determine if this number itself was entered or if it was a typing error.
You might think that no one would ever make such a dumb mistake, but
You might be sleepwalking, or your pet cat might step on your keyboard.
Even more troubling, viruses and hackers may intentionally make such entries.
If this happened to a program used by a large corporation or government....
About the Solution
It is time to offer a solution or you will all get fed up.
But actually, the solution to this problem is not easy.
At least not with your current knowledge of this.
The solution is generally to have the data entered as a string of characters and then parse the data.
So, to solve this problem, you have to know how to process strings, but
Your current knowledge does not allow you to perform string processing.
I will introduce a solution to this problem much later.
Until then, we have no choice but to continue using the scanf function.
In the meantime, please be careful to avoid typing errors.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.