MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

input function

The need for input.
With the content covered in the previous chapter, if it involves calculations using the four basic arithmetic operations, you can compute even the most complex expressions.
But settling for this shows a lack of ambition.

In the previous chapters, we were directly writing numbers into the program.
Essentially, when a program runs, the values are already determined.
That means the program can only perform the same calculation every time.

"If we do this, we'll have to rewrite the program if we want to calculate with a different number."
It's practically the same as using a high-end calculator.
It's not an exaggeration to say that programming feels completely pointless.

The way to solve this problem is to enter the number from the keyboard.
It would be more convenient if you could enter numbers each time the program runs.
scanf function
"as if using the printf function when displaying on the screen,"
We also use functions for input from the keyboard.
For that purpose, C provides the scanf function (scan-eff).
The `scanf` function is used as follows:

scanf function
scanf(" Input conversion specifier",&variable名);

An input conversion specifier is a character that indicates how to convert an input number into a numerical value.
You can use it in much the same way as the output conversion specifiers used with the printf function.

The variable name specifies the variable that will store the input data.
When using the scanf function, you must put the & character before the variable name.
This & indicates the location of a variable, but the details will be explained in Chapter 15.

When this function is executed, the program will enter a waiting state for input.
When a user of the program enters data and presses the Enter key,
That data will be assigned to the specified variable.

Note that using the scanf function requires #include .
There's no need to add it again, as it's the same as when using the printf function.
Numerical input
With this much knowledge, you can input data from the keyboard.
The following program is an example of using the scanf function to display the entered numbers.

Source code
#include <stdio.h>

int main(void)
{
    int data;
    scanf("%d", &data); /* 入力部minute */
    printf("%d\n", data);
    return 0;
}

Here's an example of the program's output:
The weakly displayed string is a sentence added for explanation and is not actually displayed.

Solution 2-1
100 入力したデータ
100 表示されたデータ

This program will wait for input.
Then, enter a number and press the return key, and that number will be displayed.
Of course, numbers other than 100 are also acceptable.

The scanf function can accept not only integer inputs, but also floating-point numbers.
However, for real numbers, you must use the %lf specifier.
Please be careful, the printf function uses the %f specifier.
The following program is an example of using the scanf function to read a floating-point number and then display it.

Source code
#include <stdio.h>

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

Here's an example of the program's output:
The weakly displayed string is a sentence added for explanation and is not actually displayed.

Solution 2-1
175.128 入力したデータ
175.128000 表示されたデータ

Multiple inputs
The `scanf` function isn't limited to accepting only one piece of data at a time.
Using multiple specifiers allows you to perform multiple inputs at once.
The following program takes two numbers as input using the scanf function and then displays them.

Source code
#include <stdio.h>

int main(void)
{
    int data1, data2;
    scanf("%d%d", &data1, &data2); /* 入力部minute */
    printf("%d , %d\n", data1, data2);
    return 0;
}

Here's an example of the program's output:

Solution 2-1
100 200 入力したデータ
100 , 200 表示されたデータ

Here, we are using two %d specifiers to allow the input of two numbers at once.
When entering, please separate the two numbers with a space, tab, or newline.

"By the way, the reason I'm declaring int data1, data2; in this program is..."
It's a convenient way to declare multiple variables of the same type.
"Rather than declaring them in two separate lines, like 'int data1; int data2;'"
It's easier if we put it all on one line.

You can also enter the two numbers as separate types.
For example, if you provide a format specifier like "%d%lf",
The first can be an integer, and the second can be a real number.

You can also specify the delimiter to use.
"In this case, specify a separator between the two specifiers."
The following program prompts the user to enter two numbers, separated by a comma.

Source code
#include <stdio.h>

int main(void)
{
    int data1, data2;
    scanf("%d,%d", &data1, &data2); /* 入力部minute */
    printf("%d , %d\n", data1, data2);
    return 0;
}

Here's an example of the program's output:

Solution 2-1
100,200 入力したデータ
100 , 200 表示されたデータ

This time, you will need to enter values separated by commas.
Instead, be aware that it will no longer be separated by spaces.
Simple Sigma Program
I'm not aiming to explain new concepts here, but rather to build a practical program.
The topic will be Simplified Sigma Calculation.

Sigma is something you learn in high school math, but it's not really that difficult.
"Calculating sums like 1+2+3+4+5+…+100 is what sigma notation represents."
For simplicity, let's calculate the sum of all integers between the minimum and maximum values.

Before we start writing the program, let me explain the formula.
The sum can be calculated as (min+max)×(max-min+1)÷2.

First, it's clear that this program requires two integer inputs.
And you'll need to calculate it and display the result.
Of course, all of this is achievable with just the knowledge we've learned so far.

Of course, the `scanf` function will be used for inputting the two numbers.
With the scanf function, no explanatory prompt is displayed before input.
We will use the printf function to display the explanation beforehand.

Now that we've thought this far, all that's left is to write the program.
The following program calculates and displays the sum of integers between a minimum and maximum value.

Source code
#include <stdio.h>

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

    /* 入力部minute */
    printf("最小値と最大値を , で区切って入力して下さい。:");
    scanf("%d , %d", &min, &max);

    /* 計算部minute */
    sum = (min + max) * (max - min + 1) / 2;

    /* 表示部minute */
    printf("%d~%d の合計は %d is.\n", min, max, sum);
    return 0;
}

Here's an example of the program's output:

Solution 2-1
最小値と最大値を , で区切って入力してください。:100,200
100~200 の合計は 15150 is.

This section contains a lot of tips for good programs, so let's go through them step by step.

First, pay attention to the results.
, separated by, clearly indicates the delimiters as well.”
"We make it clear by displaying the results, including showing the values from the input range."

The program declares three variables: min, max, and sum.
Each of these words represents minimum, maximum, and sum, respectively.
It's clear what the variable is used for.

I think variable names should be descriptive and ideally between 3 and 8 characters long.
You don't need to be overly concerned with the exact English meaning, so for example with "sum,"
another total, or
"Options like answer, solve, or solution could also be considered,"
I think it would also be a good idea to represent it as "goukei kotae" in Roman letters.
Whatever the case, avoid meaningless variable names and single-character variable names.

Please also note that the program is properly segmented and commented.
This program is divided into three stages: input, calculation, and display, and it is commented.
This could also be combined into a single process for calculation and display.
"We've deliberately divided it here for better clarity."

The Drawbacks of Comments
多くの入門書にはcommentを付けるよう書いているため、
Some people can be excessively verbose in their comments.
しかし、見ればわかる簡単な部minuteにcommentは不要is.
また、プログラムを修正したときにcommentがそのままだと、
"The comments do not align with the program, which can lead to misunderstandings."

The author's recommendation is to add comments at roughly defined sections, as in this example.
There's no single right way to provide feedback; different people have different approaches.

It's important for programs to be understandable by both those who create them and those who use them.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19