MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Fear of Input

Fearful typing errors
Perhaps the author is quite cynical.
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.
Misplaced delimiters
As an example of what might happen first, let's try typing the delimiter inc orrectly.
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.

source code
Enter minimum and maximum values separated by ,. :100 200
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
Next, we would like to have the user enter an extremely large 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.

Source Code
Enter minimum and maximum values separated by ,. :1,70000
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
I would like to try a more heavenly experiment.
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.

source code
Enter minimum and maximum values separated by ,. :ABCDEF
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
You may be tired of being shown all sorts of input errors all over the place.
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.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box