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

Functions for input

Need for input
With the previous chapters, you can calculate any complex expression using the four arithmetic operations.
However, it is too low-minded to be satisfied with this.

In the methods used up to the previous chapter, the numbers were written directly into the program.
In other words, the numerical value is already determined when the program is executed.
This means that the program can only perform the same calculation each time.

If the user wants to use a different number, the program must be rewritten.
This is almost the same as using a high-end calculator.
It is no exaggeration to say that there is no point in programming at all.

The way to break through this problem is to enter numbers from the keyboard.
Entering a number each time the program is run would be more convenient.
scanf function
Just as you used the printf function when displaying to the screen, you can use the
When inputting data from the keyboard, a function is also used for this purpose.
For this purpose, the C language provides the scanf function.
The scanf function is used as follows

scanf function
scanf(" input conversion specifier",&variable name);

An input conversion specifier is a character that indicates what kind of number the input number should be converted to.
It can be used in much the same way as the output conversion specifier used in the printf function.

The variable name specifies the name of the variable that will store the input data.
When using the scanf function, the variable name must be preceded by the & character.
The & is meant to signal the location of the variable, which will be explained in detail in Chapter 15.

When this function is executed, the program is waiting for input.
The user of the program types the data and presses the return (Enter) key.
The data is assigned to the specified variable.

Note that #include <stdio.h> is required to use the scanf function.
It is the same as using the printf function, so there is no need to add it again.
Numeric input
Once you know all this, you can enter data from the keyboard.
The following program is an example of using the scanf function to display the numbers as they are entered.

source code
 #include <stdio.h>

int main(void)
{
    int data;
    scanf("%d", &data); /* input part */
    printf("%d\n", data);
    return 0;
}

An example of the results of executing this program would be as follows
Weakly displayed strings are sentences added for illustrative purposes and are not actually displayed.

Solution 2-1
100 Input data
100 Data displayed

When this program is executed, it enters a state waiting for input.
There, enter a numerical value and press the return key, and the value will be displayed as is.
Naturally, a number other than 100 is also acceptable.

The scanf function allows the input of real numbers as well as integers.
However, for real numbers, the %lf specifier must be used.
Note that in the printf function, it was the %f specifier.
The following program is an example of using the scanf function to display a real number as it is entered.

source code
 #include <stdio.h>

int main(void)
{
    double data;
    scanf("%lf", &data); /* input part */
    printf("%f\n", data);
    return 0;
}

An example of the results of executing this program would be as follows
Weakly displayed strings are sentences added for illustrative purposes and are not actually displayed.

Solution 2-1
175.128 Data entered
175.128000 Data displayed

Multiple inputs
The scanf function does not allow only one data entry at a time.
By using multiple specifiers, it is possible to perform multiple inputs at once.
The following program uses the scanf function to enter and display two numbers.

source code
 #include <stdio.h>

int main(void)
{
    int data1, data2;
    scanf("%d%d", &data1, &data2); /* input part */
    printf("%d , %d\n", data1, data2);
    return 0;
}

An example of the results of executing this program would be as follows

Solution 2-1
100 200 Input data
100 , 200 Data displayed

Here, two %d specifiers are specified to allow two numbers to be entered at once.
When entering the data, separate the two numbers with a space, tab, or line break.

Incidentally, in this program, int data1,data2;, is used as
This is a convenient way of writing when declaring multiple variables of the same type.
Rather than declaring int data1; int data2;, and twice
It is easier to put them on one line.

It is also possible to have two numbers entered with different types.
For example, if you supply the specifier "%d%lf", then
The first can be entered as an integer and the second as a real number value.

It is also possible to specify a delimiter and have the user enter that delimiter.
In this case, the delimiter is specified between the two specifiers.
The following program asks the user to enter two numbers separated by ,.

source code
 #include <stdio.h>

int main(void)
{
    int data1, data2;
    scanf("%d,%d", &data1, &data2); /* input part */
    printf("%d , %d\n", data1, data2);
    return 0;
}

An example of the results of executing this program would be as follows

Solution 2-1
100,200 Data input
100 , 200 Data displayed

This time, when entering the data, it is to be separated by ,.
Instead, you will not be able to separate them with spaces, etc.
Simplified Sigma Program
Here, I would like to make a practical program, not an explanation of new matters.
The subject will be a simple sigma calculation.

Sigma is something you learn in high school math, but basically it is not that difficult.
Sigma is to calculate 1+2+3+4+5+・・・・100 etc.
For simplicity's sake, we will find the sum of the integers between the minimum and maximum values.

Before we program, let us explain the formulas.
The total value is obtained by (min+max)×(max-min+1)/2.

First, we see that the program requires two integer inputs.
It then needs to calculate it and display the result.
Naturally, all of this can be accomplished with only the knowledge we have learned so far.

We'll obviously use the scanf function to input the two numbers, but we'll also use the
The scanf function does not display any explanation before inputting the data.
We will use the printf function to display the description in advance.

Once you have thought this far, all you have to do is type the program.
The following program finds and displays the sum of integers between the minimum and maximum values.

source code
 #include <stdio.h>

int main(void)
{
    int min, max, sum;

    /* Input part */
    printf("Please enter the minimum and maximum values separated by ,. :");
    scanf("%d , %d", &min, &max);

    /* Calculation part */
    sum = (min + max) * (max - min + 1) / 2;

    /* Display */
    printf("The sum of %d to %d is %d. \n", min, max, sum);
    return 0;
}

An example of the results of executing this program would be as follows

Solution 2-1
Enter minimum and maximum values separated by ,. :100,200
The sum of 100-200 is 15150.

This section contains many tips for a good program and will be explained step by step.

First, note the results of the execution.
In addition to clearly indicating the delimiter symbols, separated by , and
The display of results is also made easier to understand by displaying the range of values entered.

Three variables, min, max, and sum, are declared in the program.
Each of these words is an English word meaning minimum, maximum, or total, respectively.
It is easy to see what the variables are used for.

Variable names should be easy to understand and 3 to 8 characters long.
You don't have to be particular about the English meaning of the word, so for the sum example, you can use
Another total, total, or
You can also think of answer, solve, solution, and so on.
It is also a good idea to use the Roman alphabet goukei kotae.
In any case, please avoid meaningless variable names, one-letter variable names, etc.

One more thing, note that the program is properly divided and commented.
The program is divided into three stages: input, compute, and display, with comments.
Of these, the calculation and display could be lumped together.
Here, we have purposely divided the data because it is easier to understand.

The downside of comments
Because many introductory texts ask you to make comments
Some people may make extreme comments.
However, comments are unnecessary for simple parts that can be understood by looking at them.
Also, when a program is modified, if the comments are left as they are
This can be a source of misunderstanding because the comments and the program do not match.

The author recommends writing comments at each general break, as in this example.
There are many different styles of commenting, and there is no correct answer.

Thus, it is important to make the program easy to understand for both the creator and the user.


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